@numio/bigmath 2.0.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.
- package/package.json +1 -1
- package/src/IQR/main.js +3 -3
- package/src/IQR/utils.js +8 -9
- package/src/MAD/main.js +3 -3
- package/src/MAD/utils.js +5 -5
- package/src/compare/main.js +8 -10
- package/src/compare/utils.js +21 -23
- package/src/mean/main.js +3 -3
- package/src/mean/utils.js +3 -3
- package/src/operations/add/main.js +3 -3
- package/src/operations/div/main.js +3 -4
- package/src/operations/div/utils.js +12 -12
- package/src/operations/mul/main.js +3 -3
- package/src/operations/sub/main.js +2 -2
- package/src/pipe/main.js +23 -26
- package/src/pipe/utils.js +17 -21
- package/src/quartile/main.js +3 -3
- package/src/quartile/utils.js +8 -8
- package/src/round/constants.js +7 -7
- package/src/round/main.js +16 -17
- package/src/round/utils.js +24 -25
- package/src/shared/constant.js +1 -1
- package/src/shared/utils.js +25 -25
- package/src/sort/constants.js +3 -3
- package/src/sort/main.js +3 -7
- package/src/sort/utils.js +10 -12
- package/src/sqrt/main.js +5 -5
- package/src/sqrt/utils.js +7 -9
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.0.
|
4
|
+
"version": "2.0.1",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
package/src/IQR/main.js
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import { bi2s, s2bi } from "../shared/utils.js";
|
2
2
|
import { IQRInner } from "./utils.js";
|
3
3
|
//** This function returns Interquartile Range */
|
4
|
-
export
|
5
|
-
|
6
|
-
|
4
|
+
export const IQR = (array, sigNum) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bi, fpe] = IQRInner(arrayInner, sigNum);
|
7
7
|
return bi2s(bi, fpe);
|
8
8
|
};
|
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
|
-
export
|
6
|
-
export
|
7
|
-
|
8
|
-
|
9
|
-
var sub = new PipeInner().sub([Q3, Q1]).calc();
|
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();
|
10
9
|
if (sigNum) {
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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,8 +1,8 @@
|
|
1
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
|
5
|
-
|
6
|
-
|
4
|
+
export const MAD = (array) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bi, fpe] = MADInner(arrayInner);
|
7
7
|
return bi2s(bi, fpe);
|
8
8
|
};
|
package/src/MAD/utils.js
CHANGED
@@ -2,12 +2,12 @@ import { PipeInner } from "../pipe/utils.js";
|
|
2
2
|
import { quartileInner } from "../quartile/utils.js";
|
3
3
|
import { ASC } from "../sort/constants.js";
|
4
4
|
import { sortInner } from "../sort/utils.js";
|
5
|
-
export
|
6
|
-
|
7
|
-
|
8
|
-
|
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
9
|
return bi < 0n ? [bi * -1n, fpe] : [bi, fpe];
|
10
10
|
});
|
11
|
-
|
11
|
+
const sorted = sortInner(madArray, ASC);
|
12
12
|
return quartileInner(sorted).Q2;
|
13
13
|
};
|
package/src/compare/main.js
CHANGED
@@ -1,24 +1,22 @@
|
|
1
1
|
import { bi2s, s2bi } from "../shared/utils.js";
|
2
2
|
import { isEqualInner, isLeftGreaterInner, maxInner, minInner, } from "./utils.js";
|
3
3
|
/** This function returns max value. */
|
4
|
-
export
|
5
|
-
|
6
|
-
|
4
|
+
export const max = (array) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bi, fpe] = maxInner(arrayInner);
|
7
7
|
return bi2s(bi, fpe);
|
8
8
|
};
|
9
9
|
/** This function returns min value. */
|
10
|
-
export
|
11
|
-
|
12
|
-
|
10
|
+
export const min = (array) => {
|
11
|
+
const arrayInner = array.map((str) => s2bi(str));
|
12
|
+
const [bi, fpe] = minInner(arrayInner);
|
13
13
|
return bi2s(bi, fpe);
|
14
14
|
};
|
15
15
|
/** This function returns if left value is greater than right value */
|
16
|
-
export
|
17
|
-
var left = _a.left, right = _a.right;
|
16
|
+
export const isLeftGreater = ({ left, right }) => {
|
18
17
|
return isLeftGreaterInner({ left: s2bi(left), right: s2bi(right) });
|
19
18
|
};
|
20
19
|
/** This function returns if left and right values are equal */
|
21
|
-
export
|
22
|
-
var left = _a.left, right = _a.right;
|
20
|
+
export const isEqual = ({ left, right }) => {
|
23
21
|
return isEqualInner({ left: s2bi(left), right: s2bi(right) });
|
24
22
|
};
|
package/src/compare/utils.js
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
export
|
2
|
-
|
3
|
-
for (
|
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
|
10
|
-
|
11
|
-
for (
|
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
|
18
|
-
|
19
|
-
|
17
|
+
export const compareInner = (l, r) => {
|
18
|
+
const [biL, fpeL] = l;
|
19
|
+
const [biR, fpeR] = r;
|
20
20
|
if (biL > 0n && biR < 0n)
|
21
21
|
return [l, 1];
|
22
22
|
if (biL < 0n && biR > 0n)
|
@@ -36,11 +36,11 @@ export var compareInner = function (l, r) {
|
|
36
36
|
if (fpeL === fpeR)
|
37
37
|
return [l, 0];
|
38
38
|
}
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
44
|
if (powL > powR)
|
45
45
|
return [l, 1];
|
46
46
|
if (powL < powR)
|
@@ -55,11 +55,11 @@ export var compareInner = function (l, r) {
|
|
55
55
|
if (fpeL === fpeR)
|
56
56
|
return [l, 0];
|
57
57
|
}
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
63
|
if (powL > powR)
|
64
64
|
return [l, 1];
|
65
65
|
if (powL < powR)
|
@@ -67,11 +67,9 @@ export var compareInner = function (l, r) {
|
|
67
67
|
}
|
68
68
|
return [l, 0];
|
69
69
|
};
|
70
|
-
export
|
71
|
-
var left = _a.left, right = _a.right;
|
70
|
+
export const isEqualInner = ({ left, right }) => {
|
72
71
|
return compareInner(left, right)[1] === 0;
|
73
72
|
};
|
74
|
-
export
|
75
|
-
var left = _a.left, right = _a.right;
|
73
|
+
export const isLeftGreaterInner = ({ left, right }) => {
|
76
74
|
return compareInner(left, right)[1] > 0;
|
77
75
|
};
|
package/src/mean/main.js
CHANGED
@@ -1,8 +1,8 @@
|
|
1
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
|
5
|
-
|
6
|
-
|
4
|
+
export const mean = (array) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bi, fpe] = meanInner(arrayInner);
|
7
7
|
return bi2s(bi, fpe);
|
8
8
|
};
|
package/src/mean/utils.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { PipeInner } from "../pipe/utils.js";
|
2
|
-
export
|
3
|
-
|
4
|
-
|
2
|
+
export const meanInner = (array) => {
|
3
|
+
const left = new PipeInner().add(array).calc();
|
4
|
+
const right = [BigInt(array.length), 0];
|
5
5
|
return new PipeInner().div([left, right]).calc();
|
6
6
|
};
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { bi2s, s2bi } from "../../shared/utils.js";
|
2
2
|
import { calcInner } from "../../shared/utils.js";
|
3
3
|
/** This function adds numbers (as string). */
|
4
|
-
export
|
5
|
-
|
6
|
-
|
4
|
+
export const add = (array) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bigInt, fpe] = calcInner(arrayInner, (a, b) => a + b);
|
7
7
|
return bi2s(bigInt, fpe);
|
8
8
|
};
|
@@ -1,9 +1,8 @@
|
|
1
1
|
import { bi2s, s2bi } from "../../shared/utils.js";
|
2
2
|
import { divInner } from "./utils.js";
|
3
3
|
/** This function should divide numbers (as string). */
|
4
|
-
export
|
5
|
-
|
6
|
-
|
7
|
-
var _a = divInner(arrayInner, limit !== null && limit !== void 0 ? limit : 20), bigInt = _a[0], fpe = _a[1];
|
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);
|
8
7
|
return bi2s(bigInt, fpe);
|
9
8
|
};
|
@@ -1,10 +1,10 @@
|
|
1
|
-
export
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
for (
|
6
|
-
|
7
|
-
|
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
8
|
if (bigInt === 0n && fpe === 0)
|
9
9
|
return [0n, 0];
|
10
10
|
if (fpe === dpLen) {
|
@@ -12,7 +12,7 @@ export var divInner = function (array, limit, def) {
|
|
12
12
|
dpLen = 0;
|
13
13
|
}
|
14
14
|
if (dpLen > 0 && fpe < dpLen) {
|
15
|
-
bigInt *=
|
15
|
+
bigInt *= 10n ** BigInt(dpLen - fpe);
|
16
16
|
fpe = 0;
|
17
17
|
}
|
18
18
|
if (dpLen > 0 && fpe > dpLen)
|
@@ -23,13 +23,13 @@ export var divInner = function (array, limit, def) {
|
|
23
23
|
fpe += 1;
|
24
24
|
bigInt *= 10n;
|
25
25
|
}
|
26
|
-
|
26
|
+
const q = bigInt / bigCurrent;
|
27
27
|
r = bigInt - q * bigCurrent;
|
28
28
|
bigInt = q;
|
29
29
|
while ((isNeg ? r < 0n : r > 0n) && fpe < limit) {
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
const nextBigInt = r * 10n;
|
31
|
+
const nextQ = nextBigInt / bigCurrent;
|
32
|
+
const nextRemained = nextBigInt - nextQ * bigCurrent;
|
33
33
|
bigInt = bigInt * 10n + nextQ;
|
34
34
|
r = nextRemained;
|
35
35
|
fpe += 1;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { bi2s, calcInner, s2bi } from "../../shared/utils.js";
|
2
2
|
/** This function multiplies numbers (as string). */
|
3
|
-
export
|
4
|
-
|
5
|
-
|
3
|
+
export const mul = (array) => {
|
4
|
+
const arrayInner = array.map((str) => s2bi(str));
|
5
|
+
const [bigInt, fpe] = calcInner(arrayInner, (a, b) => a * b);
|
6
6
|
return bi2s(bigInt, fpe);
|
7
7
|
};
|
@@ -2,7 +2,7 @@ import { bi2s, s2bi } from "../../shared/utils.js";
|
|
2
2
|
import { calcInner } from "../../shared/utils.js";
|
3
3
|
/** This function subtracts numbers (as string). */
|
4
4
|
export function sub(array) {
|
5
|
-
|
6
|
-
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const [bigInt, fpe] = calcInner(arrayInner, (a, b) => a - b);
|
7
7
|
return bi2s(bigInt, fpe);
|
8
8
|
}
|
package/src/pipe/main.js
CHANGED
@@ -12,41 +12,38 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
12
12
|
var _Pipe_result;
|
13
13
|
import { bi2s, calcInner, s2bi } from "../shared/utils.js";
|
14
14
|
import { divInner } from "../operations/div/utils.js";
|
15
|
-
|
16
|
-
|
15
|
+
export class Pipe {
|
16
|
+
constructor() {
|
17
17
|
_Pipe_result.set(this, void 0);
|
18
18
|
}
|
19
|
-
|
20
|
-
|
21
|
-
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner,
|
19
|
+
add(array) {
|
20
|
+
const arrayInner = array.map((str) => s2bi(str));
|
21
|
+
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, (a, b) => a + b, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
22
22
|
return this;
|
23
|
-
}
|
24
|
-
|
25
|
-
|
26
|
-
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner,
|
23
|
+
}
|
24
|
+
sub(array) {
|
25
|
+
const arrayInner = array.map((str) => s2bi(str));
|
26
|
+
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, (a, b) => a - b, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
27
27
|
return this;
|
28
|
-
}
|
29
|
-
|
30
|
-
|
31
|
-
var arrayInner = array.map(function (str) { return s2bi(str); });
|
28
|
+
}
|
29
|
+
div(array, limit = 20) {
|
30
|
+
const arrayInner = array.map((str) => s2bi(str));
|
32
31
|
__classPrivateFieldSet(this, _Pipe_result, divInner(arrayInner, limit, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
33
32
|
return this;
|
34
|
-
}
|
35
|
-
|
36
|
-
|
37
|
-
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner,
|
33
|
+
}
|
34
|
+
mul(array) {
|
35
|
+
const arrayInner = array.map((str) => s2bi(str));
|
36
|
+
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, (a, b) => a * b, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
38
37
|
return this;
|
39
|
-
}
|
40
|
-
|
38
|
+
}
|
39
|
+
calc() {
|
41
40
|
var _a;
|
42
|
-
|
43
|
-
|
41
|
+
const [bi, fpe] = (_a = __classPrivateFieldGet(this, _Pipe_result, "f")) !== null && _a !== void 0 ? _a : [0n, 0];
|
42
|
+
const result = bi2s(bi, fpe);
|
44
43
|
__classPrivateFieldSet(this, _Pipe_result, undefined, "f");
|
45
44
|
return result;
|
46
|
-
}
|
47
|
-
|
48
|
-
}());
|
49
|
-
export { Pipe };
|
45
|
+
}
|
46
|
+
}
|
50
47
|
_Pipe_result = new WeakMap();
|
51
48
|
/** Using this function you can chain operations (add, sub, div, mul). */
|
52
|
-
export
|
49
|
+
export const pipe = new Pipe();
|
package/src/pipe/utils.js
CHANGED
@@ -1,30 +1,26 @@
|
|
1
1
|
import { calcInner } from "../shared/utils.js";
|
2
2
|
import { divInner } from "../operations/div/utils.js";
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
this.result = calcInner(array, function (a, b) { return a + b; }, this.result);
|
3
|
+
export class PipeInner {
|
4
|
+
constructor() { }
|
5
|
+
add(array) {
|
6
|
+
this.result = calcInner(array, (a, b) => a + b, this.result);
|
8
7
|
return this;
|
9
|
-
}
|
10
|
-
|
11
|
-
this.result = calcInner(array,
|
8
|
+
}
|
9
|
+
sub(array) {
|
10
|
+
this.result = calcInner(array, (a, b) => a - b, this.result);
|
12
11
|
return this;
|
13
|
-
}
|
14
|
-
|
15
|
-
if (limit === void 0) { limit = 20; }
|
12
|
+
}
|
13
|
+
div(array, limit = 20) {
|
16
14
|
this.result = divInner(array, limit, this.result);
|
17
15
|
return this;
|
18
|
-
}
|
19
|
-
|
20
|
-
this.result = calcInner(array,
|
16
|
+
}
|
17
|
+
mul(array) {
|
18
|
+
this.result = calcInner(array, (a, b) => a * b, this.result);
|
21
19
|
return this;
|
22
|
-
}
|
23
|
-
|
20
|
+
}
|
21
|
+
calc() {
|
24
22
|
var _a;
|
25
|
-
|
23
|
+
const res = (_a = this.result) !== null && _a !== void 0 ? _a : [0n, 0];
|
26
24
|
return res;
|
27
|
-
}
|
28
|
-
|
29
|
-
}());
|
30
|
-
export { PipeInner };
|
25
|
+
}
|
26
|
+
}
|
package/src/quartile/main.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
import { bi2s, s2bi } from "../shared/utils.js";
|
2
2
|
import { quartileInner } from "./utils.js";
|
3
3
|
/** This function returns Q1, Q2, Q3 (quartile). */
|
4
|
-
export
|
5
|
-
|
6
|
-
|
4
|
+
export const quartile = (array) => {
|
5
|
+
const arrayInner = array.map((str) => s2bi(str));
|
6
|
+
const { Q1: [Q1bi, Q1fpe], Q2: [Q2bi, Q2fpe], Q3: [Q3bi, Q3fpe] } = quartileInner(arrayInner);
|
7
7
|
return {
|
8
8
|
Q1: bi2s(Q1bi, Q1fpe),
|
9
9
|
Q2: bi2s(Q2bi, Q2fpe),
|
package/src/quartile/utils.js
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
import { PipeInner } from "../pipe/utils.js";
|
2
|
-
|
3
|
-
|
2
|
+
const meanQ = (idx, array) => {
|
3
|
+
const left = new PipeInner().add([array[idx], array[idx - 1]]).calc();
|
4
4
|
return new PipeInner().div([left, [2n, 0]]).calc();
|
5
5
|
};
|
6
|
-
export
|
6
|
+
export const quartileInner = (array) => {
|
7
7
|
if (array.length < 3) {
|
8
8
|
throw Error("To calculate quartile you need at least 3 elements");
|
9
9
|
}
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
const { length } = array;
|
11
|
+
const Q2Idx = length >> 1;
|
12
|
+
const Q1Idx = Q2Idx >> 1;
|
13
|
+
const Q3Idx = length - Q1Idx;
|
14
|
+
const isEvenHalf = Q2Idx % 2 !== 0;
|
15
15
|
return {
|
16
16
|
Q1: isEvenHalf ? array[Q1Idx] : meanQ(Q1Idx, array),
|
17
17
|
Q2: length % 2 !== 0 ? array[Q2Idx] : meanQ(Q2Idx, array),
|
package/src/round/constants.js
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
export
|
2
|
-
export
|
3
|
-
export
|
4
|
-
export
|
5
|
-
export
|
6
|
-
export
|
7
|
-
export
|
1
|
+
export const UP = "up";
|
2
|
+
export const DOWN = "down";
|
3
|
+
export const HALF_UP = "half-up";
|
4
|
+
export const HALF_DOWN = "half-down";
|
5
|
+
export const HALF_EVEN = "half-even";
|
6
|
+
export const HALF_ODD = "half-odd";
|
7
|
+
export const ROUND_MODE = [
|
8
8
|
UP,
|
9
9
|
DOWN,
|
10
10
|
HALF_UP,
|
package/src/round/main.js
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
import { HALF_UP } from "./constants.js";
|
2
2
|
import { calcLast, handleCarryOver, handleTail } from "./utils.js";
|
3
3
|
/** This function round number. */
|
4
|
-
export
|
5
|
-
var _a;
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
var charCode = value.charCodeAt(i);
|
4
|
+
export const round = (value, options) => {
|
5
|
+
var _a, _b, _c, _d;
|
6
|
+
const roundMode = (_a = options === null || options === void 0 ? void 0 : options.roundMode) !== null && _a !== void 0 ? _a : HALF_UP;
|
7
|
+
let array = [];
|
8
|
+
let sigFig = (_b = options === null || options === void 0 ? void 0 : options.sigFig) !== null && _b !== void 0 ? _b : false;
|
9
|
+
let decimals = (_c = options === null || options === void 0 ? void 0 : options.decimals) !== null && _c !== void 0 ? _c : 0;
|
10
|
+
let isFloat = false;
|
11
|
+
let isNil = true;
|
12
|
+
for (let i = 0; i < value.length; i++) {
|
13
|
+
const charCode = value.charCodeAt(i);
|
15
14
|
if (sigFig && charCode > 48)
|
16
15
|
sigFig = false;
|
17
16
|
if (isNil && charCode > 48)
|
@@ -21,14 +20,14 @@ export var round = function (value, options) {
|
|
21
20
|
if (charCode === 46)
|
22
21
|
isFloat = true;
|
23
22
|
if (isFloat && decimals === 0) {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
const next = (_d = value.charCodeAt(i + 1)) !== null && _d !== void 0 ? _d : 0;
|
24
|
+
const prev = value.charCodeAt(i - 1);
|
25
|
+
const curr = charCode === 46 ? prev : charCode;
|
26
|
+
const [last, hasCarryOver] = calcLast(curr, next, roundMode);
|
28
27
|
if (charCode === 46)
|
29
28
|
isFloat = false;
|
30
29
|
if (hasCarryOver) {
|
31
|
-
|
30
|
+
[array, isFloat] = handleCarryOver(array, isFloat);
|
32
31
|
break;
|
33
32
|
}
|
34
33
|
charCode === 46
|
@@ -41,5 +40,5 @@ export var round = function (value, options) {
|
|
41
40
|
handleTail(array, isFloat);
|
42
41
|
return (isNil && array[array.length - 1] <= 48)
|
43
42
|
? "0"
|
44
|
-
: String.fromCharCode
|
43
|
+
: String.fromCharCode(...array);
|
45
44
|
};
|
package/src/round/utils.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { DOWN, HALF_DOWN, HALF_EVEN, HALF_ODD, HALF_UP, UP, } from "./constants.js";
|
2
|
-
export
|
3
|
-
|
2
|
+
export const handleTail = (array, isFloat) => {
|
3
|
+
let lastIdx = array.length - 1;
|
4
4
|
while (isFloat && array[lastIdx] <= 48) {
|
5
5
|
if (array[lastIdx] === 46 || array.length === 1)
|
6
6
|
isFloat = false;
|
@@ -8,53 +8,52 @@ export var handleTail = function (array, isFloat) {
|
|
8
8
|
lastIdx -= 1;
|
9
9
|
}
|
10
10
|
};
|
11
|
-
export
|
11
|
+
export const handleCarryOver = (array, isFloat) => {
|
12
12
|
var _a;
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
[57,
|
13
|
+
let hasCarryOver = true;
|
14
|
+
let idx = array.length - 1;
|
15
|
+
const mapFloat = new Map([
|
16
|
+
[57, () => {
|
17
17
|
array.pop();
|
18
18
|
idx -= 1;
|
19
19
|
}],
|
20
|
-
[46,
|
20
|
+
[46, () => {
|
21
21
|
array.pop();
|
22
22
|
idx -= 1;
|
23
23
|
isFloat = false;
|
24
24
|
}],
|
25
|
-
[0,
|
25
|
+
[0, () => {
|
26
26
|
array[idx] += 1;
|
27
27
|
hasCarryOver = false;
|
28
28
|
}],
|
29
29
|
]);
|
30
|
-
|
31
|
-
[57,
|
30
|
+
const mapInt = new Map([
|
31
|
+
[57, () => {
|
32
32
|
array[idx] = 48;
|
33
33
|
idx -= 1;
|
34
34
|
}],
|
35
|
-
[0,
|
35
|
+
[0, () => {
|
36
36
|
array[idx] += 1;
|
37
37
|
hasCarryOver = false;
|
38
38
|
}],
|
39
39
|
]);
|
40
40
|
while (hasCarryOver && idx >= 0) {
|
41
|
-
|
42
|
-
|
41
|
+
const map = isFloat ? mapFloat : mapInt;
|
42
|
+
const key = map.has(array[idx]) ? array[idx] : 0;
|
43
43
|
(_a = map.get(key)) === null || _a === void 0 ? void 0 : _a();
|
44
44
|
}
|
45
45
|
hasCarryOver && array.unshift(49);
|
46
46
|
return [array, isFloat];
|
47
47
|
};
|
48
|
-
export
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
_a);
|
48
|
+
export const calcLast = (current, next, roundMode) => {
|
49
|
+
const isEven = current % 2 === 1;
|
50
|
+
const map = {
|
51
|
+
[UP]: current + (next > 48 ? 1 : 0),
|
52
|
+
[DOWN]: current,
|
53
|
+
[HALF_UP]: current + (next >= 53 ? 1 : 0),
|
54
|
+
[HALF_DOWN]: current + (next > 53 ? 1 : 0),
|
55
|
+
[HALF_EVEN]: current + ((isEven && next >= 53) ? 1 : 0),
|
56
|
+
[HALF_ODD]: current + ((!isEven && next >= 53) ? 1 : 0),
|
57
|
+
};
|
59
58
|
return map[roundMode] > 57 ? [48, true] : [map[roundMode], false];
|
60
59
|
};
|
package/src/shared/constant.js
CHANGED
package/src/shared/utils.js
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
export
|
1
|
+
export const bi2s = (bigInt, fpe) => {
|
2
2
|
if (bigInt === 0n)
|
3
3
|
return "0";
|
4
|
-
|
4
|
+
const isNeg = bigInt < 0n;
|
5
5
|
isNeg && (bigInt *= -1n);
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
const dp = bigInt % (10n ** BigInt(fpe));
|
7
|
+
const bigStr = bigInt.toString();
|
8
|
+
let fpIdx = bigStr.length - fpe;
|
9
9
|
if (fpIdx < 0)
|
10
10
|
fpIdx = 0;
|
11
|
-
|
12
|
-
|
11
|
+
const before = bigStr.slice(0, fpIdx);
|
12
|
+
const after = bigStr.slice(fpIdx);
|
13
13
|
if (before) {
|
14
14
|
return fillHead(bigStr.length, fpe, isNeg, true) +
|
15
|
-
(fpe > 0 && dp > 0 ? trimTail(
|
15
|
+
(fpe > 0 && dp > 0 ? trimTail(`${before}.${after}`) : before);
|
16
16
|
}
|
17
|
-
return
|
17
|
+
return `${fillHead(bigStr.length, fpe, isNeg, false)}${trimTail(after)}`;
|
18
18
|
};
|
19
|
-
export
|
20
|
-
|
19
|
+
export const s2bi = (str) => {
|
20
|
+
const fpi = str.indexOf(".");
|
21
21
|
if (fpi === -1)
|
22
22
|
return [BigInt(str), 0];
|
23
23
|
return [
|
@@ -25,12 +25,12 @@ export var s2bi = function (str) {
|
|
25
25
|
fpi === -1 ? 0 : str.length - 1 - fpi,
|
26
26
|
];
|
27
27
|
};
|
28
|
-
export
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
for (
|
33
|
-
|
28
|
+
export const calcInner = (array, op, def) => {
|
29
|
+
let bigInt = def ? def[0] : array[0][0];
|
30
|
+
let fpe = def ? def[1] : array[0][1];
|
31
|
+
const opm = op(1n, 1n);
|
32
|
+
for (let i = def ? 0 : 1; i < array.length; i++) {
|
33
|
+
const [bigCurrent, dpLen] = array[i];
|
34
34
|
if (dpLen === 0 && fpe === 0) {
|
35
35
|
bigInt = op(bigInt, bigCurrent);
|
36
36
|
continue;
|
@@ -41,11 +41,11 @@ export var calcInner = function (array, op, def) {
|
|
41
41
|
}
|
42
42
|
else {
|
43
43
|
if (fpe < dpLen) {
|
44
|
-
|
45
|
-
bigInt = op(bigInt * (
|
44
|
+
const fpDiff = dpLen - fpe;
|
45
|
+
bigInt = op(bigInt * (10n ** BigInt(fpDiff)), bigCurrent);
|
46
46
|
}
|
47
47
|
if (fpe > dpLen) {
|
48
|
-
bigInt = op(bigInt, bigCurrent * (
|
48
|
+
bigInt = op(bigInt, bigCurrent * (10n ** BigInt(fpe - dpLen)));
|
49
49
|
}
|
50
50
|
if (fpe === dpLen)
|
51
51
|
bigInt = op(bigInt, bigCurrent);
|
@@ -55,19 +55,19 @@ export var calcInner = function (array, op, def) {
|
|
55
55
|
}
|
56
56
|
return [bigInt, fpe];
|
57
57
|
};
|
58
|
-
export
|
59
|
-
|
58
|
+
export const fillHead = (len, fpe, isNeg, hasBefore) => {
|
59
|
+
let head = (isNeg ? "-" : "") + (hasBefore ? "" : "0.");
|
60
60
|
while (len < fpe) {
|
61
61
|
head = head + "0";
|
62
62
|
len += 1;
|
63
63
|
}
|
64
64
|
return head;
|
65
65
|
};
|
66
|
-
export
|
66
|
+
export const trimTail = (str) => {
|
67
67
|
if (str[str.length - 1] !== "0")
|
68
68
|
return str;
|
69
|
-
|
70
|
-
for (
|
69
|
+
let count = 0;
|
70
|
+
for (let i = str.length - 1; i >= 0; i -= 1) {
|
71
71
|
if (str[i] !== "0")
|
72
72
|
return str.slice(0, str.length - count);
|
73
73
|
count += 1;
|
package/src/sort/constants.js
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
export
|
2
|
-
export
|
3
|
-
export
|
1
|
+
export const ASC = "asc";
|
2
|
+
export const DESC = "desc";
|
3
|
+
export const sortingArray = [ASC, DESC];
|
package/src/sort/main.js
CHANGED
@@ -2,12 +2,8 @@ import { bi2s, s2bi } from "../shared/utils.js";
|
|
2
2
|
import { ASC } from "./constants.js";
|
3
3
|
import { sortInner } from "./utils.js";
|
4
4
|
/** Using this function sort an array. */
|
5
|
-
export
|
6
|
-
|
7
|
-
var arrayInner = array.map(function (str) { return s2bi(str); });
|
5
|
+
export const sort = (array, sorting = ASC) => {
|
6
|
+
const arrayInner = array.map((str) => s2bi(str));
|
8
7
|
sortInner(arrayInner, sorting);
|
9
|
-
return arrayInner.map(
|
10
|
-
var bi = _a[0], fpe = _a[1];
|
11
|
-
return bi2s(bi, fpe);
|
12
|
-
});
|
8
|
+
return arrayInner.map(([bi, fpe]) => bi2s(bi, fpe));
|
13
9
|
};
|
package/src/sort/utils.js
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
import { compareInner } from "../compare/utils.js";
|
2
2
|
import { ASC } from "./constants.js";
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
var idx = i;
|
3
|
+
const heapify = (array, len, i, sorting) => {
|
4
|
+
const idxL = 2 * i + 1;
|
5
|
+
const idxR = idxL + 1;
|
6
|
+
let idx = i;
|
8
7
|
if (sorting === ASC) {
|
9
8
|
if (idxL < len && compareInner(array[idxL], array[idx])[1] >= 0)
|
10
9
|
idx = idxL;
|
@@ -18,18 +17,17 @@ var heapify = function (array, len, i, sorting) {
|
|
18
17
|
idx = idxR;
|
19
18
|
}
|
20
19
|
if (idx !== i) {
|
21
|
-
|
20
|
+
[array[i], array[idx]] = [array[idx], array[i]];
|
22
21
|
heapify(array, len, idx, sorting);
|
23
22
|
}
|
24
23
|
};
|
25
|
-
export
|
26
|
-
|
27
|
-
|
28
|
-
for (var i = (len >> 1) - 1; i >= 0; i--) {
|
24
|
+
export const sortInner = (array, sorting) => {
|
25
|
+
const { length: len } = array;
|
26
|
+
for (let i = (len >> 1) - 1; i >= 0; i--) {
|
29
27
|
heapify(array, len, i, sorting);
|
30
28
|
}
|
31
|
-
for (
|
32
|
-
|
29
|
+
for (let i = len - 1; i >= 0; i--) {
|
30
|
+
[array[0], array[i]] = [array[i], array[0]];
|
33
31
|
heapify(array, i, 0, sorting);
|
34
32
|
}
|
35
33
|
return array;
|
package/src/sqrt/main.js
CHANGED
@@ -2,9 +2,9 @@ import { round } from "../round/main.js";
|
|
2
2
|
import { bi2s, s2bi } from "../shared/utils.js";
|
3
3
|
import { sqrtInner } from "./utils.js";
|
4
4
|
/** Find square root of a number */
|
5
|
-
export
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
return round(outputStr, { decimals
|
5
|
+
export const sqrt = (str, precision) => {
|
6
|
+
const inputInner = s2bi(str);
|
7
|
+
const [[bi, fpe], decimals] = sqrtInner(inputInner, precision ? s2bi(precision) : undefined);
|
8
|
+
const outputStr = bi2s(bi, fpe);
|
9
|
+
return round(outputStr, { decimals });
|
10
10
|
};
|
package/src/sqrt/utils.js
CHANGED
@@ -1,18 +1,16 @@
|
|
1
1
|
import { isLeftGreaterInner } from "../compare/utils.js";
|
2
2
|
import { PipeInner } from "../pipe/utils.js";
|
3
|
-
|
4
|
-
|
5
|
-
export
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
for (var i = 0; i < max; i++) {
|
10
|
-
var nextGuess = new PipeInner()
|
3
|
+
const halfInner = [5n, 1];
|
4
|
+
const precDef = [1n, 13];
|
5
|
+
export const sqrtInner = (input, prec = precDef, max = 100) => {
|
6
|
+
let guess = input;
|
7
|
+
for (let i = 0; i < max; i++) {
|
8
|
+
const nextGuess = new PipeInner()
|
11
9
|
.div([input, guess])
|
12
10
|
.add([guess])
|
13
11
|
.mul([halfInner])
|
14
12
|
.calc();
|
15
|
-
|
13
|
+
let [bi, fpe] = new PipeInner().sub([nextGuess, guess]).calc();
|
16
14
|
if (bi < 0n)
|
17
15
|
bi *= -1n;
|
18
16
|
if (isLeftGreaterInner({ left: prec, right: [bi, fpe] })) {
|