@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/src/pipe/main.js
CHANGED
@@ -1,46 +1,47 @@
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
5
|
+
};
|
1
6
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
2
7
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
3
8
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
4
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
5
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
6
11
|
};
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11
|
-
};
|
12
12
|
var _Pipe_result;
|
13
|
-
import {
|
14
|
-
import {
|
15
|
-
import { subRoute } from "../operations/sub/utils.js";
|
16
|
-
import { addRoute } from "../operations/add/utils.js";
|
17
|
-
import { divRoute } from "../operations/div/utils.js";
|
18
|
-
import { mulRoute } from "../operations/mul/utils.js";
|
13
|
+
import { bi2s, calcInner, s2bi } from "../shared/utils.js";
|
14
|
+
import { divInner } from "../operations/div/utils.js";
|
19
15
|
var Pipe = /** @class */ (function () {
|
20
16
|
function Pipe() {
|
21
17
|
_Pipe_result.set(this, void 0);
|
22
|
-
__classPrivateFieldSet(this, _Pipe_result, DEFAULT, "f");
|
23
18
|
}
|
24
|
-
Pipe.prototype.add = function (
|
25
|
-
|
19
|
+
Pipe.prototype.add = function (array) {
|
20
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
21
|
+
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, function (a, b) { return a + b; }, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
26
22
|
return this;
|
27
23
|
};
|
28
|
-
Pipe.prototype.sub = function (
|
29
|
-
|
24
|
+
Pipe.prototype.sub = function (array) {
|
25
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
26
|
+
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, function (a, b) { return a - b; }, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
30
27
|
return this;
|
31
28
|
};
|
32
|
-
Pipe.prototype.div = function (
|
29
|
+
Pipe.prototype.div = function (array, limit) {
|
33
30
|
if (limit === void 0) { limit = 20; }
|
34
|
-
|
31
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
32
|
+
__classPrivateFieldSet(this, _Pipe_result, divInner(arrayInner, limit, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
35
33
|
return this;
|
36
34
|
};
|
37
|
-
Pipe.prototype.mul = function (
|
38
|
-
|
35
|
+
Pipe.prototype.mul = function (array) {
|
36
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
37
|
+
__classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, function (a, b) { return a * b; }, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
|
39
38
|
return this;
|
40
39
|
};
|
41
40
|
Pipe.prototype.calc = function () {
|
42
|
-
var
|
43
|
-
|
41
|
+
var _a;
|
42
|
+
var _b = (_a = __classPrivateFieldGet(this, _Pipe_result, "f")) !== null && _a !== void 0 ? _a : [0n, 0], bi = _b[0], fpe = _b[1];
|
43
|
+
var result = bi2s(bi, fpe);
|
44
|
+
__classPrivateFieldSet(this, _Pipe_result, undefined, "f");
|
44
45
|
return result;
|
45
46
|
};
|
46
47
|
return Pipe;
|
package/src/pipe/utils.d.ts
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
import type {
|
2
|
-
import type { InputData } from "../types.d.ts";
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
3
2
|
export declare class PipeInner {
|
4
|
-
result:
|
3
|
+
result: BI | undefined;
|
5
4
|
constructor();
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
calc():
|
5
|
+
add(array: BI[]): PipeInner;
|
6
|
+
sub(array: BI[]): PipeInner;
|
7
|
+
div(array: BI[], limit?: number): PipeInner;
|
8
|
+
mul(array: BI[]): PipeInner;
|
9
|
+
calc(): BI;
|
11
10
|
}
|
package/src/pipe/utils.js
CHANGED
@@ -1,33 +1,29 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
import { subRoute } from "../operations/sub/utils.js";
|
4
|
-
import { addRoute } from "../operations/add/utils.js";
|
5
|
-
import { divRoute } from "../operations/div/utils.js";
|
6
|
-
import { mulRoute } from "../operations/mul/utils.js";
|
1
|
+
import { calcInner } from "../shared/utils.js";
|
2
|
+
import { divInner } from "../operations/div/utils.js";
|
7
3
|
var PipeInner = /** @class */ (function () {
|
8
4
|
function PipeInner() {
|
9
|
-
this.result = DEFAULT;
|
10
5
|
}
|
11
|
-
PipeInner.prototype.
|
12
|
-
this.result =
|
6
|
+
PipeInner.prototype.add = function (array) {
|
7
|
+
this.result = calcInner(array, function (a, b) { return a + b; }, this.result);
|
13
8
|
return this;
|
14
9
|
};
|
15
|
-
PipeInner.prototype.
|
16
|
-
this.result =
|
10
|
+
PipeInner.prototype.sub = function (array) {
|
11
|
+
this.result = calcInner(array, function (a, b) { return a - b; }, this.result);
|
17
12
|
return this;
|
18
13
|
};
|
19
|
-
PipeInner.prototype.
|
20
|
-
|
14
|
+
PipeInner.prototype.div = function (array, limit) {
|
15
|
+
if (limit === void 0) { limit = 20; }
|
16
|
+
this.result = divInner(array, limit, this.result);
|
21
17
|
return this;
|
22
18
|
};
|
23
|
-
PipeInner.prototype.
|
24
|
-
this.result =
|
19
|
+
PipeInner.prototype.mul = function (array) {
|
20
|
+
this.result = calcInner(array, function (a, b) { return a * b; }, this.result);
|
25
21
|
return this;
|
26
22
|
};
|
27
23
|
PipeInner.prototype.calc = function () {
|
28
|
-
var
|
29
|
-
this.result
|
30
|
-
return
|
24
|
+
var _a;
|
25
|
+
var res = (_a = this.result) !== null && _a !== void 0 ? _a : [0n, 0];
|
26
|
+
return res;
|
31
27
|
};
|
32
28
|
return PipeInner;
|
33
29
|
}());
|
package/src/quartile/main.js
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
import {
|
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 var quartile = function (
|
5
|
-
var
|
6
|
-
var
|
4
|
+
export var quartile = function (array) {
|
5
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
6
|
+
var _a = quartileInner(arrayInner), _b = _a.Q1, Q1bi = _b[0], Q1fpe = _b[1], _c = _a.Q2, Q2bi = _c[0], Q2fpe = _c[1], _d = _a.Q3, Q3bi = _d[0], Q3fpe = _d[1];
|
7
7
|
return {
|
8
|
-
Q1:
|
9
|
-
Q2:
|
10
|
-
Q3:
|
8
|
+
Q1: bi2s(Q1bi, Q1fpe),
|
9
|
+
Q2: bi2s(Q2bi, Q2fpe),
|
10
|
+
Q3: bi2s(Q3bi, Q3fpe),
|
11
11
|
};
|
12
12
|
};
|
package/src/quartile/types.d.ts
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
2
2
|
export type Quartile = (array: string[]) => {
|
3
3
|
Q1: string;
|
4
4
|
Q2: string;
|
5
5
|
Q3: string;
|
6
6
|
};
|
7
|
-
export type MeanQ = (index: number, array:
|
8
|
-
export type QuartileInner = (array:
|
9
|
-
Q1:
|
10
|
-
Q2:
|
11
|
-
Q3:
|
7
|
+
export type MeanQ = (index: number, array: BI[]) => BI;
|
8
|
+
export type QuartileInner = (array: BI[]) => {
|
9
|
+
Q1: BI;
|
10
|
+
Q2: BI;
|
11
|
+
Q3: BI;
|
12
12
|
};
|
package/src/quartile/utils.js
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
import { PipeInner } from "../pipe/utils.js";
|
2
|
-
import { cloneInner } from "../shared/utils.js";
|
3
2
|
var meanQ = function (idx, array) {
|
4
|
-
|
5
|
-
|
6
|
-
cloneInner(array[idx - 1]),
|
7
|
-
]).divInner([{
|
8
|
-
array: [50],
|
9
|
-
intLength: 1,
|
10
|
-
isFloat: false,
|
11
|
-
isNegative: false,
|
12
|
-
}]).result;
|
3
|
+
var left = new PipeInner().add([array[idx], array[idx - 1]]).calc();
|
4
|
+
return new PipeInner().div([left, [2n, 0]]).calc();
|
13
5
|
};
|
14
6
|
export var quartileInner = function (array) {
|
15
7
|
if (array.length < 3) {
|
package/src/shared/constant.d.ts
CHANGED
@@ -1,18 +1,6 @@
|
|
1
|
-
export declare const DEFAULT: {
|
2
|
-
array: any[];
|
3
|
-
intLength: number;
|
4
|
-
isFloat: boolean;
|
5
|
-
isNegative: boolean;
|
6
|
-
};
|
7
1
|
export declare const NIL: {
|
8
2
|
array: number[];
|
9
3
|
isFloat: boolean;
|
10
4
|
isNegative: boolean;
|
11
5
|
intLength: number;
|
12
6
|
};
|
13
|
-
export declare const ONE: {
|
14
|
-
array: number[];
|
15
|
-
isFloat: boolean;
|
16
|
-
isNegative: boolean;
|
17
|
-
intLength: number;
|
18
|
-
};
|
package/src/shared/constant.js
CHANGED
@@ -1,18 +1,6 @@
|
|
1
|
-
export var DEFAULT = {
|
2
|
-
array: [],
|
3
|
-
intLength: 0,
|
4
|
-
isFloat: false,
|
5
|
-
isNegative: false,
|
6
|
-
};
|
7
1
|
export var NIL = {
|
8
2
|
array: [48],
|
9
3
|
isFloat: false,
|
10
4
|
isNegative: false,
|
11
5
|
intLength: 1,
|
12
6
|
};
|
13
|
-
export var ONE = {
|
14
|
-
array: [49],
|
15
|
-
isFloat: false,
|
16
|
-
isNegative: false,
|
17
|
-
intLength: 1,
|
18
|
-
};
|
package/src/shared/types.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
|
2
|
-
export type
|
3
|
-
export type
|
4
|
-
export type
|
5
|
-
export type
|
1
|
+
export type BI = [bigint, number];
|
2
|
+
export type FillHead = (len: number, fpe: number, isNeg: boolean, hasBefore: boolean) => string;
|
3
|
+
export type TrimTail = (str: string) => string;
|
4
|
+
export type CalcInner = (array: BI[], op: (a: bigint, b: bigint) => bigint, def?: BI) => BI;
|
5
|
+
export type BI2S = (bigInt: bigint, fpe: number) => string;
|
6
|
+
export type S2BI = (str: string) => BI;
|
package/src/shared/utils.d.ts
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
import type {
|
2
|
-
export declare const
|
3
|
-
export declare const
|
4
|
-
export declare const
|
1
|
+
import type { BI2S, CalcInner, FillHead, S2BI, TrimTail } from "./types.d.ts";
|
2
|
+
export declare const bi2s: BI2S;
|
3
|
+
export declare const s2bi: S2BI;
|
4
|
+
export declare const calcInner: CalcInner;
|
5
|
+
export declare const fillHead: FillHead;
|
6
|
+
export declare const trimTail: TrimTail;
|
package/src/shared/utils.js
CHANGED
@@ -1,83 +1,76 @@
|
|
1
|
-
var
|
2
|
-
|
1
|
+
export var bi2s = function (bigInt, fpe) {
|
2
|
+
if (bigInt === 0n)
|
3
|
+
return "0";
|
4
|
+
var isNeg = bigInt < 0n;
|
5
|
+
isNeg && (bigInt *= -1n);
|
6
|
+
var dp = bigInt % (Math.pow(10n, BigInt(fpe)));
|
7
|
+
var bigStr = bigInt.toString();
|
8
|
+
var fpIdx = bigStr.length - fpe;
|
9
|
+
if (fpIdx < 0)
|
10
|
+
fpIdx = 0;
|
11
|
+
var before = bigStr.slice(0, fpIdx);
|
12
|
+
var after = bigStr.slice(fpIdx);
|
13
|
+
if (before) {
|
14
|
+
return fillHead(bigStr.length, fpe, isNeg, true) +
|
15
|
+
(fpe > 0 && dp > 0 ? trimTail("".concat(before, ".").concat(after)) : before);
|
16
|
+
}
|
17
|
+
return "".concat(fillHead(bigStr.length, fpe, isNeg, false)).concat(trimTail(after));
|
18
|
+
};
|
19
|
+
export var s2bi = function (str) {
|
20
|
+
var fpi = str.indexOf(".");
|
21
|
+
if (fpi === -1)
|
22
|
+
return [BigInt(str), 0];
|
23
|
+
return [
|
24
|
+
BigInt(str.slice(0, fpi) + str.slice(fpi + 1)),
|
25
|
+
fpi === -1 ? 0 : str.length - 1 - fpi,
|
26
|
+
];
|
3
27
|
};
|
4
|
-
export var
|
5
|
-
var
|
6
|
-
var
|
7
|
-
var
|
8
|
-
var
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
while (array[idx] === 48 && idx >= intLength) {
|
14
|
-
lastValuableIdx = idx;
|
15
|
-
idx -= 1;
|
28
|
+
export var calcInner = function (array, op, def) {
|
29
|
+
var bigInt = def ? def[0] : array[0][0];
|
30
|
+
var fpe = def ? def[1] : array[0][1];
|
31
|
+
var opm = op(1n, 1n);
|
32
|
+
for (var i = def ? 0 : 1; i < array.length; i++) {
|
33
|
+
var _a = array[i], bigCurrent = _a[0], dpLen = _a[1];
|
34
|
+
if (dpLen === 0 && fpe === 0) {
|
35
|
+
bigInt = op(bigInt, bigCurrent);
|
36
|
+
continue;
|
16
37
|
}
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
result.push(48, 46);
|
21
|
-
for (var i = intLength; i < 0; i++) {
|
22
|
-
result.push(48);
|
23
|
-
}
|
24
|
-
for (var i = 0; i < lastValuableIdx; i++) {
|
25
|
-
result.push(array[i]);
|
26
|
-
}
|
38
|
+
if (opm === 1n) {
|
39
|
+
bigInt = op(bigInt, bigCurrent);
|
40
|
+
fpe += dpLen;
|
27
41
|
}
|
28
42
|
else {
|
29
|
-
|
30
|
-
|
31
|
-
|
43
|
+
if (fpe < dpLen) {
|
44
|
+
var fpDiff = dpLen - fpe;
|
45
|
+
bigInt = op(bigInt * (Math.pow(10n, BigInt(fpDiff))), bigCurrent);
|
32
46
|
}
|
47
|
+
if (fpe > dpLen) {
|
48
|
+
bigInt = op(bigInt, bigCurrent * (Math.pow(10n, BigInt(fpe - dpLen))));
|
49
|
+
}
|
50
|
+
if (fpe === dpLen)
|
51
|
+
bigInt = op(bigInt, bigCurrent);
|
52
|
+
if (fpe < dpLen)
|
53
|
+
fpe = dpLen;
|
33
54
|
}
|
34
|
-
return result.at(-1) === 46 ? "0" : convert(isNegative, result);
|
35
55
|
}
|
36
|
-
return
|
56
|
+
return [bigInt, fpe];
|
37
57
|
};
|
38
|
-
export var
|
39
|
-
var
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
var isNil = string.length === 1 && string.charCodeAt(0) === 48;
|
44
|
-
var isNegNil = string.length === 2 && isNegative &&
|
45
|
-
string.charCodeAt(1) === 48;
|
46
|
-
if (isNil || isNegNil) {
|
47
|
-
return {
|
48
|
-
array: [48],
|
49
|
-
intLength: 1,
|
50
|
-
isNegative: isNegNil,
|
51
|
-
isFloat: false,
|
52
|
-
};
|
53
|
-
}
|
54
|
-
for (var idx = 0 + shift; idx < string.length; idx++) {
|
55
|
-
var charCode = string.charCodeAt(idx);
|
56
|
-
if (array.length === 0 && charCode === 48)
|
57
|
-
continue;
|
58
|
-
if (charCode === 46) {
|
59
|
-
dec = string.length - 1 - idx;
|
60
|
-
continue;
|
61
|
-
}
|
62
|
-
array.push(charCode);
|
58
|
+
export var fillHead = function (len, fpe, isNeg, hasBefore) {
|
59
|
+
var head = (isNeg ? "-" : "") + (hasBefore ? "" : "0.");
|
60
|
+
while (len < fpe) {
|
61
|
+
head = head + "0";
|
62
|
+
len += 1;
|
63
63
|
}
|
64
|
-
return
|
65
|
-
array: array,
|
66
|
-
intLength: array.length - dec,
|
67
|
-
isNegative: isNegative,
|
68
|
-
isFloat: dec > 0,
|
69
|
-
};
|
64
|
+
return head;
|
70
65
|
};
|
71
|
-
export var
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
66
|
+
export var trimTail = function (str) {
|
67
|
+
if (str[str.length - 1] !== "0")
|
68
|
+
return str;
|
69
|
+
var count = 0;
|
70
|
+
for (var i = str.length - 1; i >= 0; i -= 1) {
|
71
|
+
if (str[i] !== "0")
|
72
|
+
return str.slice(0, str.length - count);
|
73
|
+
count += 1;
|
76
74
|
}
|
77
|
-
return
|
78
|
-
intLength: inner.intLength,
|
79
|
-
isFloat: inner.isFloat,
|
80
|
-
isNegative: inner.isNegative,
|
81
|
-
array: clone,
|
82
|
-
};
|
75
|
+
return str;
|
83
76
|
};
|
package/src/sort/main.js
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
-
import {
|
1
|
+
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
5
|
export var sort = function (array, sorting) {
|
6
6
|
if (sorting === void 0) { sorting = ASC; }
|
7
|
-
var
|
8
|
-
sortInner(
|
9
|
-
return
|
7
|
+
var arrayInner = array.map(function (str) { return s2bi(str); });
|
8
|
+
sortInner(arrayInner, sorting);
|
9
|
+
return arrayInner.map(function (_a) {
|
10
|
+
var bi = _a[0], fpe = _a[1];
|
11
|
+
return bi2s(bi, fpe);
|
12
|
+
});
|
10
13
|
};
|
package/src/sort/types.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
2
2
|
import type { sortingArray } from "./constants.d.ts";
|
3
3
|
export type Sorting = typeof sortingArray[number];
|
4
4
|
export type Sort = (array: string[], sorting?: Sorting) => string[];
|
5
|
-
export type SortInner = (array:
|
6
|
-
export type Heapify = (array:
|
5
|
+
export type SortInner = (array: BI[], sorting: Sorting) => BI[];
|
6
|
+
export type Heapify = (array: BI[], len: number, i: number, sorting: Sorting) => void;
|
package/src/sqrt/main.js
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
import { round } from "../round/main.js";
|
2
|
-
import {
|
2
|
+
import { bi2s, s2bi } from "../shared/utils.js";
|
3
3
|
import { sqrtInner } from "./utils.js";
|
4
4
|
/** Find square root of a number */
|
5
5
|
export var sqrt = function (str, precision) {
|
6
|
-
var inputInner =
|
7
|
-
var _a = sqrtInner(inputInner, precision ?
|
8
|
-
var outputStr =
|
9
|
-
return round(outputStr, { decimals: decimals
|
6
|
+
var inputInner = s2bi(str);
|
7
|
+
var _a = sqrtInner(inputInner, precision ? s2bi(precision) : undefined), _b = _a[0], bi = _b[0], fpe = _b[1], decimals = _a[1];
|
8
|
+
var outputStr = bi2s(bi, fpe);
|
9
|
+
return round(outputStr, { decimals: decimals });
|
10
10
|
};
|
package/src/sqrt/types.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import type {
|
2
|
-
export type SqrtInner = (input:
|
1
|
+
import type { BI } from "../shared/types.d.ts";
|
2
|
+
export type SqrtInner = (input: BI, prec?: BI, max?: number) => [BI, number];
|
3
3
|
export type Sqrt = (str: string, precision?: string) => string;
|
package/src/sqrt/utils.js
CHANGED
@@ -1,34 +1,24 @@
|
|
1
1
|
import { isLeftGreaterInner } from "../compare/utils.js";
|
2
2
|
import { PipeInner } from "../pipe/utils.js";
|
3
|
-
|
4
|
-
var
|
5
|
-
array: [53],
|
6
|
-
intLength: 0,
|
7
|
-
isNegative: false,
|
8
|
-
isFloat: true,
|
9
|
-
};
|
10
|
-
var precDef = {
|
11
|
-
array: [49],
|
12
|
-
intLength: -12,
|
13
|
-
isNegative: false,
|
14
|
-
isFloat: true,
|
15
|
-
};
|
3
|
+
var halfInner = [5n, 1];
|
4
|
+
var precDef = [1n, 13];
|
16
5
|
export var sqrtInner = function (input, prec, max) {
|
17
6
|
if (prec === void 0) { prec = precDef; }
|
18
7
|
if (max === void 0) { max = 100; }
|
19
|
-
var guess =
|
8
|
+
var guess = input;
|
20
9
|
for (var i = 0; i < max; i++) {
|
21
10
|
var nextGuess = new PipeInner()
|
22
|
-
.
|
23
|
-
.
|
24
|
-
.
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
11
|
+
.div([input, guess])
|
12
|
+
.add([guess])
|
13
|
+
.mul([halfInner])
|
14
|
+
.calc();
|
15
|
+
var _a = new PipeInner().sub([nextGuess, guess]).calc(), bi = _a[0], fpe = _a[1];
|
16
|
+
if (bi < 0n)
|
17
|
+
bi *= -1n;
|
18
|
+
if (isLeftGreaterInner({ left: prec, right: [bi, fpe] })) {
|
19
|
+
return [nextGuess, prec[1]];
|
30
20
|
}
|
31
21
|
guess = nextGuess;
|
32
22
|
}
|
33
|
-
return [guess, prec
|
23
|
+
return [guess, prec[1]];
|
34
24
|
};
|
@@ -1,59 +0,0 @@
|
|
1
|
-
import { subInner } from "../sub/utils.js";
|
2
|
-
/** This function adds 2 numbers (as array). */
|
3
|
-
export var addInner = function (_a, _b, isNegative) {
|
4
|
-
var _c, _d;
|
5
|
-
var arrL = _a[0], intL = _a[1];
|
6
|
-
var arrR = _b[0], intR = _b[1];
|
7
|
-
var _e = intL >= intR
|
8
|
-
? [arrL, arrR, intL, intR]
|
9
|
-
: [arrR, arrL, intR, intL], left = _e[0], right = _e[1], intLeft = _e[2], intRight = _e[3];
|
10
|
-
var fracLenL = left.length - intLeft;
|
11
|
-
var fracLenR = right.length - intRight;
|
12
|
-
var fracMaxLen = (fracLenL >= fracLenR ? fracLenL : fracLenR) - 1;
|
13
|
-
var pl = (intLeft >= intRight ? intLeft : intRight) + fracMaxLen;
|
14
|
-
var pr = (intLeft >= intRight ? intRight : intLeft) + fracMaxLen;
|
15
|
-
var carryOver = 48;
|
16
|
-
while (pl > left.length - 1) {
|
17
|
-
left.push(48);
|
18
|
-
}
|
19
|
-
while (pr >= 0) {
|
20
|
-
var sum = (((_c = left[pl]) !== null && _c !== void 0 ? _c : 48) + ((_d = right[pr]) !== null && _d !== void 0 ? _d : 48) + carryOver) -
|
21
|
-
3 * 48;
|
22
|
-
if (sum > 9) {
|
23
|
-
left[pl] = (sum % 10) + 48;
|
24
|
-
carryOver = ((sum / 10) | 0) + 48;
|
25
|
-
}
|
26
|
-
else {
|
27
|
-
left[pl] = sum + 48;
|
28
|
-
carryOver = 48;
|
29
|
-
}
|
30
|
-
pr -= 1;
|
31
|
-
pl -= 1;
|
32
|
-
}
|
33
|
-
while (pl >= 0 && carryOver) {
|
34
|
-
var sum = (left[pl] + carryOver) - 2 * 48;
|
35
|
-
left[pl] = (sum % 10) + 48;
|
36
|
-
carryOver = ((sum / 10) | 0) + 48;
|
37
|
-
pl -= 1;
|
38
|
-
}
|
39
|
-
carryOver > 48 && left.unshift(carryOver);
|
40
|
-
return {
|
41
|
-
array: left,
|
42
|
-
intLength: left.length - 1 - fracMaxLen,
|
43
|
-
isNegative: isNegative,
|
44
|
-
isFloat: fracLenL + fracLenR > 0,
|
45
|
-
};
|
46
|
-
};
|
47
|
-
export var addRoute = function (input, initValue) {
|
48
|
-
return input.reduce(function (left, right) {
|
49
|
-
if (left.array.length === 0)
|
50
|
-
return right;
|
51
|
-
if (left.isNegative && !right.isNegative) {
|
52
|
-
return subInner([right.array, right.intLength], [left.array, left.intLength]);
|
53
|
-
}
|
54
|
-
if (!left.isNegative && right.isNegative) {
|
55
|
-
return subInner([left.array, left.intLength], [right.array, right.intLength]);
|
56
|
-
}
|
57
|
-
return addInner([left.array, left.intLength], [right.array, right.intLength], left.isNegative && right.isNegative);
|
58
|
-
}, initValue);
|
59
|
-
};
|