@numio/bigmath 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,43 +1,56 @@
1
1
  # @numio/bigmath
2
2
 
3
- @numio/bigmath is an arbitrary-precision arithmetic library. This library provides functions for performing arithmetic operations (addition, subtraction, multiplication, and division) on numbers of arbitrary length. It addresses the limitations of JavaScript's built-in number type, which suffers from precision loss when dealing with very large or very small numbers, or numbers with more than 15 significant digits.
4
-
3
+ @numio/bigmath is an arbitrary-precision arithmetic library. This library
4
+ provides functions for performing arithmetic operations (addition, subtraction,
5
+ multiplication, and division) on numbers of arbitrary length. It addresses the
6
+ limitations of JavaScript's built-in number type, which suffers from precision
7
+ loss when dealing with very large or very small numbers, or numbers with more
8
+ than 15 significant digits.
5
9
 
6
10
  ## Key Features and Benefits
7
11
 
8
- * **Arbitrary Precision:** Handles numbers of any length, avoiding the limitations of JavaScript's `Number` type. This allows for calculations with extremely large or small numbers without loss of precision.
9
- * **No Precision Loss:** Eliminates precision errors that occur when using numeric literals with more than 15 significant digits. The library ensures accurate calculations even with very long numbers.
10
- * **Four Basic Operations:** Provides functions for addition (`add`), subtraction (`sub`), multiplication (`mul`), and division (`div`).
11
- * **Decimal Handling:** Correctly handles decimal numbers and performs calculations accurately, including scenarios involving negative numbers.
12
- * **Division Precision Control:** The `div` function allows you to specify the number of digits after the decimal point for the result. The default precision is 20 digits.
13
- * **Easy to Use:** The library provides simple and intuitive functions for performing arithmetic operations.
14
-
12
+ - **Arbitrary Precision:** Handles numbers of any length, avoiding the
13
+ limitations of JavaScript's `Number` type. This allows for calculations with
14
+ extremely large or small numbers without loss of precision.
15
+ - **No Precision Loss:** Eliminates precision errors that occur when using
16
+ numeric literals with more than 15 significant digits. The library ensures
17
+ accurate calculations even with very long numbers.
18
+ - **Four Basic Operations:** Provides functions for addition (`add`),
19
+ subtraction (`sub`), multiplication (`mul`), and division (`div`).
20
+ - **Decimal Handling:** Correctly handles decimal numbers and performs
21
+ calculations accurately, including scenarios involving negative numbers.
22
+ - **Division Precision Control:** The `div` function allows you to specify the
23
+ number of digits after the decimal point for the result. The default precision
24
+ is 20 digits.
25
+ - **Easy to Use:** The library provides simple and intuitive functions for
26
+ performing arithmetic operations.
15
27
 
16
28
  ## How it Solves the Problem
17
29
 
18
- JavaScript's `Number` type uses a 64-bit floating-point representation (IEEE 754), which can lead to precision issues when dealing with numbers that exceed its representable range or require more than 15 significant digits. This library likely uses a different representation internally (e.g., strings or arrays of digits) to store and manipulate numbers, effectively bypassing the limitations of the built-in `Number` type. This allows it to perform calculations on numbers of virtually unlimited size and maintain accuracy.
30
+ JavaScript's `Number` type uses a 64-bit floating-point representation (IEEE
31
+ 754), which can lead to precision issues when dealing with numbers that exceed
32
+ its representable range or require more than 15 significant digits. This library
33
+ likely uses a different representation internally (e.g., strings or arrays of
34
+ digits) to store and manipulate numbers, effectively bypassing the limitations
35
+ of the built-in `Number` type. This allows it to perform calculations on numbers
36
+ of virtually unlimited size and maintain accuracy.
19
37
 
20
38
  ## Use Cases
21
39
 
22
- This library is particularly useful in scenarios where precise calculations with large numbers are essential, such as:
23
-
24
- * **Financial applications:** Dealing with large sums of money or precise interest calculations.
25
- * **Scientific computing:** Working with very large or small numbers in scientific simulations.
26
- * **Cryptography:** Implementing cryptographic algorithms that require high precision.
27
- * **Any application** where exceeding JavaScript's number limits is a concern.
40
+ This library is particularly useful in scenarios where precise calculations with
41
+ large numbers are essential, such as:
28
42
 
43
+ - **Financial applications:** Dealing with large sums of money or precise
44
+ interest calculations.
45
+ - **Scientific computing:** Working with very large or small numbers in
46
+ scientific simulations.
47
+ - **Cryptography:** Implementing cryptographic algorithms that require high
48
+ precision.
49
+ - **Any application** where exceeding JavaScript's number limits is a concern.
29
50
 
30
51
  ### Latest update
31
52
 
32
- Added rounding
33
- * round up
34
- * round down
35
- * half-up
36
- * half-down
37
- * half-even
38
- * half-odd
39
- * decimal places to be rounded
40
- * significant figures decimals to be rounded
53
+ Added pipe functionality
41
54
 
42
55
  # Install:
43
56
 
@@ -84,6 +97,7 @@ const negative = add(["0.1", "-0.3", "0.1"]); // -0.1
84
97
  ```
85
98
 
86
99
  ### Subtract numbers
100
+
87
101
  ```javascript
88
102
  import { sub } from "@numio/bigmath";
89
103
 
@@ -93,6 +107,7 @@ const negative = sub(["-0.1", "-0.3", "0.4"]); // -0.2
93
107
  ```
94
108
 
95
109
  ### Multiply numbers
110
+
96
111
  ```javascript
97
112
  import { mul } from "@numio/bigmath";
98
113
 
@@ -102,10 +117,11 @@ const negative = mul(["-2", "3"]); // -6
102
117
  ```
103
118
 
104
119
  ### Divide numbers
120
+
105
121
  ```javascript
106
122
  import { div } from "@numio/bigmath";
107
123
 
108
- const int = div(["9999", "33"]); //
124
+ const int = div(["9999", "33"]); //
109
125
  const float = div(["0.06", "0.2"]); // 0.3
110
126
  const negative = div(["-2", "-3", "2"]); // 3
111
127
 
@@ -114,7 +130,10 @@ div("10", "3"); // 3.33333
114
130
  ```
115
131
 
116
132
  ### Round
133
+
117
134
  ```javascript
135
+ import { round } from "@numio/bigmath";
136
+
118
137
  round("-1.12345"); // -1
119
138
  round("1.5"); // 2
120
139
  round("1.0"); // 1
@@ -123,7 +142,10 @@ round("9.9"); // 10
123
142
  ```
124
143
 
125
144
  ### Round at position
145
+
126
146
  ```javascript
147
+ import { round } from "@numio/bigmath";
148
+
127
149
  round("1.12345", { decimals: 1 }); // 1.1
128
150
  round("1.12345", { decimals: 2 }); // 1.12
129
151
  round("1.12234", { decimals: 0 }); // 1
@@ -131,7 +153,10 @@ round("9.999", { decimals: 2 }); // 10
131
153
  ```
132
154
 
133
155
  ### Round modes
156
+
134
157
  ```javascript
158
+ import { round } from "@numio/bigmath";
159
+
135
160
  round("1.11", { decimals: 1, roundMode: "up" }); // 1.2
136
161
  round("1.19", { decimals: 1, roundMode: "up" }); // 1.2
137
162
 
@@ -155,7 +180,10 @@ round("1.55", { decimals: 1, roundMode: "half-odd" }); // 1.5
155
180
  ```
156
181
 
157
182
  ### Round with "significant figures" flag
183
+
158
184
  ```javascript
185
+ import { round } from "@numio/bigmath";
186
+
159
187
  round("0.000119", { decimals: 2, sigFig: false }); // 0
160
188
  round("0.000119", { decimals: 2, sigFig: true }); // 0.00012
161
189
 
@@ -166,6 +194,23 @@ round("1.000119", { decimals: 2, sigFig: false }); // 1
166
194
  round("1.000119", { decimals: 2, sigFig: true }); // 1
167
195
  ```
168
196
 
197
+ ### Pipe
198
+
199
+ ```javascript
200
+ import { pipe } from "@numio/bigmath";
201
+
202
+ const addNums = ["1", "2", "3"];
203
+ const subNums = ["0.2", "0.3"];
204
+ const divNums = ["4"];
205
+ const mulNums = ["2", "5", "0.2"];
206
+
207
+ pipe.add(addNums) // 6
208
+ .div(divNums) // 6 / 4 = 1.5
209
+ .sub(subNums) // 1.5 - 0.2 - 0.3 = 1
210
+ .mul(mulNums) // 1 * 2 * 5 * 0.2 = 2
211
+ .calc()
212
+ ```
213
+
169
214
  Does not have a limitation on the number of digits. You can use any length you'd
170
215
  like
171
216
 
@@ -182,7 +227,7 @@ const float = mul(
182
227
  ); // 0.00000000000000000000000000000000000000000000000000000000000000000000000000018
183
228
  ```
184
229
 
185
- Download from NPM - https://www.npmjs.com/package/@numio/bigmath \
186
- Download from JSR - https://jsr.io/@numio/bigmath \
187
- Home page - https://github.com/shpunter/numio-bigmath/blob/main/README.md \
188
- License - https://github.com/shpunter/numio-bigmath/blob/main/LICENSE
230
+ Download from NPM - https://www.npmjs.com/package/@numio/bigmath\
231
+ Download from JSR - https://jsr.io/@numio/bigmath\
232
+ Home page - https://github.com/shpunter/numio-bigmath/blob/main/README.md\
233
+ License - https://github.com/shpunter/numio-bigmath/blob/main/LICENSE
package/index.d.ts CHANGED
@@ -3,4 +3,5 @@ import { mul } from "./src/mul/index.d.ts";
3
3
  import { sub } from "./src/sub/index.d.ts";
4
4
  import { div } from "./src/div/index.d.ts";
5
5
  import { round } from "./src/round/index.d.ts";
6
- export { add, div, mul, round, sub };
6
+ import { pipe } from "./src/pipe/main.d.ts";
7
+ export { add, div, mul, round, sub, pipe };
package/index.js CHANGED
@@ -3,4 +3,5 @@ import { mul } from "./src/mul/index.js";
3
3
  import { sub } from "./src/sub/index.js";
4
4
  import { div } from "./src/div/index.js";
5
5
  import { round } from "./src/round/index.js";
6
- export { add, div, mul, round, sub };
6
+ import { pipe } from "./src/pipe/main.js";
7
+ export { add, div, mul, round, sub, pipe };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@numio/bigmath",
3
3
  "description": "@numio/bigmath is an arbitrary-precision arithmetic library. It can be used for basic operations with decimal numbers (integers and float)",
4
- "version": "1.0.3",
4
+ "version": "1.0.4",
5
5
  "keywords": [
6
6
  "precision",
7
7
  "arithmetic",
package/src/add/index.js CHANGED
@@ -1,19 +1,7 @@
1
- import { a2sSA, s2aSA } from "../shared/utils.js";
2
- import { subtract } from "../sub/utils.js";
3
- import { addition } from "./utils.js";
1
+ import { DEFAULT } from "../shared/constant.js";
2
+ import { a2s, s2a } from "../shared/utils.js";
3
+ import { addRoute } from "./utils.js";
4
4
  /** This function adds numbers (as string). */
5
5
  export function add(strs) {
6
- var arrays = strs.map(function (str) { return s2aSA(str); });
7
- var inputData = arrays.reduce(function (left, right) {
8
- if (left.array.length === 0)
9
- return right;
10
- if (left.isNegative && !right.isNegative) {
11
- return subtract([right.array, right.intLength], [left.array, left.intLength]);
12
- }
13
- if (!left.isNegative && right.isNegative) {
14
- return subtract([left.array, left.intLength], [right.array, right.intLength]);
15
- }
16
- return addition([left.array, left.intLength], [right.array, right.intLength], left.isNegative && right.isNegative);
17
- }, { array: [], intLength: 0, isFloat: false, isNegative: false });
18
- return a2sSA(inputData.array, inputData.isFloat, inputData.isNegative);
6
+ return a2s(addRoute(strs.map(function (str) { return s2a(str); }), DEFAULT));
19
7
  }
@@ -1,3 +1,5 @@
1
+ import type { InputData } from "../types.d.ts";
1
2
  import type { Addition } from "./types.d.ts";
2
3
  /** This function adds 2 numbers (as array). */
3
4
  export declare const addition: Addition;
5
+ export declare const addRoute: (input: InputData[], initValue: InputData) => InputData;
package/src/add/utils.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { subtract } from "../sub/utils.js";
1
2
  /** This function adds 2 numbers (as array). */
2
3
  export var addition = function (_a, _b, isNegative) {
3
4
  var _c, _d;
@@ -12,13 +13,10 @@ export var addition = function (_a, _b, isNegative) {
12
13
  var pl = (intLeft >= intRight ? intLeft : intRight) + fracMaxLen;
13
14
  var pr = (intLeft >= intRight ? intRight : intLeft) + fracMaxLen;
14
15
  var carryOver = 48;
15
- if (fracLenL === 0 && fracLenR > 0)
16
- left.push(46);
16
+ while (pl > left.length - 1) {
17
+ left.push(48);
18
+ }
17
19
  while (pr >= 0) {
18
- if (left[pl] === 46 || right[pr] === 46) {
19
- pr -= 1;
20
- pl -= 1;
21
- }
22
20
  var sum = (((_c = left[pl]) !== null && _c !== void 0 ? _c : 48) + ((_d = right[pr]) !== null && _d !== void 0 ? _d : 48) + carryOver) -
23
21
  3 * 48;
24
22
  if (sum > 9) {
@@ -46,3 +44,16 @@ export var addition = function (_a, _b, isNegative) {
46
44
  isFloat: fracLenL + fracLenR > 0,
47
45
  };
48
46
  };
47
+ export var addRoute = function (input, initValue) {
48
+ return input.reduce(function (left, right) {
49
+ if (left.array.length === 0)
50
+ return right;
51
+ if (left.isNegative && !right.isNegative) {
52
+ return subtract([right.array, right.intLength], [left.array, left.intLength]);
53
+ }
54
+ if (!left.isNegative && right.isNegative) {
55
+ return subtract([left.array, left.intLength], [right.array, right.intLength]);
56
+ }
57
+ return addition([left.array, left.intLength], [right.array, right.intLength], left.isNegative && right.isNegative);
58
+ }, initValue);
59
+ };
package/src/div/index.js CHANGED
@@ -1,15 +1,10 @@
1
- import { a2sMD, s2aMD } from "../shared/utils.js";
2
- import { division } from "./utils.js";
1
+ import { DEFAULT } from "../shared/constant.js";
2
+ import { a2s, s2a } from "../shared/utils.js";
3
+ import { divRoute } from "./utils.js";
3
4
  /** This function should divide numbers (as string). */
4
5
  export var div = function (strs, limit) {
5
6
  if (limit === void 0) { limit = 20; }
6
7
  if (strs[0] === "0")
7
8
  return strs[0];
8
- var arrays = strs.map(function (str) { return s2aMD(str); });
9
- var inputData = arrays.reduce(function (left, right) {
10
- if (left.array.length === 0)
11
- return right;
12
- return division([left.array, left.intLength], [right.array, right.intLength], left.isNegative !== right.isNegative, limit);
13
- }, { array: [], intLength: 0, isNegative: false, isFloat: false });
14
- return a2sMD(inputData.array, inputData.intLength != inputData.array.length, inputData.isNegative, inputData.intLength);
9
+ return a2s(divRoute(strs.map(function (str) { return s2a(str); }), DEFAULT, limit));
15
10
  };
@@ -1,2 +1,4 @@
1
+ import type { InputData } from "../types.d.ts";
1
2
  import type { Division } from "./types.d.ts";
2
3
  export declare const division: Division;
4
+ export declare const divRoute: (input: InputData[], initValue: InputData, limit: number) => InputData;
package/src/div/utils.js CHANGED
@@ -80,11 +80,24 @@ export var division = function (_a, _b, isNegative, initLimit) {
80
80
  intLength -= 1;
81
81
  }
82
82
  }
83
- var isZero = result.length === count;
84
- return {
85
- array: result[0] === 48 ? multipliedResult : result,
86
- isFloat: isZero ? false : isFloat,
87
- isNegative: isZero ? false : isNegative,
88
- intLength: isZero ? 1 : intLength,
89
- };
83
+ return result.length === count
84
+ ? {
85
+ array: [48],
86
+ isFloat: false,
87
+ isNegative: false,
88
+ intLength: 1,
89
+ }
90
+ : {
91
+ array: result[0] === 48 ? multipliedResult : result,
92
+ isFloat: isFloat,
93
+ isNegative: isNegative,
94
+ intLength: intLength,
95
+ };
96
+ };
97
+ export var divRoute = function (input, initValue, limit) {
98
+ return input.reduce(function (left, right) {
99
+ if (left.array.length === 0)
100
+ return right;
101
+ return division([left.array, left.intLength], [right.array, right.intLength], left.isNegative !== right.isNegative, limit);
102
+ }, initValue);
90
103
  };
package/src/mul/index.js CHANGED
@@ -1,12 +1,7 @@
1
- import { a2sMD, s2aMD } from "../shared/utils.js";
2
- import { multiplication } from "./utils.js";
1
+ import { DEFAULT } from "../shared/constant.js";
2
+ import { a2s, s2a } from "../shared/utils.js";
3
+ import { mulRoute } from "./utils.js";
3
4
  /** This function multiplies numbers (as string). */
4
5
  export var mul = function (strs) {
5
- var arrays = strs.map(function (str) { return s2aMD(str); });
6
- var inputData = arrays.reduce(function (left, right) {
7
- if (left.array.length === 0)
8
- return right;
9
- return multiplication([left.array, left.intLength], [right.array, right.intLength], left.isNegative !== right.isNegative);
10
- }, { array: [], intLength: 0, isNegative: false, isFloat: false });
11
- return a2sMD(inputData.array, inputData.intLength != inputData.array.length, inputData.isNegative, inputData.intLength);
6
+ return a2s(mulRoute(strs.map(function (str) { return s2a(str); }), DEFAULT));
12
7
  };
@@ -1,3 +1,5 @@
1
+ import type { InputData } from "../types.d.ts";
1
2
  import type { Multiplication } from "./types.d.ts";
2
3
  /** This function multiplies 2 numbers (as array). */
3
4
  export declare const multiplication: Multiplication;
5
+ export declare const mulRoute: (input: InputData[], initValue: InputData) => InputData;
package/src/mul/utils.js CHANGED
@@ -40,3 +40,10 @@ export var multiplication = function (_a, _b, isNegative) {
40
40
  isNegative: isNegative,
41
41
  };
42
42
  };
43
+ export var mulRoute = function (input, initValue) {
44
+ return input.reduce(function (left, right) {
45
+ if (left.array.length === 0)
46
+ return right;
47
+ return multiplication([left.array, left.intLength], [right.array, right.intLength], left.isNegative !== right.isNegative);
48
+ }, initValue);
49
+ };
@@ -0,0 +1,13 @@
1
+ import type { A2S } from "../shared/types.d.ts";
2
+ import type { InputData } from "../types.d.ts";
3
+ declare class Pipe {
4
+ result: InputData;
5
+ constructor();
6
+ add(strs: string[]): Pipe;
7
+ sub(strs: string[]): Pipe;
8
+ div(strs: string[], limit?: number): Pipe;
9
+ mul(strs: string[]): Pipe;
10
+ calc(): ReturnType<A2S>;
11
+ }
12
+ export declare const pipe: Pipe;
13
+ export {};
@@ -0,0 +1,35 @@
1
+ import { addRoute } from "../add/utils.js";
2
+ import { divRoute } from "../div/utils.js";
3
+ import { mulRoute } from "../mul/utils.js";
4
+ import { DEFAULT } from "../shared/constant.js";
5
+ import { a2s, s2a } from "../shared/utils.js";
6
+ import { subRoute } from "../sub/utils.js";
7
+ var Pipe = /** @class */ (function () {
8
+ function Pipe() {
9
+ this.result = DEFAULT;
10
+ }
11
+ Pipe.prototype.add = function (strs) {
12
+ this.result = addRoute(strs.map(function (str) { return s2a(str); }), this.result);
13
+ return this;
14
+ };
15
+ Pipe.prototype.sub = function (strs) {
16
+ this.result = subRoute(strs.map(function (str) { return s2a(str); }), this.result);
17
+ return this;
18
+ };
19
+ Pipe.prototype.div = function (strs, limit) {
20
+ if (limit === void 0) { limit = 20; }
21
+ this.result = divRoute(strs.map(function (str) { return s2a(str); }), this.result, limit);
22
+ return this;
23
+ };
24
+ Pipe.prototype.mul = function (strs) {
25
+ this.result = mulRoute(strs.map(function (str) { return s2a(str); }), this.result);
26
+ return this;
27
+ };
28
+ Pipe.prototype.calc = function () {
29
+ var result = a2s(this.result);
30
+ this.result = DEFAULT;
31
+ return result;
32
+ };
33
+ return Pipe;
34
+ }());
35
+ export var pipe = new Pipe();
@@ -0,0 +1,6 @@
1
+ export declare const DEFAULT: {
2
+ array: any[];
3
+ intLength: number;
4
+ isFloat: boolean;
5
+ isNegative: boolean;
6
+ };
@@ -0,0 +1,6 @@
1
+ export var DEFAULT = {
2
+ array: [],
3
+ intLength: 0,
4
+ isFloat: false,
5
+ isNegative: false,
6
+ };
@@ -1,4 +1,3 @@
1
1
  import type { InputData } from "../types.d.ts";
2
- export type A2S = (array: number[], isFloat: boolean, isNegative?: boolean, intLength?: number) => string;
3
- export type S2ASA = (string: string) => InputData;
4
- export type S2AMD = (strings: string) => InputData;
2
+ export type A2S = (input: InputData) => string;
3
+ export type S2A = (strings: string) => InputData;
@@ -1,5 +1,3 @@
1
- import type { A2S, S2AMD, S2ASA } from "./types.d.ts";
2
- export declare const a2sMD: A2S;
3
- export declare const a2sSA: A2S;
4
- export declare const s2aSA: S2ASA;
5
- export declare const s2aMD: S2AMD;
1
+ import type { A2S, S2A } from "./types.d.ts";
2
+ export declare const a2s: A2S;
3
+ export declare const s2a: S2A;
@@ -1,7 +1,5 @@
1
- // array to string
2
- export var a2sMD = function (array, isFloat, isNegative, intLength) {
3
- if (isNegative === void 0) { isNegative = false; }
4
- if (intLength === void 0) { intLength = 0; }
1
+ export var a2s = function (_a) {
2
+ var array = _a.array, isFloat = _a.isFloat, isNegative = _a.isNegative, intLength = _a.intLength;
5
3
  var result = [];
6
4
  var lastValuableIdx = array.length;
7
5
  if (isFloat) {
@@ -32,49 +30,7 @@ export var a2sMD = function (array, isFloat, isNegative, intLength) {
32
30
  }
33
31
  return (isNegative ? "-" : "") + String.fromCharCode.apply(String, array).trim();
34
32
  };
35
- export var a2sSA = function (array, isFloat, isNegative) {
36
- if (isNegative === void 0) { isNegative = false; }
37
- var isToCheckTail = isFloat;
38
- for (var i = array.length - 1; i >= 0; i--) {
39
- if (isToCheckTail && array[i] === 46) {
40
- array[i] = 32;
41
- isToCheckTail = false;
42
- break;
43
- }
44
- if (isToCheckTail && array[i] === 48)
45
- array[i] = 32;
46
- else
47
- break;
48
- }
49
- for (var i = 0; i < array.length; i++) {
50
- if (array[i + 1] === 46 || array.length <= 1)
51
- break;
52
- if (array[i] === 48)
53
- array[i] = 32;
54
- else
55
- break;
56
- }
57
- return (isNegative ? "-" : "") + String.fromCharCode.apply(String, array).trim();
58
- };
59
- // string to array (sub, add)
60
- export var s2aSA = function (string) {
61
- var isNegative = string.charCodeAt(0) === 45;
62
- var shift = isNegative ? 1 : 0;
63
- var array = Array(string.length - shift);
64
- var intLength = string.length - shift;
65
- var isFloat = false;
66
- for (var idx = 0 + shift; idx < string.length; idx++) {
67
- var charCode = string.charCodeAt(idx);
68
- if (charCode === 46) {
69
- intLength = idx - shift;
70
- isFloat || (isFloat = true);
71
- }
72
- array[idx - shift] = charCode;
73
- }
74
- return { array: array, intLength: intLength, isNegative: isNegative, isFloat: isFloat };
75
- };
76
- // string to array (mul, div)
77
- export var s2aMD = function (string) {
33
+ export var s2a = function (string) {
78
34
  var array = Array(0);
79
35
  var isNegative = string.charCodeAt(0) === 45;
80
36
  var shift = isNegative ? 1 : 0;
package/src/sub/index.js CHANGED
@@ -1,19 +1,7 @@
1
- import { addition } from "../add/utils.js";
2
- import { a2sSA, s2aSA } from "../shared/utils.js";
3
- import { subtract } from "./utils.js";
1
+ import { DEFAULT } from "../shared/constant.js";
2
+ import { a2s, s2a } from "../shared/utils.js";
3
+ import { subRoute } from "./utils.js";
4
4
  /** This function subtracts numbers (as string). */
5
5
  export function sub(strs) {
6
- var arrays = strs.map(function (str) { return s2aSA(str); });
7
- var inputData = arrays.reduce(function (left, right) {
8
- if (left.array.length === 0)
9
- return right;
10
- if (left.isNegative && right.isNegative) {
11
- return subtract([right.array, right.intLength], [left.array, left.intLength]);
12
- }
13
- if (!left.isNegative && !right.isNegative) {
14
- return subtract([left.array, left.intLength], [right.array, right.intLength]);
15
- }
16
- return addition([left.array, left.intLength], [right.array, right.intLength], left.isNegative);
17
- }, { array: [], intLength: 0, isNegative: false, isFloat: false });
18
- return a2sSA(inputData.array, inputData.isFloat, inputData.isNegative);
6
+ return a2s(subRoute(strs.map(function (str) { return s2a(str); }), DEFAULT));
19
7
  }
@@ -1,3 +1,5 @@
1
+ import type { InputData } from "../types.d.ts";
1
2
  import type { Subtract } from "./types.d.ts";
2
3
  /** This function subtracts 2 numbers (as array). */
3
4
  export declare const subtract: Subtract;
5
+ export declare const subRoute: (input: InputData[], initValue: InputData) => InputData;
package/src/sub/utils.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { addition } from "../add/utils.js";
1
2
  /** This function subtracts 2 numbers (as array). */
2
3
  export var subtract = function (_a, _b) {
3
4
  var _c, _d;
@@ -15,15 +16,10 @@ export var subtract = function (_a, _b) {
15
16
  var isLeftBigger = lenDiff > 0;
16
17
  var carryOver = false;
17
18
  var isNegative = intLeft !== intL;
18
- if (intLeft === left.length && intRight !== right.length)
19
- left.push(46);
20
- if (intRight === right.length && intLeft !== left.length)
21
- right.push(46);
19
+ while (pl > left.length - 1) {
20
+ left.push(48);
21
+ }
22
22
  while (pr < right.length) {
23
- if (left[pl] === 46 || right[pr] === 46) {
24
- pr += 1;
25
- pl += 1;
26
- }
27
23
  var sub = (((_e = left[pl]) !== null && _e !== void 0 ? _e : 48) - ((_f = right[pr]) !== null && _f !== void 0 ? _f : 48)) + 48;
28
24
  if (!isLeftBigger && left[pl] > right[pr]) {
29
25
  isLeftBigger = true;
@@ -42,10 +38,6 @@ export var subtract = function (_a, _b) {
42
38
  var plReverse = pl - 1;
43
39
  var prReverse = pr - 1;
44
40
  while (carryOver) {
45
- if (left[plReverse] === 46 || right[prReverse] === 46) {
46
- plReverse -= 1;
47
- prReverse -= 1;
48
- }
49
41
  if (left[plReverse] !== 48) {
50
42
  plReverse >= 0 && (left[plReverse] -= 1);
51
43
  prReverse >= 0 && (right[prReverse] -= 1);
@@ -63,7 +55,7 @@ export var subtract = function (_a, _b) {
63
55
  pl += 1;
64
56
  pr += 1;
65
57
  }
66
- while (left[0] === 48 && left[1] !== 46 && left.length > 1) {
58
+ while (left[0] === 48 && left.length > 1 && fracLenL < left.length - 1) {
67
59
  left.shift();
68
60
  intLeft -= 1;
69
61
  }
@@ -74,3 +66,16 @@ export var subtract = function (_a, _b) {
74
66
  isFloat: fracLenL + fracLenR > 0,
75
67
  };
76
68
  };
69
+ export var subRoute = function (input, initValue) {
70
+ return input.reduce(function (left, right) {
71
+ if (left.array.length === 0)
72
+ return right;
73
+ if (left.isNegative && right.isNegative) {
74
+ return subtract([right.array, right.intLength], [left.array, left.intLength]);
75
+ }
76
+ if (!left.isNegative && !right.isNegative) {
77
+ return subtract([left.array, left.intLength], [right.array, right.intLength]);
78
+ }
79
+ return addition([left.array, left.intLength], [right.array, right.intLength], left.isNegative);
80
+ }, initValue);
81
+ };