@numio/bigmath 1.0.3 → 1.0.5

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.
Files changed (41) hide show
  1. package/README.md +87 -30
  2. package/index.d.ts +7 -5
  3. package/index.js +7 -5
  4. package/package.json +7 -2
  5. package/src/mathOperations/add/index.js +7 -0
  6. package/src/{add → mathOperations/add}/types.d.ts +1 -1
  7. package/src/{add → mathOperations/add}/utils.d.ts +2 -0
  8. package/src/{add → mathOperations/add}/utils.js +17 -6
  9. package/src/mathOperations/div/index.js +10 -0
  10. package/src/mathOperations/div/types.d.ts +3 -0
  11. package/src/mathOperations/div/utils.d.ts +3 -0
  12. package/src/{div → mathOperations/div}/utils.js +20 -7
  13. package/src/mathOperations/mul/index.js +7 -0
  14. package/src/{mul → mathOperations/mul}/types.d.ts +1 -1
  15. package/src/{mul → mathOperations/mul}/utils.d.ts +2 -0
  16. package/src/{mul → mathOperations/mul}/utils.js +7 -0
  17. package/src/mathOperations/sub/index.js +7 -0
  18. package/src/{sub → mathOperations/sub}/types.d.ts +1 -1
  19. package/src/{sub → mathOperations/sub}/utils.d.ts +2 -0
  20. package/src/{sub → mathOperations/sub}/utils.js +18 -13
  21. package/src/pipe/main.d.ts +13 -0
  22. package/src/pipe/main.js +35 -0
  23. package/src/quartile/main.d.ts +2 -0
  24. package/src/quartile/main.js +19 -0
  25. package/src/quartile/types.d.ts +6 -0
  26. package/src/shared/constant.d.ts +6 -0
  27. package/src/shared/constant.js +6 -0
  28. package/src/shared/types.d.ts +2 -3
  29. package/src/shared/utils.d.ts +3 -5
  30. package/src/shared/utils.js +3 -47
  31. package/src/types.d.ts +1 -0
  32. package/src/add/index.js +0 -19
  33. package/src/div/index.js +0 -15
  34. package/src/div/types.d.ts +0 -2
  35. package/src/div/utils.d.ts +0 -2
  36. package/src/mul/index.js +0 -12
  37. package/src/sub/index.js +0 -19
  38. /package/src/{add → mathOperations/add}/index.d.ts +0 -0
  39. /package/src/{div → mathOperations/div}/index.d.ts +0 -0
  40. /package/src/{mul → mathOperations/mul}/index.d.ts +0 -0
  41. /package/src/{sub → mathOperations/sub}/index.d.ts +0 -0
package/README.md CHANGED
@@ -1,43 +1,58 @@
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 quartile functionality. When you might need it?
54
+ Understanding Distribution. Quartiles (Q1, Q2, and Q3) split a dataset into four groups, each representing 25% of the data.
55
+ Identifying Outliers. They help visualize the shape and spread of the data, revealing whether it's skewed or symmetrical.
41
56
 
42
57
  # Install:
43
58
 
@@ -84,6 +99,7 @@ const negative = add(["0.1", "-0.3", "0.1"]); // -0.1
84
99
  ```
85
100
 
86
101
  ### Subtract numbers
102
+
87
103
  ```javascript
88
104
  import { sub } from "@numio/bigmath";
89
105
 
@@ -93,6 +109,7 @@ const negative = sub(["-0.1", "-0.3", "0.4"]); // -0.2
93
109
  ```
94
110
 
95
111
  ### Multiply numbers
112
+
96
113
  ```javascript
97
114
  import { mul } from "@numio/bigmath";
98
115
 
@@ -102,10 +119,11 @@ const negative = mul(["-2", "3"]); // -6
102
119
  ```
103
120
 
104
121
  ### Divide numbers
122
+
105
123
  ```javascript
106
124
  import { div } from "@numio/bigmath";
107
125
 
108
- const int = div(["9999", "33"]); //
126
+ const int = div(["9999", "33"]); //
109
127
  const float = div(["0.06", "0.2"]); // 0.3
110
128
  const negative = div(["-2", "-3", "2"]); // 3
111
129
 
@@ -114,7 +132,10 @@ div("10", "3"); // 3.33333
114
132
  ```
115
133
 
116
134
  ### Round
135
+
117
136
  ```javascript
137
+ import { round } from "@numio/bigmath";
138
+
118
139
  round("-1.12345"); // -1
119
140
  round("1.5"); // 2
120
141
  round("1.0"); // 1
@@ -123,7 +144,10 @@ round("9.9"); // 10
123
144
  ```
124
145
 
125
146
  ### Round at position
147
+
126
148
  ```javascript
149
+ import { round } from "@numio/bigmath";
150
+
127
151
  round("1.12345", { decimals: 1 }); // 1.1
128
152
  round("1.12345", { decimals: 2 }); // 1.12
129
153
  round("1.12234", { decimals: 0 }); // 1
@@ -131,7 +155,10 @@ round("9.999", { decimals: 2 }); // 10
131
155
  ```
132
156
 
133
157
  ### Round modes
158
+
134
159
  ```javascript
160
+ import { round } from "@numio/bigmath";
161
+
135
162
  round("1.11", { decimals: 1, roundMode: "up" }); // 1.2
136
163
  round("1.19", { decimals: 1, roundMode: "up" }); // 1.2
137
164
 
@@ -155,7 +182,10 @@ round("1.55", { decimals: 1, roundMode: "half-odd" }); // 1.5
155
182
  ```
156
183
 
157
184
  ### Round with "significant figures" flag
185
+
158
186
  ```javascript
187
+ import { round } from "@numio/bigmath";
188
+
159
189
  round("0.000119", { decimals: 2, sigFig: false }); // 0
160
190
  round("0.000119", { decimals: 2, sigFig: true }); // 0.00012
161
191
 
@@ -166,6 +196,33 @@ round("1.000119", { decimals: 2, sigFig: false }); // 1
166
196
  round("1.000119", { decimals: 2, sigFig: true }); // 1
167
197
  ```
168
198
 
199
+ ### Pipe
200
+
201
+ ```javascript
202
+ import { pipe } from "@numio/bigmath";
203
+
204
+ const addNums = ["1", "2", "3"];
205
+ const subNums = ["0.2", "0.3"];
206
+ const divNums = ["4"];
207
+ const mulNums = ["2", "5", "0.2"];
208
+
209
+ pipe.add(addNums) // 6
210
+ .div(divNums) // 6 / 4 = 1.5
211
+ .sub(subNums) // 1.5 - 0.2 - 0.3 = 1
212
+ .mul(mulNums) // 1 * 2 * 5 * 0.2 = 2
213
+ .calc()
214
+ ```
215
+
216
+ ### Quartile
217
+ ```javascript
218
+ import { quartile } from "@numio/bigmath";
219
+
220
+ quartile(["1", "2", "3", "4", "5", "6", "7", "8", "9"]) // { Q1: "2.5", Q2: "5", Q3: "7.5" }
221
+ quartile(["0.001", "0.3", "0.4", "1"]) // { Q1: "0.1505", Q2: "0.35", Q3: "0.7" }
222
+
223
+ ```
224
+
225
+
169
226
  Does not have a limitation on the number of digits. You can use any length you'd
170
227
  like
171
228
 
@@ -182,7 +239,7 @@ const float = mul(
182
239
  ); // 0.00000000000000000000000000000000000000000000000000000000000000000000000000018
183
240
  ```
184
241
 
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
242
+ Download from NPM - https://www.npmjs.com/package/@numio/bigmath\
243
+ Download from JSR - https://jsr.io/@numio/bigmath\
244
+ Home page - https://github.com/shpunter/numio-bigmath/blob/main/README.md\
245
+ License - https://github.com/shpunter/numio-bigmath/blob/main/LICENSE
package/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- import { add } from "./src/add/index.d.ts";
2
- import { mul } from "./src/mul/index.d.ts";
3
- import { sub } from "./src/sub/index.d.ts";
4
- import { div } from "./src/div/index.d.ts";
1
+ import { add } from "./src/mathOperations/add/index.d.ts";
2
+ import { mul } from "./src/mathOperations/mul/index.d.ts";
3
+ import { sub } from "./src/mathOperations/sub/index.d.ts";
4
+ import { div } from "./src/mathOperations/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
+ import { quartile } from "./src/quartile/main.d.ts";
8
+ export { add, div, mul, pipe, quartile, round, sub };
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
- import { add } from "./src/add/index.js";
2
- import { mul } from "./src/mul/index.js";
3
- import { sub } from "./src/sub/index.js";
4
- import { div } from "./src/div/index.js";
1
+ import { add } from "./src/mathOperations/add/index.js";
2
+ import { mul } from "./src/mathOperations/mul/index.js";
3
+ import { sub } from "./src/mathOperations/sub/index.js";
4
+ import { div } from "./src/mathOperations/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
+ import { quartile } from "./src/quartile/main.js";
8
+ export { add, div, mul, pipe, quartile, round, sub };
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.5",
5
5
  "keywords": [
6
6
  "precision",
7
7
  "arithmetic",
@@ -28,7 +28,12 @@
28
28
  "divide",
29
29
  "long division",
30
30
  "bigmath",
31
- "numio"
31
+ "numio",
32
+ "quartile",
33
+ "Q1",
34
+ "Q2",
35
+ "Q3",
36
+ "round"
32
37
  ],
33
38
  "repository": {
34
39
  "type": "git",
@@ -0,0 +1,7 @@
1
+ import { DEFAULT } from "../../shared/constant.js";
2
+ import { a2s, s2a } from "../../shared/utils.js";
3
+ import { addRoute } from "./utils.js";
4
+ /** This function adds numbers (as string). */
5
+ export function add(strs) {
6
+ return a2s(addRoute(strs.map(function (str) { return s2a(str); }), DEFAULT));
7
+ }
@@ -1,2 +1,2 @@
1
- import type { InputData } from "../types.d.ts";
1
+ import type { InputData } from "../../types.d.ts";
2
2
  export type Addition = (L: [number[], number], R: [number[], number], isNegative: boolean) => InputData;
@@ -1,3 +1,5 @@
1
+ import type { Route } 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: Route;
@@ -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
+ };
@@ -0,0 +1,10 @@
1
+ import { DEFAULT } from "../../shared/constant.js";
2
+ import { a2s, s2a } from "../../shared/utils.js";
3
+ import { divRoute } from "./utils.js";
4
+ /** This function should divide numbers (as string). */
5
+ export var div = function (strs, limit) {
6
+ if (limit === void 0) { limit = 20; }
7
+ if (strs[0] === "0")
8
+ return strs[0];
9
+ return a2s(divRoute(strs.map(function (str) { return s2a(str); }), DEFAULT, limit));
10
+ };
@@ -0,0 +1,3 @@
1
+ import type { InputData } from "../../types.d.ts";
2
+ export type Division = (L: [number[], number], R: [number[], number], isNegative: boolean, initLimit: number) => InputData;
3
+ export type DivRoute = (input: InputData[], initValue: InputData, limit: number) => InputData;
@@ -0,0 +1,3 @@
1
+ import type { Division, DivRoute } from "./types.d.ts";
2
+ export declare const division: Division;
3
+ export declare const divRoute: DivRoute;
@@ -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
  };
@@ -0,0 +1,7 @@
1
+ import { DEFAULT } from "../../shared/constant.js";
2
+ import { a2s, s2a } from "../../shared/utils.js";
3
+ import { mulRoute } from "./utils.js";
4
+ /** This function multiplies numbers (as string). */
5
+ export var mul = function (strs) {
6
+ return a2s(mulRoute(strs.map(function (str) { return s2a(str); }), DEFAULT));
7
+ };
@@ -1,2 +1,2 @@
1
- import type { InputData } from "../types.d.ts";
1
+ import type { InputData } from "../../types.d.ts";
2
2
  export type Multiplication = (arrL: [number[], number], arrR: [number[], number], isNegative: boolean) => InputData;
@@ -1,3 +1,5 @@
1
+ import type { Route } 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: Route;
@@ -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,7 @@
1
+ import { DEFAULT } from "../../shared/constant.js";
2
+ import { a2s, s2a } from "../../shared/utils.js";
3
+ import { subRoute } from "./utils.js";
4
+ /** This function subtracts numbers (as string). */
5
+ export function sub(strs) {
6
+ return a2s(subRoute(strs.map(function (str) { return s2a(str); }), DEFAULT));
7
+ }
@@ -1,2 +1,2 @@
1
- import type { InputData } from "../types.d.ts";
1
+ import type { InputData } from "../../types.d.ts";
2
2
  export type Subtract = (L: [number[], number], R: [number[], number]) => InputData;
@@ -1,3 +1,5 @@
1
+ import type { Route } 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: Route;
@@ -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
+ };
@@ -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 { DEFAULT } from "../shared/constant.js";
2
+ import { a2s, s2a } from "../shared/utils.js";
3
+ import { subRoute } from "../mathOperations/sub/utils.js";
4
+ import { addRoute } from "../mathOperations/add/utils.js";
5
+ import { divRoute } from "../mathOperations/div/utils.js";
6
+ import { mulRoute } from "../mathOperations/mul/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,2 @@
1
+ import type { Quartile } from "./types.d.ts";
2
+ export declare const quartile: Quartile;
@@ -0,0 +1,19 @@
1
+ import { pipe } from "../pipe/main.js";
2
+ var mean = function (idx, array) {
3
+ return pipe.add([array[idx], array[idx - 1]]).div(["2"]).calc();
4
+ };
5
+ export var quartile = function (array) {
6
+ if (array.length < 3) {
7
+ throw Error("To calculate quartile you need at least 3 elements");
8
+ }
9
+ var length = array.length;
10
+ var Q2Idx = length >> 1;
11
+ var Q1Idx = Q2Idx >> 1;
12
+ var Q3Idx = length - Q1Idx;
13
+ var isEvenHalf = Q2Idx % 2 !== 0;
14
+ return {
15
+ Q1: isEvenHalf ? array[Q1Idx] : mean(Q1Idx, array),
16
+ Q2: length % 2 !== 0 ? array[Q2Idx] : mean(Q2Idx, array),
17
+ Q3: isEvenHalf ? array[Q3Idx - 1] : mean(Q3Idx, array),
18
+ };
19
+ };
@@ -0,0 +1,6 @@
1
+ export type Quartile = (array: string[]) => {
2
+ Q1: string;
3
+ Q2: string;
4
+ Q3: string;
5
+ };
6
+ export type Mean = (index: number, array: string[]) => string;
@@ -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/types.d.ts CHANGED
@@ -4,3 +4,4 @@ export type InputData = {
4
4
  isNegative: boolean;
5
5
  isFloat: boolean;
6
6
  };
7
+ export type Route = (input: InputData[], initValue: InputData) => InputData;
package/src/add/index.js DELETED
@@ -1,19 +0,0 @@
1
- import { a2sSA, s2aSA } from "../shared/utils.js";
2
- import { subtract } from "../sub/utils.js";
3
- import { addition } from "./utils.js";
4
- /** This function adds numbers (as string). */
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);
19
- }
package/src/div/index.js DELETED
@@ -1,15 +0,0 @@
1
- import { a2sMD, s2aMD } from "../shared/utils.js";
2
- import { division } from "./utils.js";
3
- /** This function should divide numbers (as string). */
4
- export var div = function (strs, limit) {
5
- if (limit === void 0) { limit = 20; }
6
- if (strs[0] === "0")
7
- 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);
15
- };
@@ -1,2 +0,0 @@
1
- import type { InputData } from "../types.d.ts";
2
- export type Division = (L: [number[], number], R: [number[], number], isNegative: boolean, initLimit: number) => InputData;
@@ -1,2 +0,0 @@
1
- import type { Division } from "./types.d.ts";
2
- export declare const division: Division;
package/src/mul/index.js DELETED
@@ -1,12 +0,0 @@
1
- import { a2sMD, s2aMD } from "../shared/utils.js";
2
- import { multiplication } from "./utils.js";
3
- /** This function multiplies numbers (as string). */
4
- 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);
12
- };
package/src/sub/index.js DELETED
@@ -1,19 +0,0 @@
1
- import { addition } from "../add/utils.js";
2
- import { a2sSA, s2aSA } from "../shared/utils.js";
3
- import { subtract } from "./utils.js";
4
- /** This function subtracts numbers (as string). */
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);
19
- }
File without changes
File without changes
File without changes
File without changes