@numio/bigmath 2.2.5 → 2.2.6
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/CHANGELOG.md +3 -0
- package/package.json +1 -1
- package/src/operations/add/add.js +27 -3
- package/src/shared/types.d.ts +1 -1
- package/src/shared/utils.js +2 -2
package/CHANGELOG.md
CHANGED
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": "2.2.
|
4
|
+
"version": "2.2.6",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
@@ -1,8 +1,32 @@
|
|
1
|
-
import { bi2s, s2bi } from "../../shared/utils.js";
|
2
|
-
|
1
|
+
import { bi2s, s2bi, calcInner } from "../../shared/utils.js";
|
2
|
+
const MAX_SUM = 8000000000000000;
|
3
|
+
const MIN_SUM = -8000000000000000;
|
3
4
|
/** This function adds numbers (as string). */
|
4
5
|
export const add = (array) => {
|
5
|
-
const arrayInner = array.
|
6
|
+
const arrayInner = Array(array.length);
|
7
|
+
let sum = 0;
|
8
|
+
let i = 0;
|
9
|
+
let j = 0;
|
10
|
+
let fpi = 0;
|
11
|
+
while (i < array.length) {
|
12
|
+
fpi = array[i].indexOf(".");
|
13
|
+
if (array[i].length <= 15 && fpi === -1) {
|
14
|
+
sum += +array[i];
|
15
|
+
}
|
16
|
+
else {
|
17
|
+
arrayInner[j] = s2bi(array[i], fpi);
|
18
|
+
j += 1;
|
19
|
+
}
|
20
|
+
i += 1;
|
21
|
+
if (sum > MAX_SUM || sum < MIN_SUM) {
|
22
|
+
arrayInner[j] = [BigInt(sum), 0];
|
23
|
+
sum = 0;
|
24
|
+
j += 1;
|
25
|
+
}
|
26
|
+
}
|
27
|
+
if (i !== j)
|
28
|
+
arrayInner.length = j + 1;
|
29
|
+
arrayInner[j] = [BigInt(sum), 0];
|
6
30
|
const [bigInt, fpe] = calcInner(arrayInner, (a, b) => a + b);
|
7
31
|
return bi2s(bigInt, fpe);
|
8
32
|
};
|
package/src/shared/types.d.ts
CHANGED
@@ -3,4 +3,4 @@ export type FillHead = (len: number, fpe: number, isNeg: boolean, hasBefore: boo
|
|
3
3
|
export type TrimTail = (str: string) => string;
|
4
4
|
export type CalcInner = (array: BI[], op: (a: bigint, b: bigint) => bigint, def?: BI) => BI;
|
5
5
|
export type BI2S = (bigInt: bigint, fpe: number) => string;
|
6
|
-
export type S2BI = (str: string) => BI;
|
6
|
+
export type S2BI = (str: string, fpi?: number) => BI;
|
package/src/shared/utils.js
CHANGED
@@ -16,8 +16,8 @@ export const bi2s = (bigInt, fpe) => {
|
|
16
16
|
}
|
17
17
|
return `${fillHead(bigStr.length, fpe, isNeg, false)}${trimTail(after)}`;
|
18
18
|
};
|
19
|
-
export const s2bi = (str) => {
|
20
|
-
const fpi = str.indexOf(".");
|
19
|
+
export const s2bi = (str, _fpi) => {
|
20
|
+
const fpi = _fpi !== null && _fpi !== void 0 ? _fpi : str.indexOf(".");
|
21
21
|
const isHex = str.startsWith("0x") || str.startsWith("-0x") ||
|
22
22
|
str.startsWith("-0X") || str.startsWith("0X");
|
23
23
|
const isOctal = str.startsWith("0o") || str.startsWith("-0o") ||
|