@numio/bigmath 1.1.0 → 2.0.1

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 (57) hide show
  1. package/README.md +16 -4
  2. package/package.json +7 -2
  3. package/src/IQR/main.js +5 -4
  4. package/src/IQR/types.d.ts +2 -2
  5. package/src/IQR/utils.js +8 -9
  6. package/src/MAD/main.js +5 -4
  7. package/src/MAD/types.d.ts +2 -2
  8. package/src/MAD/utils.js +6 -8
  9. package/src/compare/main.js +14 -14
  10. package/src/compare/types.d.ts +5 -5
  11. package/src/compare/utils.js +52 -75
  12. package/src/mean/main.js +5 -5
  13. package/src/mean/types.d.ts +2 -2
  14. package/src/mean/utils.js +4 -7
  15. package/src/operations/add/main.d.ts +1 -1
  16. package/src/operations/add/main.js +7 -6
  17. package/src/operations/div/main.d.ts +1 -1
  18. package/src/operations/div/main.js +6 -8
  19. package/src/operations/div/types.d.ts +2 -3
  20. package/src/operations/div/utils.d.ts +1 -2
  21. package/src/operations/div/utils.js +33 -93
  22. package/src/operations/mul/main.d.ts +1 -1
  23. package/src/operations/mul/main.js +5 -5
  24. package/src/operations/sub/main.d.ts +1 -1
  25. package/src/operations/sub/main.js +6 -5
  26. package/src/pipe/main.d.ts +6 -6
  27. package/src/pipe/main.js +33 -35
  28. package/src/pipe/utils.d.ts +7 -8
  29. package/src/pipe/utils.js +22 -30
  30. package/src/quartile/main.js +7 -7
  31. package/src/quartile/types.d.ts +6 -6
  32. package/src/quartile/utils.js +9 -17
  33. package/src/round/constants.js +7 -7
  34. package/src/round/main.js +16 -17
  35. package/src/round/utils.js +24 -25
  36. package/src/shared/constant.d.ts +0 -12
  37. package/src/shared/constant.js +1 -13
  38. package/src/shared/types.d.ts +6 -5
  39. package/src/shared/utils.d.ts +6 -4
  40. package/src/shared/utils.js +64 -71
  41. package/src/sort/constants.js +3 -3
  42. package/src/sort/main.js +5 -6
  43. package/src/sort/types.d.ts +3 -3
  44. package/src/sort/utils.js +10 -12
  45. package/src/sqrt/main.js +6 -6
  46. package/src/sqrt/types.d.ts +2 -2
  47. package/src/sqrt/utils.js +16 -28
  48. package/src/operations/add/types.d.ts +0 -2
  49. package/src/operations/add/utils.d.ts +0 -5
  50. package/src/operations/add/utils.js +0 -59
  51. package/src/operations/mul/types.d.ts +0 -2
  52. package/src/operations/mul/utils.d.ts +0 -5
  53. package/src/operations/mul/utils.js +0 -50
  54. package/src/operations/sub/types.d.ts +0 -2
  55. package/src/operations/sub/utils.d.ts +0 -5
  56. package/src/operations/sub/utils.js +0 -81
  57. package/src/types.d.ts +0 -7
package/README.md CHANGED
@@ -11,6 +11,8 @@
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
 
@@ -40,16 +42,15 @@ This library is particularly invaluable in applications where numerical accuracy
40
42
  * **Cryptography:** Implementing cryptographic algorithms that rely on high-precision arithmetic.
41
43
  * **E-commerce and Payments:** Handling precise amounts and avoiding rounding errors in transactions.
42
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.
43
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.
44
47
 
45
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.
46
49
 
47
50
  ### Latest update
48
51
 
49
- Added `sqrt` - square root of a number\
50
- Added `isEqual` and `isLeftGreater` - to compare 2 numbers.\
51
- Added `MAD` - Median Absolute Deviation.\
52
- 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.
53
54
 
54
55
  # Install:
55
56
 
@@ -128,6 +129,17 @@ const negative = div(["-2", "-3", "2"]); //0.33333333333333333333
128
129
  div(["10", "3"], 4); // 3.3333
129
130
  ```
130
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
+
131
143
  ### Round
132
144
 
133
145
  ```javascript
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.1.0",
4
+ "version": "2.0.1",
5
5
  "keywords": [
6
6
  "precision",
7
7
  "arithmetic",
@@ -46,7 +46,12 @@
46
46
  "isEqual",
47
47
  "equality",
48
48
  "sqrt",
49
- "square root"
49
+ "square root",
50
+ "HEX",
51
+ "octal",
52
+ "binary",
53
+ "decimal",
54
+ "binary"
50
55
  ],
51
56
  "repository": {
52
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 const IQR = (array, sigNum) => {
5
+ const arrayInner = array.map((str) => s2bi(str));
6
+ const [bi, fpe] = IQRInner(arrayInner, sigNum);
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
@@ -2,16 +2,15 @@ import { isEqualInner } from "../compare/utils.js";
2
2
  import { MADInner } from "../MAD/utils.js";
3
3
  import { PipeInner } from "../pipe/utils.js";
4
4
  import { quartileInner } from "../quartile/utils.js";
5
- import { NIL, ONE } from "../shared/constant.js";
6
- export var MIN_LENGTH_FOR_MAD = 30;
7
- export var IQRInner = function (array, sigNum) {
8
- if (sigNum === void 0) { sigNum = false; }
9
- var _a = quartileInner(array), Q1 = _a.Q1, Q3 = _a.Q3;
10
- var sub = new PipeInner().subInner([Q3, Q1]).result;
5
+ export const MIN_LENGTH_FOR_MAD = 30;
6
+ export const IQRInner = (array, sigNum = false) => {
7
+ const { Q1, Q3 } = quartileInner(array);
8
+ const sub = new PipeInner().sub([Q3, Q1]).calc();
11
9
  if (sigNum) {
12
- var isEqual = isEqualInner({ left: Q3, right: Q1 });
13
- var MAD = MADInner(array);
14
- var nonNilMAD = isEqualInner({ left: MAD, right: NIL }) ? ONE : MAD;
10
+ const isEqual = isEqualInner({ left: Q3, right: Q1 });
11
+ const MAD = MADInner(array);
12
+ const isNil = isEqualInner({ left: MAD, right: [0n, 0] });
13
+ const nonNilMAD = isNil ? [1n, 0] : MAD;
15
14
  return isEqual ? nonNilMAD : sub;
16
15
  }
17
16
  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 const MAD = (array) => {
5
+ const arrayInner = array.map((str) => s2bi(str));
6
+ const [bi, fpe] = MADInner(arrayInner);
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,15 +1,13 @@
1
1
  import { PipeInner } from "../pipe/utils.js";
2
2
  import { quartileInner } from "../quartile/utils.js";
3
- import { cloneInner } from "../shared/utils.js";
4
3
  import { ASC } from "../sort/constants.js";
5
4
  import { sortInner } from "../sort/utils.js";
6
- export var MADInner = function (array) {
7
- var median = quartileInner(array).Q2;
8
- var madArray = array.map(function (el) {
9
- var result = new PipeInner().subInner([el, cloneInner(median)]).result;
10
- result.isNegative = false;
11
- return result;
5
+ export const MADInner = (array) => {
6
+ const median = quartileInner(array).Q2;
7
+ const madArray = array.map((el) => {
8
+ const [bi, fpe] = new PipeInner().sub([el, median]).calc();
9
+ return bi < 0n ? [bi * -1n, fpe] : [bi, fpe];
12
10
  });
13
- var sorted = sortInner(madArray, ASC);
11
+ const sorted = sortInner(madArray, ASC);
14
12
  return quartileInner(sorted).Q2;
15
13
  };
@@ -1,22 +1,22 @@
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 const max = (array) => {
5
+ const arrayInner = array.map((str) => s2bi(str));
6
+ const [bi, fpe] = maxInner(arrayInner);
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 const min = (array) => {
11
+ const arrayInner = array.map((str) => s2bi(str));
12
+ const [bi, fpe] = minInner(arrayInner);
13
+ return bi2s(bi, fpe);
12
14
  };
13
15
  /** This function returns if left value is greater than right value */
14
- export var isLeftGreater = function (_a) {
15
- var left = _a.left, right = _a.right;
16
- return isLeftGreaterInner({ left: s2a(left), right: s2a(right) });
16
+ export const isLeftGreater = ({ left, right }) => {
17
+ return isLeftGreaterInner({ left: s2bi(left), right: s2bi(right) });
17
18
  };
18
19
  /** This function returns if left and right values are equal */
19
- export var isEqual = function (_a) {
20
- var left = _a.left, right = _a.right;
21
- return isEqualInner({ left: s2a(left), right: s2a(right) });
20
+ export const isEqual = ({ left, right }) => {
21
+ return isEqualInner({ left: s2bi(left), right: s2bi(right) });
22
22
  };
@@ -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;
@@ -1,98 +1,75 @@
1
- export var maxInner = function (array) {
2
- var max = array[0];
3
- for (var i = 1; i < array.length; i++) {
1
+ export const maxInner = (array) => {
2
+ let max = array[0];
3
+ for (let i = 1; i < array.length; i++) {
4
4
  if (compareInner(array[i], max)[1] === 1)
5
5
  max = array[i];
6
6
  }
7
7
  return max;
8
8
  };
9
- export var minInner = function (array) {
10
- var min = array[0];
11
- for (var i = 1; i < array.length; i++) {
9
+ export const minInner = (array) => {
10
+ let min = array[0];
11
+ for (let i = 1; i < array.length; i++) {
12
12
  if (compareInner(array[i], min)[1] === -1)
13
13
  min = array[i];
14
14
  }
15
15
  return min;
16
16
  };
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)
17
+ export const compareInner = (l, r) => {
18
+ const [biL, fpeL] = l;
19
+ const [biR, fpeR] = r;
20
+ if (biL > 0n && biR < 0n)
32
21
  return [l, 1];
33
- if (lIsNeg && !rIsNeg)
22
+ if (biL < 0n && biR > 0n)
34
23
  return [r, -1];
35
- if (bothPos && !isNilL && isNilR)
36
- return [l, 1];
37
- if (bothPos && isNilL && !isNilR)
38
- return [r, -1];
39
- if (bothNeg && isNilL && !isNilR)
40
- return [l, 1];
41
- if (bothNeg && !isNilL && isNilR)
42
- return [r, -1];
43
- if (bothPos && lIntLen > rIntLen)
44
- return [l, 1];
45
- if (bothPos && lIntLen < rIntLen)
46
- return [r, -1];
47
- if (bothPos && lIntLen > 0 && rIntLen === 0)
48
- return [l, 1];
49
- if (bothPos && lIntLen === 0 && rIntLen > 0)
50
- return [r, -1];
51
- if (!bothPos && lIntLen === 0 && rIntLen > 0)
52
- return [l, 1];
53
- if (!bothPos && lIntLen > 0 && rIntLen === 0)
54
- return [r, -1];
55
- if (bothPos && bothIntPos && lIntLen > rIntLen)
56
- return [l, 1];
57
- if (bothPos && bothIntPos && lIntLen < rIntLen)
58
- return [r, -1];
59
- if (bothNeg && bothIntNeg && lIntLen < rIntLen)
60
- return [l, 1];
61
- if (bothNeg && bothIntNeg && lIntLen > rIntLen)
62
- return [r, -1];
63
- if (bothNeg && bothIntPos && lIntLen < rIntLen)
64
- return [l, 1];
65
- if (bothNeg && bothIntPos && lIntLen > rIntLen)
66
- return [r, -1];
67
- if (bothPos && bothIntNeg && lIntLen > rIntLen)
68
- return [l, 1];
69
- if (bothPos && bothIntNeg && lIntLen < rIntLen)
70
- return [r, -1];
71
- if (bothNeg)
72
- _a = [r, l], left = _a[0], right = _a[1];
73
- while (idx < (lenL > lenR ? lenL : lenR)) {
74
- var numL = left.array[idx];
75
- var numR = right.array[idx];
76
- if (!numL && bothPos)
77
- return [r, -1];
78
- if (!numR && bothPos)
24
+ if (biL === 0n || biR === 0n) {
25
+ if (biL > biR)
79
26
  return [l, 1];
80
- 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
+ const fpeBiL = BigInt(fpeL);
40
+ const fpeBiR = BigInt(fpeR);
41
+ const max = fpeBiL > fpeBiR ? fpeBiL : fpeBiR;
42
+ const powL = biL * 10n ** (max - fpeBiL);
43
+ const powR = biR * 10n ** (max - fpeBiR);
44
+ if (powL > powR)
81
45
  return [l, 1];
82
- if (!numR && bothNeg)
46
+ if (powL < powR)
83
47
  return [r, -1];
84
- if (numL !== numR) {
85
- 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];
86
57
  }
87
- idx += 1;
58
+ const fpeBiL = BigInt(fpeL);
59
+ const fpeBiR = BigInt(fpeR);
60
+ const max = fpeBiL > fpeBiR ? fpeBiL : fpeBiR;
61
+ const powL = biL * 10n ** (max - fpeBiL);
62
+ const powR = biR * 10n ** (max - fpeBiR);
63
+ if (powL > powR)
64
+ return [l, 1];
65
+ if (powL < powR)
66
+ return [r, -1];
88
67
  }
89
68
  return [l, 0];
90
69
  };
91
- export var isEqualInner = function (_a) {
92
- var left = _a.left, right = _a.right;
70
+ export const isEqualInner = ({ left, right }) => {
93
71
  return compareInner(left, right)[1] === 0;
94
72
  };
95
- export var isLeftGreaterInner = function (_a) {
96
- var left = _a.left, right = _a.right;
73
+ export const isLeftGreaterInner = ({ left, right }) => {
97
74
  return compareInner(left, right)[1] > 0;
98
75
  };
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 const mean = (array) => {
5
+ const arrayInner = array.map((str) => s2bi(str));
6
+ const [bi, fpe] = meanInner(arrayInner);
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,9 +1,6 @@
1
- import { addRoute } from "../operations/add/utils.js";
2
1
  import { PipeInner } from "../pipe/utils.js";
3
- import { DEFAULT } from "../shared/constant.js";
4
- import { s2a } from "../shared/utils.js";
5
- export var meanInner = function (array) {
6
- var left = addRoute(array, DEFAULT);
7
- var right = s2a(array.length.toString());
8
- return new PipeInner().divInner([left, right]).result;
2
+ export const meanInner = (array) => {
3
+ const left = new PipeInner().add(array).calc();
4
+ const right = [BigInt(array.length), 0];
5
+ return new PipeInner().div([left, right]).calc();
9
6
  };
@@ -1,2 +1,2 @@
1
1
  /** This function adds numbers (as string). */
2
- export declare function add(strs: string[]): string;
2
+ export declare const add: (array: string[]) => string;
@@ -1,7 +1,8 @@
1
- import { DEFAULT } from "../../shared/constant.js";
2
- import { a2s, s2a } from "../../shared/utils.js";
3
- import { addRoute } from "./utils.js";
1
+ import { bi2s, s2bi } from "../../shared/utils.js";
2
+ import { calcInner } from "../../shared/utils.js";
4
3
  /** This function adds numbers (as string). */
5
- export function add(strs) {
6
- return a2s(addRoute(strs.map(function (str) { return s2a(str); }), DEFAULT));
7
- }
4
+ export const add = (array) => {
5
+ const arrayInner = array.map((str) => s2bi(str));
6
+ const [bigInt, fpe] = calcInner(arrayInner, (a, b) => a + b);
7
+ return bi2s(bigInt, fpe);
8
+ };
@@ -1,2 +1,2 @@
1
1
  /** This function should divide numbers (as string). */
2
- export declare const div: (strs: string[], limit?: number) => string;
2
+ export declare const div: (array: string[], limit?: number) => string;
@@ -1,10 +1,8 @@
1
- import { DEFAULT } from "../../shared/constant.js";
2
- import { a2s, s2a } from "../../shared/utils.js";
3
- import { divRoute } from "./utils.js";
1
+ import { bi2s, s2bi } from "../../shared/utils.js";
2
+ import { divInner } from "./utils.js";
4
3
  /** 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));
4
+ export const div = (array, limit = 20) => {
5
+ const arrayInner = array.map((str) => s2bi(str));
6
+ const [bigInt, fpe] = divInner(arrayInner, limit !== null && limit !== void 0 ? limit : 20);
7
+ return bi2s(bigInt, fpe);
10
8
  };
@@ -1,3 +1,2 @@
1
- import type { InputData } from "../../types.d.ts";
2
- export type DivInner = (L: [number[], number], R: [number[], number], isNegative: boolean, initLimit: number) => InputData;
3
- export type DivRoute = (input: InputData[], initValue: InputData, limit: number) => InputData;
1
+ import type { BI } from "../../shared/types.d.ts";
2
+ export type DivInner = (array: BI[], limit: number, def?: BI) => BI;
@@ -1,3 +1,2 @@
1
- import type { DivInner, DivRoute } from "./types.d.ts";
1
+ import type { DivInner } from "./types.d.ts";
2
2
  export declare const divInner: DivInner;
3
- export declare const divRoute: DivRoute;
@@ -1,99 +1,39 @@
1
- import { NIL } from "../../shared/constant.js";
2
- export var divInner = function (_a, _b, isNegative, initLimit) {
3
- var arrL = _a[0], intL = _a[1];
4
- var arrR = _b[0], intR = _b[1];
5
- var decDiff = (arrL.length - intL) - (arrR.length - intR);
6
- var _c = [[arrR, 1], [arrL, -1]], L = _c[0], R = _c[1];
7
- var _d = decDiff > 0 ? L : R, arr = _d[0], abs = _d[1];
8
- for (var i = 0; i < decDiff * abs; i++) {
9
- arr.push(48);
10
- }
11
- var digit = 48;
12
- var limit = initLimit;
13
- var isFloat = false;
14
- var l = 0;
15
- var r = arrR.length - 1;
16
- var intLength = 0;
17
- var result = [];
18
- var lenArrayL = r - arrL.length;
19
- for (var i = 0; i <= lenArrayL; i++) {
20
- if (i === 0) {
21
- isFloat = true;
22
- intLength = 1;
23
- }
24
- result.push(48);
25
- arrL.push(48);
26
- limit -= 1;
27
- }
28
- while (r < arrL.length && limit >= 0) {
29
- var comp = false;
30
- if (arrL[l] === 48)
31
- l += 1;
32
- for (var i = 0; i < arrR.length; i++) {
33
- if ((r - l + 1) < arrR.length)
34
- comp = true;
35
- if ((r - l + 1) !== arrR.length)
36
- break;
37
- if (arrL[l + i] !== arrR[i]) {
38
- comp = arrL[l + i] < arrR[i];
39
- break;
40
- }
1
+ export const divInner = (array, limit, def) => {
2
+ let bigInt = def ? def[0] : array[0][0];
3
+ let fpe = def ? def[1] : array[0][1];
4
+ const isNeg = bigInt < 0n;
5
+ for (let i = def ? 0 : 1; i < array.length; i++) {
6
+ let [bigCurrent, dpLen] = array[i];
7
+ let r = 0n;
8
+ if (bigInt === 0n && fpe === 0)
9
+ return [0n, 0];
10
+ if (fpe === dpLen) {
11
+ fpe = 0;
12
+ dpLen = 0;
41
13
  }
42
- if (comp) {
43
- if (r < arrL.length && (result.length > 0 || digit !== 48)) {
44
- if (!isFloat)
45
- intLength += 1;
46
- result.push(digit);
47
- }
48
- if (r >= arrL.length - 1) {
49
- if (initLimit === limit)
50
- isFloat = true;
51
- arrL.push(48);
52
- limit -= 1;
53
- }
54
- r += 1;
55
- digit = 48;
56
- continue;
14
+ if (dpLen > 0 && fpe < dpLen) {
15
+ bigInt *= 10n ** BigInt(dpLen - fpe);
16
+ fpe = 0;
57
17
  }
58
- for (var i = arrR.length - 1; i >= 0; i--) {
59
- var idx = r - (arrR.length - 1 - i);
60
- if (arrL[idx] < arrR[i]) {
61
- arrL[idx] = arrL[idx] - arrR[i] + 58;
62
- arrL[idx - 1] -= 1;
63
- }
64
- else {
65
- arrL[idx] = arrL[idx] - arrR[i] + 48;
66
- }
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;
67
25
  }
68
- digit += 1;
69
- }
70
- var multipliedResult = [];
71
- var count = 0;
72
- if (result[0] === 48) {
73
- var isGrZero = false;
74
- for (var i = 0; i < result.length; i++) {
75
- if (!isGrZero)
76
- result[i] > 48 ? isGrZero = true : count += 1;
77
- isGrZero && multipliedResult.push(result[i]);
78
- }
79
- for (var i = 0; i < count; i++) {
80
- multipliedResult.push(48);
81
- intLength -= 1;
26
+ const q = bigInt / bigCurrent;
27
+ r = bigInt - q * bigCurrent;
28
+ bigInt = q;
29
+ while ((isNeg ? r < 0n : r > 0n) && fpe < limit) {
30
+ const nextBigInt = r * 10n;
31
+ const nextQ = nextBigInt / bigCurrent;
32
+ const nextRemained = nextBigInt - nextQ * bigCurrent;
33
+ bigInt = bigInt * 10n + nextQ;
34
+ r = nextRemained;
35
+ fpe += 1;
82
36
  }
83
37
  }
84
- return result.length === count
85
- ? NIL
86
- : {
87
- array: result[0] === 48 ? multipliedResult : result,
88
- isFloat: isFloat,
89
- isNegative: isNegative,
90
- intLength: intLength,
91
- };
92
- };
93
- export var divRoute = function (input, initValue, limit) {
94
- return input.reduce(function (left, right) {
95
- if (left.array.length === 0)
96
- return right;
97
- return divInner([left.array, left.intLength], [right.array, right.intLength], left.isNegative !== right.isNegative, limit);
98
- }, initValue);
38
+ return [bigInt, fpe];
99
39
  };
@@ -1,2 +1,2 @@
1
1
  /** This function multiplies numbers (as string). */
2
- export declare const mul: (strs: string[]) => string;
2
+ export declare const mul: (array: string[]) => string;
@@ -1,7 +1,7 @@
1
- import { DEFAULT } from "../../shared/constant.js";
2
- import { a2s, s2a } from "../../shared/utils.js";
3
- import { mulRoute } from "./utils.js";
1
+ import { bi2s, calcInner, s2bi } from "../../shared/utils.js";
4
2
  /** This function multiplies numbers (as string). */
5
- export var mul = function (strs) {
6
- return a2s(mulRoute(strs.map(function (str) { return s2a(str); }), DEFAULT));
3
+ export const mul = (array) => {
4
+ const arrayInner = array.map((str) => s2bi(str));
5
+ const [bigInt, fpe] = calcInner(arrayInner, (a, b) => a * b);
6
+ return bi2s(bigInt, fpe);
7
7
  };
@@ -1,2 +1,2 @@
1
1
  /** This function subtracts numbers (as string). */
2
- export declare function sub(strs: string[]): string;
2
+ export declare function sub(array: string[]): string;