@numio/bigmath 1.1.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/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 +8 -9
- package/src/MAD/main.js +5 -4
- package/src/MAD/types.d.ts +2 -2
- package/src/MAD/utils.js +6 -8
- package/src/compare/main.js +14 -14
- package/src/compare/types.d.ts +5 -5
- package/src/compare/utils.js +52 -75
- package/src/mean/main.js +5 -5
- package/src/mean/types.d.ts +2 -2
- package/src/mean/utils.js +4 -7
- 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 -8
- 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 +33 -35
- package/src/pipe/utils.d.ts +7 -8
- package/src/pipe/utils.js +22 -30
- package/src/quartile/main.js +7 -7
- package/src/quartile/types.d.ts +6 -6
- package/src/quartile/utils.js +9 -17
- 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.d.ts +0 -12
- package/src/shared/constant.js +1 -13
- 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/constants.js +3 -3
- package/src/sort/main.js +5 -6
- package/src/sort/types.d.ts +3 -3
- package/src/sort/utils.js +10 -12
- package/src/sqrt/main.js +6 -6
- package/src/sqrt/types.d.ts +2 -2
- package/src/sqrt/utils.js +16 -28
- 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/shared/utils.js
CHANGED
@@ -1,83 +1,76 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
export const bi2s = (bigInt, fpe) => {
|
2
|
+
if (bigInt === 0n)
|
3
|
+
return "0";
|
4
|
+
const isNeg = bigInt < 0n;
|
5
|
+
isNeg && (bigInt *= -1n);
|
6
|
+
const dp = bigInt % (10n ** BigInt(fpe));
|
7
|
+
const bigStr = bigInt.toString();
|
8
|
+
let fpIdx = bigStr.length - fpe;
|
9
|
+
if (fpIdx < 0)
|
10
|
+
fpIdx = 0;
|
11
|
+
const before = bigStr.slice(0, fpIdx);
|
12
|
+
const after = bigStr.slice(fpIdx);
|
13
|
+
if (before) {
|
14
|
+
return fillHead(bigStr.length, fpe, isNeg, true) +
|
15
|
+
(fpe > 0 && dp > 0 ? trimTail(`${before}.${after}`) : before);
|
16
|
+
}
|
17
|
+
return `${fillHead(bigStr.length, fpe, isNeg, false)}${trimTail(after)}`;
|
18
|
+
};
|
19
|
+
export const s2bi = (str) => {
|
20
|
+
const 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
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
while (array[idx] === 48 && idx >= intLength) {
|
14
|
-
lastValuableIdx = idx;
|
15
|
-
idx -= 1;
|
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
|
+
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
|
+
const fpDiff = dpLen - fpe;
|
45
|
+
bigInt = op(bigInt * (10n ** BigInt(fpDiff)), bigCurrent);
|
32
46
|
}
|
47
|
+
if (fpe > dpLen) {
|
48
|
+
bigInt = op(bigInt, bigCurrent * (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
|
39
|
-
|
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 const fillHead = (len, fpe, isNeg, hasBefore) => {
|
59
|
+
let 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
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
66
|
+
export const trimTail = (str) => {
|
67
|
+
if (str[str.length - 1] !== "0")
|
68
|
+
return str;
|
69
|
+
let count = 0;
|
70
|
+
for (let 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/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
@@ -1,10 +1,9 @@
|
|
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
|
-
export
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
return inputDataArray.map(function (input) { return a2s(input); });
|
5
|
+
export const sort = (array, sorting = ASC) => {
|
6
|
+
const arrayInner = array.map((str) => s2bi(str));
|
7
|
+
sortInner(arrayInner, sorting);
|
8
|
+
return arrayInner.map(([bi, fpe]) => bi2s(bi, fpe));
|
10
9
|
};
|
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/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
@@ -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
|
-
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/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,22 @@
|
|
1
1
|
import { isLeftGreaterInner } from "../compare/utils.js";
|
2
2
|
import { PipeInner } from "../pipe/utils.js";
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
if (max === void 0) { max = 100; }
|
19
|
-
var guess = cloneInner(input);
|
20
|
-
for (var i = 0; i < max; i++) {
|
21
|
-
var nextGuess = new PipeInner()
|
22
|
-
.divInner([cloneInner(input), cloneInner(guess)])
|
23
|
-
.addInner([guess])
|
24
|
-
.mulInner([halfInner]).result;
|
25
|
-
var candidate = new PipeInner()
|
26
|
-
.subInner([cloneInner(nextGuess), cloneInner(guess)]).result;
|
27
|
-
candidate.isNegative = false;
|
28
|
-
if (isLeftGreaterInner({ left: prec, right: candidate })) {
|
29
|
-
return [nextGuess, prec.intLength * -1];
|
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()
|
9
|
+
.div([input, guess])
|
10
|
+
.add([guess])
|
11
|
+
.mul([halfInner])
|
12
|
+
.calc();
|
13
|
+
let [bi, fpe] = new PipeInner().sub([nextGuess, guess]).calc();
|
14
|
+
if (bi < 0n)
|
15
|
+
bi *= -1n;
|
16
|
+
if (isLeftGreaterInner({ left: prec, right: [bi, fpe] })) {
|
17
|
+
return [nextGuess, prec[1]];
|
30
18
|
}
|
31
19
|
guess = nextGuess;
|
32
20
|
}
|
33
|
-
return [guess, prec
|
21
|
+
return [guess, prec[1]];
|
34
22
|
};
|
@@ -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
|
-
};
|
@@ -1,50 +0,0 @@
|
|
1
|
-
import { NIL } from "../../shared/constant.js";
|
2
|
-
/** This function multiplies 2 numbers (as array). */
|
3
|
-
export var mulInner = 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
|
-
if (arrL.length === 0 || arrR.length === 0 ||
|
8
|
-
(arrL.length === 1 && arrL[0] === 48) ||
|
9
|
-
(arrR.length === 1 && arrR[0] === 48)) {
|
10
|
-
return NIL;
|
11
|
-
}
|
12
|
-
var dec = (arrL.length - intL) + (arrR.length - intR);
|
13
|
-
var _e = arrL.length >= arrR.length
|
14
|
-
? [arrL, arrR]
|
15
|
-
: [arrR, arrL], left = _e[0], right = _e[1];
|
16
|
-
var sums = Array(right.length + left.length - 1);
|
17
|
-
for (var i = right.length - 1; i >= 0; i--) {
|
18
|
-
for (var j = left.length - 1; j >= 0; j--) {
|
19
|
-
var idx_1 = j + i;
|
20
|
-
sums[idx_1] = ((_c = sums[idx_1]) !== null && _c !== void 0 ? _c : 0) +
|
21
|
-
(left[j] - 48) * (right[i] - 48);
|
22
|
-
}
|
23
|
-
}
|
24
|
-
var result = Array(sums.length);
|
25
|
-
var idx = sums.length - 1;
|
26
|
-
var carryOver = 0;
|
27
|
-
var currNum = 0;
|
28
|
-
var nextNum = 0;
|
29
|
-
while (idx >= 0) {
|
30
|
-
currNum = (_d = sums[idx]) !== null && _d !== void 0 ? _d : 0;
|
31
|
-
nextNum = (currNum + carryOver) % 10;
|
32
|
-
carryOver = (carryOver + currNum) / 10 | 0;
|
33
|
-
result[idx] = nextNum + 48;
|
34
|
-
idx -= 1;
|
35
|
-
}
|
36
|
-
carryOver > 0 && result.unshift(carryOver + 48);
|
37
|
-
return {
|
38
|
-
array: result,
|
39
|
-
intLength: result.length - dec,
|
40
|
-
isFloat: dec > 0,
|
41
|
-
isNegative: isNegative,
|
42
|
-
};
|
43
|
-
};
|
44
|
-
export var mulRoute = function (input, initValue) {
|
45
|
-
return input.reduce(function (left, right) {
|
46
|
-
if (left.array.length === 0)
|
47
|
-
return right;
|
48
|
-
return mulInner([left.array, left.intLength], [right.array, right.intLength], left.isNegative !== right.isNegative);
|
49
|
-
}, initValue);
|
50
|
-
};
|
@@ -1,81 +0,0 @@
|
|
1
|
-
import { addInner } from "../add/utils.js";
|
2
|
-
/** This function subtracts 2 numbers (as array). */
|
3
|
-
export var subInner = function (_a, _b) {
|
4
|
-
var _c, _d;
|
5
|
-
var _e, _f;
|
6
|
-
var arrL = _a[0], intL = _a[1];
|
7
|
-
var arrR = _b[0], intR = _b[1];
|
8
|
-
var lenDiff = (intL - intR) * (intL > intR ? 1 : -1);
|
9
|
-
var _g = intL >= intR
|
10
|
-
? [arrL, arrR, intL, intR]
|
11
|
-
: [arrR, arrL, intR, intL], left = _g[0], right = _g[1], intLeft = _g[2], intRight = _g[3];
|
12
|
-
var fracLenL = left.length - intLeft;
|
13
|
-
var fracLenR = right.length - intRight;
|
14
|
-
var pl = lenDiff;
|
15
|
-
var pr = 0;
|
16
|
-
var isLeftBigger = lenDiff > 0;
|
17
|
-
var carryOver = false;
|
18
|
-
var isNegative = intLeft !== intL;
|
19
|
-
while (pl > left.length - 1) {
|
20
|
-
left.push(48);
|
21
|
-
}
|
22
|
-
while (pr < right.length) {
|
23
|
-
var sub = (((_e = left[pl]) !== null && _e !== void 0 ? _e : 48) - ((_f = right[pr]) !== null && _f !== void 0 ? _f : 48)) + 48;
|
24
|
-
if (!isLeftBigger && left[pl] > right[pr]) {
|
25
|
-
isLeftBigger = true;
|
26
|
-
}
|
27
|
-
if (!isLeftBigger && sub < 48 && lenDiff === 0) {
|
28
|
-
_c = [right, left], left = _c[0], right = _c[1];
|
29
|
-
_d = [intR, intL], intL = _d[0], intR = _d[1];
|
30
|
-
isLeftBigger = true;
|
31
|
-
isNegative = true;
|
32
|
-
continue;
|
33
|
-
}
|
34
|
-
if (sub < 48) {
|
35
|
-
sub += 10;
|
36
|
-
carryOver = true;
|
37
|
-
}
|
38
|
-
var plReverse = pl - 1;
|
39
|
-
var prReverse = pr - 1;
|
40
|
-
while (carryOver) {
|
41
|
-
if (left[plReverse] !== 48) {
|
42
|
-
plReverse >= 0 && (left[plReverse] -= 1);
|
43
|
-
prReverse >= 0 && (right[prReverse] -= 1);
|
44
|
-
carryOver = false;
|
45
|
-
}
|
46
|
-
else {
|
47
|
-
plReverse >= 0 && (left[plReverse] = 57);
|
48
|
-
prReverse >= 0 && (right[prReverse] = 57);
|
49
|
-
}
|
50
|
-
plReverse -= 1;
|
51
|
-
prReverse -= 1;
|
52
|
-
}
|
53
|
-
left[pl] = sub;
|
54
|
-
right[pr] = sub;
|
55
|
-
pl += 1;
|
56
|
-
pr += 1;
|
57
|
-
}
|
58
|
-
while (left[0] === 48 && left.length > 1) {
|
59
|
-
left.shift();
|
60
|
-
intLeft -= 1;
|
61
|
-
}
|
62
|
-
return {
|
63
|
-
array: left,
|
64
|
-
intLength: intLeft,
|
65
|
-
isNegative: isNegative,
|
66
|
-
isFloat: fracLenL + fracLenR > 0,
|
67
|
-
};
|
68
|
-
};
|
69
|
-
export var subRoute = function (input, initValue) {
|
70
|
-
return input.reduce(function (left, right) {
|
71
|
-
if (left.array.length === 0)
|
72
|
-
return right;
|
73
|
-
if (left.isNegative && right.isNegative) {
|
74
|
-
return subInner([right.array, right.intLength], [left.array, left.intLength]);
|
75
|
-
}
|
76
|
-
if (!left.isNegative && !right.isNegative) {
|
77
|
-
return subInner([left.array, left.intLength], [right.array, right.intLength]);
|
78
|
-
}
|
79
|
-
return addInner([left.array, left.intLength], [right.array, right.intLength], left.isNegative);
|
80
|
-
}, initValue);
|
81
|
-
};
|