@numio/bigmath 2.2.5 → 2.2.7
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 +6 -0
- package/package.json +1 -1
- package/src/IQR/iqr.js +2 -2
- package/src/MAD/mad.js +2 -2
- package/src/abs/abs.js +2 -2
- package/src/cbrt/cbrt.js +2 -2
- package/src/compare/max.js +2 -2
- package/src/compare/min.js +2 -2
- package/src/mean/mean.js +2 -2
- package/src/operations/add/add.js +29 -5
- package/src/operations/div/div.js +2 -2
- package/src/operations/mul/mul.js +2 -2
- package/src/operations/sub/sub.js +2 -2
- package/src/pipe/pipe.js +1 -2
- package/src/quartile/quartile.js +4 -4
- package/src/shared/types.d.ts +3 -2
- package/src/shared/utils.d.ts +2 -1
- package/src/shared/utils.js +25 -8
- package/src/sort/sort.js +1 -1
- package/src/sqrt/sqrt.js +2 -2
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
### 2.2.7
|
2
|
+
Throw an error if input is not valid. In the error show exact invalid input.
|
3
|
+
|
4
|
+
### 2.2.6
|
5
|
+
Optimize performance for small numbers (add operation)
|
6
|
+
|
1
7
|
### 2.2.5
|
2
8
|
Handle "0B", "0X", "0O", "-0B", "-0X", "-0O" as a start of the input for toBase function.
|
3
9
|
Move documentation from README.md to web-site http://numio.deno.dev
|
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.7",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
package/src/IQR/iqr.js
CHANGED
@@ -3,6 +3,6 @@ import { IQRInner } from "./utils.js";
|
|
3
3
|
//** This function returns Interquartile Range */
|
4
4
|
export const IQR = (array, sigNum) => {
|
5
5
|
const arrayInner = array.map((str) => s2bi(str));
|
6
|
-
const
|
7
|
-
return bi2s(bi
|
6
|
+
const bi = IQRInner(arrayInner, sigNum);
|
7
|
+
return bi2s(bi);
|
8
8
|
};
|
package/src/MAD/mad.js
CHANGED
@@ -3,6 +3,6 @@ import { MADInner } from "./utils.js";
|
|
3
3
|
//** This function returns Median Absolute Deviation */
|
4
4
|
export const MAD = (array) => {
|
5
5
|
const arrayInner = array.map((str) => s2bi(str));
|
6
|
-
const
|
7
|
-
return bi2s(bi
|
6
|
+
const bi = MADInner(arrayInner);
|
7
|
+
return bi2s(bi);
|
8
8
|
};
|
package/src/abs/abs.js
CHANGED
@@ -2,6 +2,6 @@ import { bi2s, s2bi } from "../shared/utils.js";
|
|
2
2
|
import { absInner } from "./utils.js";
|
3
3
|
/** Get absolute value of a number */
|
4
4
|
export const abs = (value) => {
|
5
|
-
const
|
6
|
-
return bi2s(bi
|
5
|
+
const bi = absInner(s2bi(value));
|
6
|
+
return bi2s(bi);
|
7
7
|
};
|
package/src/cbrt/cbrt.js
CHANGED
@@ -4,6 +4,6 @@ import { cbrtInner } from "./utils.js";
|
|
4
4
|
/** Find cube root of a number */
|
5
5
|
export const cbrt = (value, precision) => {
|
6
6
|
const valueInner = s2bi(value);
|
7
|
-
const [
|
8
|
-
return round(bi2s(bi
|
7
|
+
const [bi, decimals] = cbrtInner(valueInner, precision ? s2bi(precision) : undefined);
|
8
|
+
return round(bi2s(bi), { decimals });
|
9
9
|
};
|
package/src/compare/max.js
CHANGED
@@ -3,6 +3,6 @@ import { maxInner } from "./utils.js";
|
|
3
3
|
/** This function returns max value. */
|
4
4
|
export const max = (array) => {
|
5
5
|
const arrayInner = array.map((str) => s2bi(str));
|
6
|
-
const
|
7
|
-
return bi2s(bi
|
6
|
+
const bi = maxInner(arrayInner);
|
7
|
+
return bi2s(bi);
|
8
8
|
};
|
package/src/compare/min.js
CHANGED
@@ -3,6 +3,6 @@ import { minInner } from "./utils.js";
|
|
3
3
|
/** This function returns min value. */
|
4
4
|
export const min = (array) => {
|
5
5
|
const arrayInner = array.map((str) => s2bi(str));
|
6
|
-
const
|
7
|
-
return bi2s(bi
|
6
|
+
const bi = minInner(arrayInner);
|
7
|
+
return bi2s(bi);
|
8
8
|
};
|
package/src/mean/mean.js
CHANGED
@@ -3,6 +3,6 @@ import { meanInner } from "./utils.js";
|
|
3
3
|
/** This function returns mean of an array. */
|
4
4
|
export const mean = (array) => {
|
5
5
|
const arrayInner = array.map((str) => s2bi(str));
|
6
|
-
const
|
7
|
-
return bi2s(bi
|
6
|
+
const bi = meanInner(arrayInner);
|
7
|
+
return bi2s(bi);
|
8
8
|
};
|
@@ -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
|
-
|
7
|
-
|
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];
|
30
|
+
const bi = calcInner(arrayInner, (a, b) => a + b);
|
31
|
+
return bi2s(bi);
|
8
32
|
};
|
@@ -3,6 +3,6 @@ import { divInner } from "./utils.js";
|
|
3
3
|
/** This function should divide numbers (as string). */
|
4
4
|
export const div = (array, limit = 20) => {
|
5
5
|
const arrayInner = array.map((str) => s2bi(str));
|
6
|
-
const
|
7
|
-
return bi2s(
|
6
|
+
const bi = divInner(arrayInner, limit !== null && limit !== void 0 ? limit : 20);
|
7
|
+
return bi2s(bi);
|
8
8
|
};
|
@@ -2,6 +2,6 @@ import { bi2s, calcInner, s2bi } from "../../shared/utils.js";
|
|
2
2
|
/** This function multiplies numbers (as string). */
|
3
3
|
export const mul = (array) => {
|
4
4
|
const arrayInner = array.map((str) => s2bi(str));
|
5
|
-
const
|
6
|
-
return bi2s(
|
5
|
+
const bi = calcInner(arrayInner, (a, b) => a * b);
|
6
|
+
return bi2s(bi);
|
7
7
|
};
|
@@ -3,6 +3,6 @@ import { calcInner } from "../../shared/utils.js";
|
|
3
3
|
/** This function subtracts numbers (as string). */
|
4
4
|
export function sub(array) {
|
5
5
|
const arrayInner = array.map((str) => s2bi(str));
|
6
|
-
const
|
7
|
-
return bi2s(
|
6
|
+
const bi = calcInner(arrayInner, (a, b) => a - b);
|
7
|
+
return bi2s(bi);
|
8
8
|
}
|
package/src/pipe/pipe.js
CHANGED
@@ -50,8 +50,7 @@ export class Pipe {
|
|
50
50
|
if (!__classPrivateFieldGet(this, _Pipe_result, "f")) {
|
51
51
|
throw new Error("Cannot calculate based on an undefined input.");
|
52
52
|
}
|
53
|
-
|
54
|
-
return bi2s(bi, fpe);
|
53
|
+
return bi2s(__classPrivateFieldGet(this, _Pipe_result, "f"));
|
55
54
|
}
|
56
55
|
resultToBase(radix) {
|
57
56
|
var _a;
|
package/src/quartile/quartile.js
CHANGED
@@ -3,10 +3,10 @@ import { quartileInner } from "./utils.js";
|
|
3
3
|
/** This function returns Q1, Q2, Q3 (quartile). */
|
4
4
|
export const quartile = (array) => {
|
5
5
|
const arrayInner = array.map((str) => s2bi(str));
|
6
|
-
const { Q1
|
6
|
+
const { Q1, Q2, Q3 } = quartileInner(arrayInner);
|
7
7
|
return {
|
8
|
-
Q1: bi2s(
|
9
|
-
Q2: bi2s(
|
10
|
-
Q3: bi2s(
|
8
|
+
Q1: bi2s(Q1),
|
9
|
+
Q2: bi2s(Q2),
|
10
|
+
Q3: bi2s(Q3),
|
11
11
|
};
|
12
12
|
};
|
package/src/shared/types.d.ts
CHANGED
@@ -2,5 +2,6 @@ export type BI = [bigint, number];
|
|
2
2
|
export type FillHead = (len: number, fpe: number, isNeg: boolean, hasBefore: boolean) => string;
|
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
|
-
export type BI2S = (
|
6
|
-
export type S2BI = (str: string) => BI;
|
5
|
+
export type BI2S = (value: BI) => string;
|
6
|
+
export type S2BI = (str: string, fpi?: number) => BI;
|
7
|
+
export type GetBigInt = (value: string, type: "di" | "hbo" | "si" | "reg", fpi: number) => bigint;
|
package/src/shared/utils.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
import type { BI2S, CalcInner, FillHead, S2BI, TrimTail } from "./types.d.ts";
|
1
|
+
import type { BI2S, CalcInner, FillHead, GetBigInt, S2BI, TrimTail } from "./types.d.ts";
|
2
2
|
export declare const bi2s: BI2S;
|
3
3
|
export declare const s2bi: S2BI;
|
4
|
+
export declare const getBigInt: GetBigInt;
|
4
5
|
export declare const calcInner: CalcInner;
|
5
6
|
export declare const fillHead: FillHead;
|
6
7
|
export declare const trimTail: TrimTail;
|
package/src/shared/utils.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
export const bi2s = (bigInt, fpe) => {
|
1
|
+
export const bi2s = ([bigInt, fpe]) => {
|
2
2
|
if (bigInt === 0n)
|
3
3
|
return "0";
|
4
4
|
const isNeg = bigInt < 0n;
|
@@ -16,32 +16,49 @@ 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") ||
|
24
24
|
str.startsWith("-0O") || str.startsWith("0O");
|
25
25
|
const isBinary = str.startsWith("0b") || str.startsWith("-0b") ||
|
26
26
|
str.startsWith("-0B") || str.startsWith("0B");
|
27
|
-
if (fpi === -1 && !isHex && !isOctal && !isBinary)
|
28
|
-
return [
|
27
|
+
if (fpi === -1 && !isHex && !isOctal && !isBinary) {
|
28
|
+
return [getBigInt(str, "di", fpi), 0];
|
29
|
+
}
|
29
30
|
if (isHex || isBinary || isOctal) {
|
30
31
|
const isNegative = str[0] === "-";
|
31
|
-
const bi =
|
32
|
+
const bi = getBigInt(str, "hbo", fpi);
|
32
33
|
return [isNegative ? -1n * bi : bi, 0];
|
33
34
|
}
|
34
35
|
if (str.length < 15 && str[0] !== "0") {
|
35
36
|
return [
|
36
|
-
|
37
|
+
getBigInt(str, "si", fpi),
|
37
38
|
str.length - 1 - fpi,
|
38
39
|
];
|
39
40
|
}
|
40
41
|
return [
|
41
|
-
|
42
|
+
getBigInt(str, "reg", fpi),
|
42
43
|
str.length - 1 - fpi,
|
43
44
|
];
|
44
45
|
};
|
46
|
+
export const getBigInt = (value, type, fpi) => {
|
47
|
+
let bi = 0n;
|
48
|
+
const typeMap = {
|
49
|
+
di: value,
|
50
|
+
hbo: value.slice(value[0] === "-" ? 1 : 0),
|
51
|
+
si: +(value.slice(0, fpi) + value.slice(fpi + 1)),
|
52
|
+
reg: value.slice(0, fpi) + value.slice(fpi + 1),
|
53
|
+
};
|
54
|
+
try {
|
55
|
+
bi = BigInt(typeMap[type]);
|
56
|
+
}
|
57
|
+
catch (_) {
|
58
|
+
throw new Error(`${value} is not valid input`);
|
59
|
+
}
|
60
|
+
return bi;
|
61
|
+
};
|
45
62
|
export const calcInner = (array, op, def) => {
|
46
63
|
let totalBi = def ? def[0] : array[0][0];
|
47
64
|
let totalFpe = def ? def[1] : array[0][1];
|
package/src/sort/sort.js
CHANGED
@@ -5,5 +5,5 @@ import { sortInner } from "./utils.js";
|
|
5
5
|
export const sort = (array, sorting = ASC) => {
|
6
6
|
const arrayInner = array.map((str) => s2bi(str));
|
7
7
|
sortInner(arrayInner, sorting);
|
8
|
-
return arrayInner.map((
|
8
|
+
return arrayInner.map((bi) => bi2s(bi));
|
9
9
|
};
|
package/src/sqrt/sqrt.js
CHANGED
@@ -4,6 +4,6 @@ import { sqrtInner } from "./utils.js";
|
|
4
4
|
/** Find square root of a number */
|
5
5
|
export const sqrt = (value, precision) => {
|
6
6
|
const valueInner = s2bi(value);
|
7
|
-
const [
|
8
|
-
return round(bi2s(bi
|
7
|
+
const [bi, decimals] = sqrtInner(valueInner, precision ? s2bi(precision) : undefined);
|
8
|
+
return round(bi2s(bi), { decimals });
|
9
9
|
};
|