@numio/bigmath 1.0.11 → 2.0.0

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 (67) hide show
  1. package/README.md +30 -3
  2. package/index.d.ts +6 -5
  3. package/index.js +6 -5
  4. package/package.json +9 -2
  5. package/src/IQR/main.js +5 -4
  6. package/src/IQR/types.d.ts +2 -2
  7. package/src/IQR/utils.js +4 -4
  8. package/src/MAD/main.js +5 -4
  9. package/src/MAD/types.d.ts +2 -2
  10. package/src/MAD/utils.js +3 -5
  11. package/src/compare/main.js +12 -10
  12. package/src/compare/types.d.ts +5 -5
  13. package/src/compare/utils.js +43 -60
  14. package/src/mean/main.js +5 -5
  15. package/src/mean/types.d.ts +2 -2
  16. package/src/mean/utils.js +4 -8
  17. package/src/operations/add/main.d.ts +2 -0
  18. package/src/operations/add/main.js +8 -0
  19. package/src/operations/div/main.d.ts +2 -0
  20. package/src/operations/div/main.js +9 -0
  21. package/src/operations/div/types.d.ts +2 -0
  22. package/src/operations/div/utils.d.ts +2 -0
  23. package/src/operations/div/utils.js +39 -0
  24. package/src/operations/mul/main.d.ts +2 -0
  25. package/src/operations/mul/main.js +7 -0
  26. package/src/operations/sub/main.d.ts +2 -0
  27. package/src/operations/sub/main.js +8 -0
  28. package/src/pipe/main.d.ts +8 -9
  29. package/src/pipe/main.js +34 -19
  30. package/src/pipe/utils.d.ts +10 -0
  31. package/src/pipe/utils.js +30 -0
  32. package/src/quartile/main.js +7 -7
  33. package/src/quartile/types.d.ts +6 -6
  34. package/src/quartile/utils.js +3 -10
  35. package/src/shared/constant.d.ts +0 -12
  36. package/src/shared/constant.js +0 -12
  37. package/src/shared/types.d.ts +6 -4
  38. package/src/shared/utils.d.ts +6 -3
  39. package/src/shared/utils.js +65 -59
  40. package/src/sort/main.js +7 -4
  41. package/src/sort/types.d.ts +3 -3
  42. package/src/sqrt/main.d.ts +3 -0
  43. package/src/sqrt/main.js +10 -0
  44. package/src/sqrt/types.d.ts +3 -0
  45. package/src/sqrt/utils.d.ts +2 -0
  46. package/src/sqrt/utils.js +24 -0
  47. package/src/mathOperations/add/main.d.ts +0 -2
  48. package/src/mathOperations/add/main.js +0 -7
  49. package/src/mathOperations/add/types.d.ts +0 -2
  50. package/src/mathOperations/add/utils.d.ts +0 -5
  51. package/src/mathOperations/add/utils.js +0 -59
  52. package/src/mathOperations/div/main.d.ts +0 -2
  53. package/src/mathOperations/div/main.js +0 -10
  54. package/src/mathOperations/div/types.d.ts +0 -3
  55. package/src/mathOperations/div/utils.d.ts +0 -3
  56. package/src/mathOperations/div/utils.js +0 -99
  57. package/src/mathOperations/mul/main.d.ts +0 -2
  58. package/src/mathOperations/mul/main.js +0 -7
  59. package/src/mathOperations/mul/types.d.ts +0 -2
  60. package/src/mathOperations/mul/utils.d.ts +0 -5
  61. package/src/mathOperations/mul/utils.js +0 -50
  62. package/src/mathOperations/sub/main.d.ts +0 -2
  63. package/src/mathOperations/sub/main.js +0 -7
  64. package/src/mathOperations/sub/types.d.ts +0 -2
  65. package/src/mathOperations/sub/utils.d.ts +0 -5
  66. package/src/mathOperations/sub/utils.js +0 -81
  67. package/src/types.d.ts +0 -7
package/README.md CHANGED
@@ -11,12 +11,16 @@
11
11
  * **Handle Numbers of Any Size:** Perform calculations on integers and decimals of virtually unlimited length, without the risk of JavaScript's `Number` limitations.
12
12
  * **Eliminate Precision Loss:** Achieve accurate results even with numeric literals exceeding 15 significant digits, ensuring the integrity of your calculations.
13
13
  * **Precise Decimal Operations:** Execute addition, subtraction, multiplication, and division on decimal numbers with guaranteed accuracy, including scenarios with negative values.
14
+ * ****NEW! Multi-Base Number Support:** Seamlessly perform arithmetic operations involving **hexadecimal (HEX), octal, binary, and decimal numbers**, offering unparalleled flexibility in handling various number formats.
15
+
14
16
 
15
17
  **Unlock Advanced Numerical Operations:**
16
18
 
17
19
  * **Control Division Precision:** Specify the exact number of digits after the decimal point for division results, with a default precision of 20 digits for high accuracy.
18
20
  * **Flexible Rounding:** Round numbers to the nearest integer or a specific number of decimal places with various rounding modes (up, down, half-up, half-down, half-even, half-odd) to meet your exact requirements.
19
21
  * **Round Based on Significant Figures:** Control rounding based on the number of significant figures, crucial for scientific and engineering applications.
22
+ * **Calculate Roots:**
23
+ * **Calculate Square Root (`sqrt`):** Compute the square root of a number with arbitrary precision. You can also specify the desired precision of the result.
20
24
  * **Chain Operations with Pipe:** Simplify complex calculations by chaining arithmetic operations in a readable and intuitive manner.
21
25
  * **Analyze Data Distribution:**
22
26
  * **Calculate Quartiles (Q1, Q2, Q3):** Understand the spread and central tendency of your numerical data, helping identify outliers and the shape of the distribution.
@@ -38,15 +42,15 @@ This library is particularly invaluable in applications where numerical accuracy
38
42
  * **Cryptography:** Implementing cryptographic algorithms that rely on high-precision arithmetic.
39
43
  * **E-commerce and Payments:** Handling precise amounts and avoiding rounding errors in transactions.
40
44
  * **Data Analysis and Statistics:** Performing accurate statistical calculations on datasets with varying scales.
45
+ * **Low-Level Operations:** Working with different number representations commonly found in computer systems.
41
46
  * **Any Scenario Exceeding JavaScript's Number Limits:** Ensuring the reliability of your calculations when dealing with numbers beyond the safe integer limit or requiring more than 15 significant digits.
42
47
 
43
48
  With `@numio/bigmath`, you can confidently perform complex arithmetic operations with the assurance of accuracy, regardless of the size or precision of the numbers involved.
44
49
 
45
50
  ### Latest update
46
51
 
47
- Added `isEqual` and `isLeftGreater` - to compare 2 numbers.\
48
- Added `MAD` - Median Absolute Deviation.\
49
- Added `IQR` - Interquartile Range.
52
+ The performance was improved. Adding, subtracting, dividing, and multiplying are now **2** to **5** times faster than before.\
53
+ Adding, subtracting, dividing, and multiplying support operations on HEX, octal, binary, and decimal numbers. Mixed-type calculations are allowed, with the final result converted to decimal.
50
54
 
51
55
  # Install:
52
56
 
@@ -125,6 +129,17 @@ const negative = div(["-2", "-3", "2"]); //0.33333333333333333333
125
129
  div(["10", "3"], 4); // 3.3333
126
130
  ```
127
131
 
132
+ ### HEX, decimal, octal, binary
133
+ ```javascript
134
+ import { add, sub } from "@numio/bigmath";
135
+
136
+ add(["0xA", "0x5"]) // HEX + HEX, 10 + 5 = 15
137
+ add(["0xA", "2"]) // HEX + decimal, 10 + 2 = 12
138
+ sub(["35", "0b11"]) // decimal - binary, 35 - 3 = 32
139
+ sub(["0x1A", "0o31", "0b101", ]) // HEX - octal - binary, 26 - 25 - 5 = -4
140
+
141
+ ```
142
+
128
143
  ### Round
129
144
 
130
145
  ```javascript
@@ -288,6 +303,18 @@ import { IQR } from "@numio/bigmath";
288
303
  IQR(["7", "15", "36", "39", "40", "41"]) // 25;
289
304
  ```
290
305
 
306
+ ### SQRT - square root of a number
307
+ ```javascript
308
+ import { sqrt } from "@numio/bigmath";
309
+
310
+ sqrt("81") // 9;
311
+ sqrt("3") // 1.7320508075689;
312
+
313
+ // you can change precision of a result (second parameter),
314
+ sqrt("3", "0.01") // 1.732;
315
+ sqrt("3", "0.000000000000000000001") // 1.732050807568877293527;
316
+ ```
317
+
291
318
  Does not have a limitation on the number of digits. You can use any length you'd
292
319
  like
293
320
 
package/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { add } from "./src/mathOperations/add/main.d.ts";
2
- import { mul } from "./src/mathOperations/mul/main.d.ts";
3
- import { sub } from "./src/mathOperations/sub/main.d.ts";
4
- import { div } from "./src/mathOperations/div/main.d.ts";
1
+ import { add } from "./src/operations/add/main.d.ts";
2
+ import { mul } from "./src/operations/mul/main.d.ts";
3
+ import { sub } from "./src/operations/sub/main.d.ts";
4
+ import { div } from "./src/operations/div/main.d.ts";
5
5
  import { round } from "./src/round/main.d.ts";
6
6
  import { pipe } from "./src/pipe/main.d.ts";
7
7
  import { quartile } from "./src/quartile/main.d.ts";
@@ -10,4 +10,5 @@ import { mean } from "./src/mean/main.d.ts";
10
10
  import { isEqual, isLeftGreater, max, min } from "./src/compare/main.d.ts";
11
11
  import { IQR } from "./src/IQR/main.d.ts";
12
12
  import { MAD } from "./src/MAD/main.d.ts";
13
- export { add, div, IQR, isEqual, isLeftGreater, MAD, max, mean, min, mul, pipe, quartile, round, sort, sub, };
13
+ import { sqrt } from "./src/sqrt/main.d.ts";
14
+ export { add, div, IQR, isEqual, isLeftGreater, MAD, max, mean, min, mul, pipe, quartile, round, sort, sub, sqrt, };
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { add } from "./src/mathOperations/add/main.js";
2
- import { mul } from "./src/mathOperations/mul/main.js";
3
- import { sub } from "./src/mathOperations/sub/main.js";
4
- import { div } from "./src/mathOperations/div/main.js";
1
+ import { add } from "./src/operations/add/main.js";
2
+ import { mul } from "./src/operations/mul/main.js";
3
+ import { sub } from "./src/operations/sub/main.js";
4
+ import { div } from "./src/operations/div/main.js";
5
5
  import { round } from "./src/round/main.js";
6
6
  import { pipe } from "./src/pipe/main.js";
7
7
  import { quartile } from "./src/quartile/main.js";
@@ -10,4 +10,5 @@ import { mean } from "./src/mean/main.js";
10
10
  import { isEqual, isLeftGreater, max, min } from "./src/compare/main.js";
11
11
  import { IQR } from "./src/IQR/main.js";
12
12
  import { MAD } from "./src/MAD/main.js";
13
- export { add, div, IQR, isEqual, isLeftGreater, MAD, max, mean, min, mul, pipe, quartile, round, sort, sub, };
13
+ import { sqrt } from "./src/sqrt/main.js";
14
+ export { add, div, IQR, isEqual, isLeftGreater, MAD, max, mean, min, mul, pipe, quartile, round, sort, sub, sqrt, };
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.11",
4
+ "version": "2.0.0",
5
5
  "keywords": [
6
6
  "precision",
7
7
  "arithmetic",
@@ -44,7 +44,14 @@
44
44
  "IQR",
45
45
  "Interquartile Range",
46
46
  "isEqual",
47
- "equality"
47
+ "equality",
48
+ "sqrt",
49
+ "square root",
50
+ "HEX",
51
+ "octal",
52
+ "binary",
53
+ "decimal",
54
+ "binary"
48
55
  ],
49
56
  "repository": {
50
57
  "type": "git",
package/src/IQR/main.js CHANGED
@@ -1,7 +1,8 @@
1
- import { a2s, s2a } from "../shared/utils.js";
1
+ import { bi2s, s2bi } from "../shared/utils.js";
2
2
  import { IQRInner } from "./utils.js";
3
3
  //** This function returns Interquartile Range */
4
- export var IQR = function (strs, sigNum) {
5
- var array = strs.map(function (str) { return s2a(str); });
6
- return a2s(IQRInner(array, sigNum));
4
+ export var IQR = function (array, sigNum) {
5
+ var arrayInner = array.map(function (str) { return s2bi(str); });
6
+ var _a = IQRInner(arrayInner, sigNum), bi = _a[0], fpe = _a[1];
7
+ return bi2s(bi, fpe);
7
8
  };
@@ -1,3 +1,3 @@
1
- import type { InputData } from "../types.d.ts";
1
+ import type { BI } from "../shared/types.d.ts";
2
2
  export type TIQR = (array: string[], sigNum?: boolean) => string;
3
- export type TIQRInner = (array: InputData[], sigNum?: boolean) => InputData;
3
+ export type TIQRInner = (array: BI[], sigNum?: boolean) => BI;
package/src/IQR/utils.js CHANGED
@@ -1,17 +1,17 @@
1
1
  import { isEqualInner } from "../compare/utils.js";
2
2
  import { MADInner } from "../MAD/utils.js";
3
- import { subRoute } from "../mathOperations/sub/utils.js";
3
+ import { PipeInner } from "../pipe/utils.js";
4
4
  import { quartileInner } from "../quartile/utils.js";
5
- import { DEFAULT, NIL, ONE } from "../shared/constant.js";
6
5
  export var MIN_LENGTH_FOR_MAD = 30;
7
6
  export var IQRInner = function (array, sigNum) {
8
7
  if (sigNum === void 0) { sigNum = false; }
9
8
  var _a = quartileInner(array), Q1 = _a.Q1, Q3 = _a.Q3;
10
- var sub = subRoute([structuredClone(Q3), structuredClone(Q1)], DEFAULT);
9
+ var sub = new PipeInner().sub([Q3, Q1]).calc();
11
10
  if (sigNum) {
12
11
  var isEqual = isEqualInner({ left: Q3, right: Q1 });
13
12
  var MAD = MADInner(array);
14
- var nonNilMAD = isEqualInner({ left: MAD, right: NIL }) ? ONE : MAD;
13
+ var isNil = isEqualInner({ left: MAD, right: [0n, 0] });
14
+ var nonNilMAD = isNil ? [1n, 0] : MAD;
15
15
  return isEqual ? nonNilMAD : sub;
16
16
  }
17
17
  return sub;
package/src/MAD/main.js CHANGED
@@ -1,7 +1,8 @@
1
- import { a2s, s2a } from "../shared/utils.js";
1
+ import { bi2s, s2bi } from "../shared/utils.js";
2
2
  import { MADInner } from "./utils.js";
3
3
  //** This function returns Median Absolute Deviation */
4
- export var MAD = function (strs) {
5
- var array = strs.map(function (str) { return s2a(str); });
6
- return a2s(MADInner(array));
4
+ export var MAD = function (array) {
5
+ var arrayInner = array.map(function (str) { return s2bi(str); });
6
+ var _a = MADInner(arrayInner), bi = _a[0], fpe = _a[1];
7
+ return bi2s(bi, fpe);
7
8
  };
@@ -1,3 +1,3 @@
1
- import type { InputData } from "../types.d.ts";
1
+ import type { BI } from "../shared/types.d.ts";
2
2
  export type TMAD = (array: string[]) => string;
3
- export type TMADInner = (array: InputData[]) => InputData;
3
+ export type TMADInner = (array: BI[]) => BI;
package/src/MAD/utils.js CHANGED
@@ -1,14 +1,12 @@
1
- import { subRoute } from "../mathOperations/sub/utils.js";
1
+ import { PipeInner } from "../pipe/utils.js";
2
2
  import { quartileInner } from "../quartile/utils.js";
3
- import { DEFAULT } from "../shared/constant.js";
4
3
  import { ASC } from "../sort/constants.js";
5
4
  import { sortInner } from "../sort/utils.js";
6
5
  export var MADInner = function (array) {
7
6
  var median = quartileInner(array).Q2;
8
7
  var madArray = array.map(function (el) {
9
- var res = subRoute([structuredClone(el), structuredClone(median)], DEFAULT);
10
- res.isNegative = false;
11
- return res;
8
+ var _a = new PipeInner().sub([el, median]).calc(), bi = _a[0], fpe = _a[1];
9
+ return bi < 0n ? [bi * -1n, fpe] : [bi, fpe];
12
10
  });
13
11
  var sorted = sortInner(madArray, ASC);
14
12
  return quartileInner(sorted).Q2;
@@ -1,22 +1,24 @@
1
- import { a2s, s2a } from "../shared/utils.js";
2
- import { isEqualInner, isLeftGreaterInner, maxInner, minInner } from "./utils.js";
1
+ import { bi2s, s2bi } from "../shared/utils.js";
2
+ import { isEqualInner, isLeftGreaterInner, maxInner, minInner, } from "./utils.js";
3
3
  /** This function returns max value. */
4
- export var max = function (strs) {
5
- var array = strs.map(function (str) { return s2a(str); });
6
- return a2s(maxInner(array));
4
+ export var max = function (array) {
5
+ var arrayInner = array.map(function (str) { return s2bi(str); });
6
+ var _a = maxInner(arrayInner), bi = _a[0], fpe = _a[1];
7
+ return bi2s(bi, fpe);
7
8
  };
8
9
  /** This function returns min value. */
9
- export var min = function (strs) {
10
- var array = strs.map(function (str) { return s2a(str); });
11
- return a2s(minInner(array));
10
+ export var min = function (array) {
11
+ var arrayInner = array.map(function (str) { return s2bi(str); });
12
+ var _a = minInner(arrayInner), bi = _a[0], fpe = _a[1];
13
+ return bi2s(bi, fpe);
12
14
  };
13
15
  /** This function returns if left value is greater than right value */
14
16
  export var isLeftGreater = function (_a) {
15
17
  var left = _a.left, right = _a.right;
16
- return isLeftGreaterInner({ left: s2a(left), right: s2a(right) });
18
+ return isLeftGreaterInner({ left: s2bi(left), right: s2bi(right) });
17
19
  };
18
20
  /** This function returns if left and right values are equal */
19
21
  export var isEqual = function (_a) {
20
22
  var left = _a.left, right = _a.right;
21
- return isEqualInner({ left: s2a(left), right: s2a(right) });
23
+ return isEqualInner({ left: s2bi(left), right: s2bi(right) });
22
24
  };
@@ -1,4 +1,4 @@
1
- import type { InputData } from "../types.d.ts";
1
+ import type { BI } from "../shared/types.d.ts";
2
2
  export type Min = (strs: string[]) => string;
3
3
  export type Max = Min;
4
4
  export type IsLeftGreater = (attr: {
@@ -6,11 +6,11 @@ export type IsLeftGreater = (attr: {
6
6
  right: string;
7
7
  }) => boolean;
8
8
  export type IsEqual = IsLeftGreater;
9
- export type CompareInner = (left: InputData, right: InputData) => [InputData, number];
10
- export type MinInner = (array: InputData[]) => InputData;
9
+ export type CompareInner = (left: BI, right: BI) => [BI, number];
10
+ export type MinInner = (array: BI[]) => BI;
11
11
  export type MaxInner = MinInner;
12
12
  export type IsEqualInner = (attr: {
13
- left: InputData;
14
- right: InputData;
13
+ left: BI;
14
+ right: BI;
15
15
  }) => boolean;
16
16
  export type IsLeftGreaterInner = IsEqualInner;
@@ -15,72 +15,55 @@ export var minInner = function (array) {
15
15
  return min;
16
16
  };
17
17
  export var compareInner = function (l, r) {
18
- var _a;
19
- var idx = 0;
20
- var _b = [l, r], left = _b[0], right = _b[1];
21
- var lIsNeg = l.isNegative, lIntLen = l.intLength;
22
- var rIsNeg = r.isNegative, rIntLen = r.intLength;
23
- var lenL = l.array.length;
24
- var lenR = r.array.length;
25
- var bothNeg = lIsNeg && rIsNeg;
26
- var bothPos = !lIsNeg && !rIsNeg;
27
- var bothIntPos = lIntLen > 0 && rIntLen > 0;
28
- var bothIntNeg = lIntLen <= 0 && rIntLen <= 0;
29
- var isNilL = l.array.length === 1 && l.array[0] === 48;
30
- var isNilR = r.array.length === 1 && r.array[0] === 48;
31
- if (lIsNeg && !rIsNeg)
32
- return [r, -1];
33
- if (!lIsNeg && rIsNeg)
34
- return [l, 1];
35
- if (bothPos && isNilL && !isNilR)
36
- return [r, -1];
37
- if (bothPos && !isNilL && isNilR)
38
- return [l, 1];
39
- if (bothNeg && isNilL && !isNilR)
40
- return [l, 1];
41
- if (bothNeg && !isNilL && isNilR)
42
- return [r, -1];
43
- if (bothPos && lIntLen > 0 && rIntLen === 0)
18
+ var biL = l[0], fpeL = l[1];
19
+ var biR = r[0], fpeR = r[1];
20
+ if (biL > 0n && biR < 0n)
44
21
  return [l, 1];
45
- if (bothPos && lIntLen === 0 && rIntLen > 0)
22
+ if (biL < 0n && biR > 0n)
46
23
  return [r, -1];
47
- if (!bothPos && lIntLen > 0 && rIntLen === 0)
48
- return [r, -1];
49
- if (!bothPos && lIntLen === 0 && rIntLen > 0)
50
- return [l, 1];
51
- if (bothPos && bothIntPos && lIntLen < rIntLen)
52
- return [r, -1];
53
- if (bothPos && bothIntNeg && lIntLen < rIntLen)
54
- return [r, -1];
55
- if (bothNeg && bothIntPos && lIntLen < rIntLen)
56
- return [l, 1];
57
- if (bothNeg && bothIntNeg && lIntLen < rIntLen)
58
- return [l, 1];
59
- if (bothNeg && bothIntNeg && lIntLen > rIntLen)
60
- return [r, -1];
61
- if (bothNeg && bothIntPos && lIntLen > rIntLen)
62
- return [r, -1];
63
- if (bothPos && bothIntPos && lIntLen > rIntLen)
64
- return [l, 1];
65
- if (bothPos && bothIntNeg && lIntLen > rIntLen)
66
- return [l, 1];
67
- if (bothNeg)
68
- _a = [r, l], left = _a[0], right = _a[1];
69
- while (idx < (lenL > lenR ? lenL : lenR)) {
70
- var numL = left.array[idx];
71
- var numR = right.array[idx];
72
- if (!numL && bothPos)
73
- return [r, -1];
74
- if (!numR && bothPos)
24
+ if (biL === 0n || biR === 0n) {
25
+ if (biL > biR)
75
26
  return [l, 1];
76
- if (!numL && bothNeg)
27
+ if (biL < biR)
28
+ return [r, -1];
29
+ }
30
+ if (biL < 0n && biR < 0n) {
31
+ if (biL === biR) {
32
+ if (fpeL > fpeR)
33
+ return [l, 1];
34
+ if (fpeL < fpeR)
35
+ return [r, -1];
36
+ if (fpeL === fpeR)
37
+ return [l, 0];
38
+ }
39
+ var fpeBiL = BigInt(fpeL);
40
+ var fpeBiR = BigInt(fpeR);
41
+ var max = fpeBiL > fpeBiR ? fpeBiL : fpeBiR;
42
+ var powL = biL * Math.pow(10n, (max - fpeBiL));
43
+ var powR = biR * Math.pow(10n, (max - fpeBiR));
44
+ if (powL > powR)
77
45
  return [l, 1];
78
- if (!numR && bothNeg)
46
+ if (powL < powR)
79
47
  return [r, -1];
80
- if (numL !== numR) {
81
- return numL > numR ? [l, 1] : [r, -1];
48
+ }
49
+ if (biL > 0n && biR > 0n) {
50
+ if (biL === biR) {
51
+ if (fpeL > fpeR)
52
+ return [r, -1];
53
+ if (fpeL < fpeR)
54
+ return [l, 1];
55
+ if (fpeL === fpeR)
56
+ return [l, 0];
82
57
  }
83
- idx += 1;
58
+ var fpeBiL = BigInt(fpeL);
59
+ var fpeBiR = BigInt(fpeR);
60
+ var max = fpeBiL > fpeBiR ? fpeBiL : fpeBiR;
61
+ var powL = biL * Math.pow(10n, (max - fpeBiL));
62
+ var powR = biR * Math.pow(10n, (max - fpeBiR));
63
+ if (powL > powR)
64
+ return [l, 1];
65
+ if (powL < powR)
66
+ return [r, -1];
84
67
  }
85
68
  return [l, 0];
86
69
  };
package/src/mean/main.js CHANGED
@@ -1,8 +1,8 @@
1
- import { a2s, s2a } from "../shared/utils.js";
1
+ import { bi2s, s2bi } from "../shared/utils.js";
2
2
  import { meanInner } from "./utils.js";
3
3
  /** This function returns mean of an array. */
4
- export var mean = function (strs) {
5
- var array = strs.map(function (str) { return s2a(str); });
6
- var res = meanInner(array);
7
- return a2s(res);
4
+ export var mean = function (array) {
5
+ var arrayInner = array.map(function (str) { return s2bi(str); });
6
+ var _a = meanInner(arrayInner), bi = _a[0], fpe = _a[1];
7
+ return bi2s(bi, fpe);
8
8
  };
@@ -1,3 +1,3 @@
1
- import type { InputData } from "../types.d.ts";
1
+ import type { BI } from "../shared/types.d.ts";
2
2
  export type Mean = (array: string[]) => string;
3
- export type MeanInner = (array: InputData[]) => InputData;
3
+ export type MeanInner = (array: BI[]) => BI;
package/src/mean/utils.js CHANGED
@@ -1,10 +1,6 @@
1
- import { addRoute } from "../mathOperations/add/utils.js";
2
- import { divInner } from "../mathOperations/div/utils.js";
3
- import { DEFAULT } from "../shared/constant.js";
4
- import { s2a } from "../shared/utils.js";
1
+ import { PipeInner } from "../pipe/utils.js";
5
2
  export var meanInner = function (array) {
6
- var left = addRoute(array, DEFAULT);
7
- var right = s2a(array.length.toString());
8
- var res = divInner([left.array, left.intLength], [right.array, right.intLength], left.isNegative !== right.isNegative, 20);
9
- return res;
3
+ var left = new PipeInner().add(array).calc();
4
+ var right = [BigInt(array.length), 0];
5
+ return new PipeInner().div([left, right]).calc();
10
6
  };
@@ -0,0 +1,2 @@
1
+ /** This function adds numbers (as string). */
2
+ export declare const add: (array: string[]) => string;
@@ -0,0 +1,8 @@
1
+ import { bi2s, s2bi } from "../../shared/utils.js";
2
+ import { calcInner } from "../../shared/utils.js";
3
+ /** This function adds numbers (as string). */
4
+ export var add = function (array) {
5
+ var arrayInner = array.map(function (str) { return s2bi(str); });
6
+ var _a = calcInner(arrayInner, function (a, b) { return a + b; }), bigInt = _a[0], fpe = _a[1];
7
+ return bi2s(bigInt, fpe);
8
+ };
@@ -0,0 +1,2 @@
1
+ /** This function should divide numbers (as string). */
2
+ export declare const div: (array: string[], limit?: number) => string;
@@ -0,0 +1,9 @@
1
+ import { bi2s, s2bi } from "../../shared/utils.js";
2
+ import { divInner } from "./utils.js";
3
+ /** This function should divide numbers (as string). */
4
+ export var div = function (array, limit) {
5
+ if (limit === void 0) { limit = 20; }
6
+ var arrayInner = array.map(function (str) { return s2bi(str); });
7
+ var _a = divInner(arrayInner, limit !== null && limit !== void 0 ? limit : 20), bigInt = _a[0], fpe = _a[1];
8
+ return bi2s(bigInt, fpe);
9
+ };
@@ -0,0 +1,2 @@
1
+ import type { BI } from "../../shared/types.d.ts";
2
+ export type DivInner = (array: BI[], limit: number, def?: BI) => BI;
@@ -0,0 +1,2 @@
1
+ import type { DivInner } from "./types.d.ts";
2
+ export declare const divInner: DivInner;
@@ -0,0 +1,39 @@
1
+ export var divInner = function (array, limit, def) {
2
+ var bigInt = def ? def[0] : array[0][0];
3
+ var fpe = def ? def[1] : array[0][1];
4
+ var isNeg = bigInt < 0n;
5
+ for (var i = def ? 0 : 1; i < array.length; i++) {
6
+ var _a = array[i], bigCurrent = _a[0], dpLen = _a[1];
7
+ var r = 0n;
8
+ if (bigInt === 0n && fpe === 0)
9
+ return [0n, 0];
10
+ if (fpe === dpLen) {
11
+ fpe = 0;
12
+ dpLen = 0;
13
+ }
14
+ if (dpLen > 0 && fpe < dpLen) {
15
+ bigInt *= Math.pow(10n, BigInt(dpLen - fpe));
16
+ fpe = 0;
17
+ }
18
+ if (dpLen > 0 && fpe > dpLen)
19
+ fpe = fpe - dpLen;
20
+ while ((bigInt < 0 ? bigInt * -1n : bigInt) < bigCurrent) {
21
+ if (limit <= fpe)
22
+ return [bigInt / bigCurrent, fpe];
23
+ fpe += 1;
24
+ bigInt *= 10n;
25
+ }
26
+ var q = bigInt / bigCurrent;
27
+ r = bigInt - q * bigCurrent;
28
+ bigInt = q;
29
+ while ((isNeg ? r < 0n : r > 0n) && fpe < limit) {
30
+ var nextBigInt = r * 10n;
31
+ var nextQ = nextBigInt / bigCurrent;
32
+ var nextRemained = nextBigInt - nextQ * bigCurrent;
33
+ bigInt = bigInt * 10n + nextQ;
34
+ r = nextRemained;
35
+ fpe += 1;
36
+ }
37
+ }
38
+ return [bigInt, fpe];
39
+ };
@@ -0,0 +1,2 @@
1
+ /** This function multiplies numbers (as string). */
2
+ export declare const mul: (array: string[]) => string;
@@ -0,0 +1,7 @@
1
+ import { bi2s, calcInner, s2bi } from "../../shared/utils.js";
2
+ /** This function multiplies numbers (as string). */
3
+ export var mul = function (array) {
4
+ var arrayInner = array.map(function (str) { return s2bi(str); });
5
+ var _a = calcInner(arrayInner, function (a, b) { return a * b; }), bigInt = _a[0], fpe = _a[1];
6
+ return bi2s(bigInt, fpe);
7
+ };
@@ -0,0 +1,2 @@
1
+ /** This function subtracts numbers (as string). */
2
+ export declare function sub(array: string[]): string;
@@ -0,0 +1,8 @@
1
+ import { bi2s, s2bi } from "../../shared/utils.js";
2
+ import { calcInner } from "../../shared/utils.js";
3
+ /** This function subtracts numbers (as string). */
4
+ export function sub(array) {
5
+ var arrayInner = array.map(function (str) { return s2bi(str); });
6
+ var _a = calcInner(arrayInner, function (a, b) { return a - b; }), bigInt = _a[0], fpe = _a[1];
7
+ return bi2s(bigInt, fpe);
8
+ }
@@ -1,13 +1,12 @@
1
- import type { A2S } from "../shared/types.d.ts";
2
- import type { InputData } from "../types.d.ts";
1
+ import type { BI2S } from "../shared/types.d.ts";
3
2
  export declare class Pipe {
4
- result: InputData;
5
- constructor(array: InputData);
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>;
3
+ #private;
4
+ constructor();
5
+ add(array: string[]): Pipe;
6
+ sub(array: string[]): Pipe;
7
+ div(array: string[], limit?: number): Pipe;
8
+ mul(array: string[]): Pipe;
9
+ calc(): ReturnType<BI2S>;
11
10
  }
12
11
  /** Using this function you can chain operations (add, sub, div, mul). */
13
12
  export declare const pipe: Pipe;