@numio/bigmath 1.1.0 → 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.
- package/README.md +16 -4
- package/package.json +7 -2
- package/src/IQR/main.js +5 -4
- package/src/IQR/types.d.ts +2 -2
- package/src/IQR/utils.js +3 -3
- package/src/MAD/main.js +5 -4
- package/src/MAD/types.d.ts +2 -2
- package/src/MAD/utils.js +2 -4
- package/src/compare/main.js +12 -10
- package/src/compare/types.d.ts +5 -5
- package/src/compare/utils.js +43 -64
- package/src/mean/main.js +5 -5
- package/src/mean/types.d.ts +2 -2
- package/src/mean/utils.js +3 -6
- package/src/operations/add/main.d.ts +1 -1
- package/src/operations/add/main.js +7 -6
- package/src/operations/div/main.d.ts +1 -1
- package/src/operations/div/main.js +6 -7
- package/src/operations/div/types.d.ts +2 -3
- package/src/operations/div/utils.d.ts +1 -2
- package/src/operations/div/utils.js +33 -93
- package/src/operations/mul/main.d.ts +1 -1
- package/src/operations/mul/main.js +5 -5
- package/src/operations/sub/main.d.ts +1 -1
- package/src/operations/sub/main.js +6 -5
- package/src/pipe/main.d.ts +6 -6
- package/src/pipe/main.js +23 -22
- package/src/pipe/utils.d.ts +7 -8
- package/src/pipe/utils.js +14 -18
- package/src/quartile/main.js +7 -7
- package/src/quartile/types.d.ts +6 -6
- package/src/quartile/utils.js +2 -10
- package/src/shared/constant.d.ts +0 -12
- package/src/shared/constant.js +0 -12
- package/src/shared/types.d.ts +6 -5
- package/src/shared/utils.d.ts +6 -4
- package/src/shared/utils.js +64 -71
- package/src/sort/main.js +7 -4
- package/src/sort/types.d.ts +3 -3
- package/src/sqrt/main.js +5 -5
- package/src/sqrt/types.d.ts +2 -2
- package/src/sqrt/utils.js +13 -23
- package/src/operations/add/types.d.ts +0 -2
- package/src/operations/add/utils.d.ts +0 -5
- package/src/operations/add/utils.js +0 -59
- package/src/operations/mul/types.d.ts +0 -2
- package/src/operations/mul/utils.d.ts +0 -5
- package/src/operations/mul/utils.js +0 -50
- package/src/operations/sub/types.d.ts +0 -2
- package/src/operations/sub/utils.d.ts +0 -5
- package/src/operations/sub/utils.js +0 -81
- 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
|
-
|
50
|
-
|
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": "
|
4
|
+
"version": "2.0.0",
|
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 {
|
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 (
|
5
|
-
var
|
6
|
-
|
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
|
};
|
package/src/IQR/types.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
2
2
|
export type TIQR = (array: string[], sigNum?: boolean) => string;
|
3
|
-
export type TIQRInner = (array:
|
3
|
+
export type TIQRInner = (array: BI[], sigNum?: boolean) => BI;
|
package/src/IQR/utils.js
CHANGED
@@ -2,16 +2,16 @@ 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
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 = new PipeInner().
|
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
|
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 {
|
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 (
|
5
|
-
var
|
6
|
-
|
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
|
};
|
package/src/MAD/types.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
2
2
|
export type TMAD = (array: string[]) => string;
|
3
|
-
export type TMADInner = (array:
|
3
|
+
export type TMADInner = (array: BI[]) => BI;
|
package/src/MAD/utils.js
CHANGED
@@ -1,14 +1,12 @@
|
|
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
5
|
export var MADInner = function (array) {
|
7
6
|
var median = quartileInner(array).Q2;
|
8
7
|
var madArray = array.map(function (el) {
|
9
|
-
var
|
10
|
-
|
11
|
-
return result;
|
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;
|
package/src/compare/main.js
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
-
import {
|
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 (
|
5
|
-
var
|
6
|
-
|
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 (
|
10
|
-
var
|
11
|
-
|
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:
|
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:
|
23
|
+
return isEqualInner({ left: s2bi(left), right: s2bi(right) });
|
22
24
|
};
|
package/src/compare/types.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import type {
|
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:
|
10
|
-
export type MinInner = (array:
|
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:
|
14
|
-
right:
|
13
|
+
left: BI;
|
14
|
+
right: BI;
|
15
15
|
}) => boolean;
|
16
16
|
export type IsLeftGreaterInner = IsEqualInner;
|
package/src/compare/utils.js
CHANGED
@@ -15,76 +15,55 @@ export var minInner = function (array) {
|
|
15
15
|
return min;
|
16
16
|
};
|
17
17
|
export var compareInner = function (l, r) {
|
18
|
-
var
|
19
|
-
var
|
20
|
-
|
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)
|
18
|
+
var biL = l[0], fpeL = l[1];
|
19
|
+
var biR = r[0], fpeR = r[1];
|
20
|
+
if (biL > 0n && biR < 0n)
|
32
21
|
return [l, 1];
|
33
|
-
if (
|
22
|
+
if (biL < 0n && biR > 0n)
|
34
23
|
return [r, -1];
|
35
|
-
if (
|
36
|
-
|
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 (
|
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)
|
81
45
|
return [l, 1];
|
82
|
-
if (
|
46
|
+
if (powL < powR)
|
83
47
|
return [r, -1];
|
84
|
-
|
85
|
-
|
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
|
-
|
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];
|
88
67
|
}
|
89
68
|
return [l, 0];
|
90
69
|
};
|
package/src/mean/main.js
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
import {
|
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 (
|
5
|
-
var
|
6
|
-
var
|
7
|
-
return
|
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
|
};
|
package/src/mean/types.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
2
2
|
export type Mean = (array: string[]) => string;
|
3
|
-
export type MeanInner = (array:
|
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
2
|
export var meanInner = function (array) {
|
6
|
-
var left =
|
7
|
-
var right =
|
8
|
-
return new PipeInner().
|
3
|
+
var left = new PipeInner().add(array).calc();
|
4
|
+
var 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
|
2
|
+
export declare const add: (array: string[]) => string;
|
@@ -1,7 +1,8 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
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
|
6
|
-
|
7
|
-
}
|
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
|
+
};
|
@@ -1,2 +1,2 @@
|
|
1
1
|
/** This function should divide numbers (as string). */
|
2
|
-
export declare const div: (
|
2
|
+
export declare const div: (array: string[], limit?: number) => string;
|
@@ -1,10 +1,9 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
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 (
|
4
|
+
export var div = function (array, limit) {
|
6
5
|
if (limit === void 0) { limit = 20; }
|
7
|
-
|
8
|
-
|
9
|
-
return
|
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);
|
10
9
|
};
|
@@ -1,3 +1,2 @@
|
|
1
|
-
import type {
|
2
|
-
export type DivInner = (
|
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,99 +1,39 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
var
|
4
|
-
var
|
5
|
-
var
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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 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;
|
41
13
|
}
|
42
|
-
if (
|
43
|
-
|
44
|
-
|
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 *= Math.pow(10n, BigInt(dpLen - fpe));
|
16
|
+
fpe = 0;
|
57
17
|
}
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
}
|
79
|
-
for (var i = 0; i < count; i++) {
|
80
|
-
multipliedResult.push(48);
|
81
|
-
intLength -= 1;
|
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;
|
82
36
|
}
|
83
37
|
}
|
84
|
-
return
|
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: (
|
2
|
+
export declare const mul: (array: string[]) => string;
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import {
|
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 (
|
6
|
-
|
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
7
|
};
|
@@ -1,2 +1,2 @@
|
|
1
1
|
/** This function subtracts numbers (as string). */
|
2
|
-
export declare function sub(
|
2
|
+
export declare function sub(array: string[]): string;
|
@@ -1,7 +1,8 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
import { subRoute } from "./utils.js";
|
1
|
+
import { bi2s, s2bi } from "../../shared/utils.js";
|
2
|
+
import { calcInner } from "../../shared/utils.js";
|
4
3
|
/** This function subtracts numbers (as string). */
|
5
|
-
export function sub(
|
6
|
-
|
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);
|
7
8
|
}
|
package/src/pipe/main.d.ts
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BI2S } from "../shared/types.d.ts";
|
2
2
|
export declare class Pipe {
|
3
3
|
#private;
|
4
4
|
constructor();
|
5
|
-
add(
|
6
|
-
sub(
|
7
|
-
div(
|
8
|
-
mul(
|
9
|
-
calc(): ReturnType<
|
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>;
|
10
10
|
}
|
11
11
|
/** Using this function you can chain operations (add, sub, div, mul). */
|
12
12
|
export declare const pipe: Pipe;
|