@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.
Files changed (52) hide show
  1. package/README.md +16 -4
  2. package/package.json +7 -2
  3. package/src/IQR/main.js +5 -4
  4. package/src/IQR/types.d.ts +2 -2
  5. package/src/IQR/utils.js +3 -3
  6. package/src/MAD/main.js +5 -4
  7. package/src/MAD/types.d.ts +2 -2
  8. package/src/MAD/utils.js +2 -4
  9. package/src/compare/main.js +12 -10
  10. package/src/compare/types.d.ts +5 -5
  11. package/src/compare/utils.js +43 -64
  12. package/src/mean/main.js +5 -5
  13. package/src/mean/types.d.ts +2 -2
  14. package/src/mean/utils.js +3 -6
  15. package/src/operations/add/main.d.ts +1 -1
  16. package/src/operations/add/main.js +7 -6
  17. package/src/operations/div/main.d.ts +1 -1
  18. package/src/operations/div/main.js +6 -7
  19. package/src/operations/div/types.d.ts +2 -3
  20. package/src/operations/div/utils.d.ts +1 -2
  21. package/src/operations/div/utils.js +33 -93
  22. package/src/operations/mul/main.d.ts +1 -1
  23. package/src/operations/mul/main.js +5 -5
  24. package/src/operations/sub/main.d.ts +1 -1
  25. package/src/operations/sub/main.js +6 -5
  26. package/src/pipe/main.d.ts +6 -6
  27. package/src/pipe/main.js +23 -22
  28. package/src/pipe/utils.d.ts +7 -8
  29. package/src/pipe/utils.js +14 -18
  30. package/src/quartile/main.js +7 -7
  31. package/src/quartile/types.d.ts +6 -6
  32. package/src/quartile/utils.js +2 -10
  33. package/src/shared/constant.d.ts +0 -12
  34. package/src/shared/constant.js +0 -12
  35. package/src/shared/types.d.ts +6 -5
  36. package/src/shared/utils.d.ts +6 -4
  37. package/src/shared/utils.js +64 -71
  38. package/src/sort/main.js +7 -4
  39. package/src/sort/types.d.ts +3 -3
  40. package/src/sqrt/main.js +5 -5
  41. package/src/sqrt/types.d.ts +2 -2
  42. package/src/sqrt/utils.js +13 -23
  43. package/src/operations/add/types.d.ts +0 -2
  44. package/src/operations/add/utils.d.ts +0 -5
  45. package/src/operations/add/utils.js +0 -59
  46. package/src/operations/mul/types.d.ts +0 -2
  47. package/src/operations/mul/utils.d.ts +0 -5
  48. package/src/operations/mul/utils.js +0 -50
  49. package/src/operations/sub/types.d.ts +0 -2
  50. package/src/operations/sub/utils.d.ts +0 -5
  51. package/src/operations/sub/utils.js +0 -81
  52. 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 { DEFAULT } from "../shared/constant.js";
14
- import { a2s, s2a } from "../shared/utils.js";
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 (strs) {
25
- __classPrivateFieldSet(this, _Pipe_result, addRoute(strs.map(function (str) { return s2a(str); }), __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
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 (strs) {
29
- __classPrivateFieldSet(this, _Pipe_result, subRoute(strs.map(function (str) { return s2a(str); }), __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
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 (strs, limit) {
29
+ Pipe.prototype.div = function (array, limit) {
33
30
  if (limit === void 0) { limit = 20; }
34
- __classPrivateFieldSet(this, _Pipe_result, divRoute(strs.map(function (str) { return s2a(str); }), __classPrivateFieldGet(this, _Pipe_result, "f"), limit), "f");
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 (strs) {
38
- __classPrivateFieldSet(this, _Pipe_result, mulRoute(strs.map(function (str) { return s2a(str); }), __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
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 result = a2s(__classPrivateFieldGet(this, _Pipe_result, "f"));
43
- __classPrivateFieldSet(this, _Pipe_result, DEFAULT, "f");
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;
@@ -1,11 +1,10 @@
1
- import type { A2S } from "../shared/types.d.ts";
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: InputData;
3
+ result: BI | undefined;
5
4
  constructor();
6
- addInner(array: InputData[]): PipeInner;
7
- subInner(array: InputData[]): PipeInner;
8
- divInner(array: InputData[]): PipeInner;
9
- mulInner(array: InputData[]): PipeInner;
10
- calc(): ReturnType<A2S>;
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 { DEFAULT } from "../shared/constant.js";
2
- import { a2s, cloneInner } from "../shared/utils.js";
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.addInner = function (array) {
12
- this.result = addRoute(array, cloneInner(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.subInner = function (array) {
16
- this.result = subRoute(array, cloneInner(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.divInner = function (array) {
20
- this.result = divRoute(array, cloneInner(this.result), 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.mulInner = function (array) {
24
- this.result = mulRoute(array, cloneInner(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 result = a2s(this.result);
29
- this.result = DEFAULT;
30
- return result;
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
  }());
@@ -1,12 +1,12 @@
1
- import { a2s, s2a } from "../shared/utils.js";
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 (strs) {
5
- var array = strs.map(function (str) { return s2a(str); });
6
- var Qs = quartileInner(array);
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: a2s(Qs.Q1),
9
- Q2: a2s(Qs.Q2),
10
- Q3: a2s(Qs.Q3),
8
+ Q1: bi2s(Q1bi, Q1fpe),
9
+ Q2: bi2s(Q2bi, Q2fpe),
10
+ Q3: bi2s(Q3bi, Q3fpe),
11
11
  };
12
12
  };
@@ -1,12 +1,12 @@
1
- import type { InputData } from "../types.d.ts";
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: InputData[]) => InputData;
8
- export type QuartileInner = (array: InputData[]) => {
9
- Q1: InputData;
10
- Q2: InputData;
11
- Q3: InputData;
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
  };
@@ -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
- return new PipeInner().addInner([
5
- cloneInner(array[idx]),
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) {
@@ -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
- };
@@ -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
- };
@@ -1,5 +1,6 @@
1
- import type { InputData } from "../types.d.ts";
2
- export type A2S = (input: InputData) => string;
3
- export type S2A = (strings: string) => InputData;
4
- export type Convert = (isNegative: InputData["isNegative"], array: InputData["array"]) => string;
5
- export type CloneInner = (inner: InputData) => InputData;
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;
@@ -1,4 +1,6 @@
1
- import type { A2S, CloneInner, S2A } from "./types.d.ts";
2
- export declare const a2s: A2S;
3
- export declare const s2a: S2A;
4
- export declare const cloneInner: CloneInner;
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;
@@ -1,83 +1,76 @@
1
- var convert = function (isNegative, array) {
2
- return (isNegative ? "-" : "") + String.fromCharCode.apply(String, array);
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 a2s = function (_a) {
5
- var array = _a.array, isFloat = _a.isFloat, isNegative = _a.isNegative, intLength = _a.intLength;
6
- var result = [];
7
- var lastValuableIdx = array.length;
8
- var isNil = array.length === 1 && array[0] === 48;
9
- if (isNil)
10
- return convert(isNegative, array);
11
- if (isFloat) {
12
- var idx = array.length - 1;
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
- if (intLength !== array.length) {
19
- if (intLength <= 0) {
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
- for (var i = 0; i < lastValuableIdx; i++) {
30
- i === intLength && result.push(46);
31
- result.push(array[i]);
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 convert(isNegative, array);
56
+ return [bigInt, fpe];
37
57
  };
38
- export var s2a = function (string) {
39
- var array = Array(0);
40
- var isNegative = string.charCodeAt(0) === 45;
41
- var shift = isNegative ? 1 : 0;
42
- var dec = 0;
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 cloneInner = function (inner) {
72
- var len = inner.array.length;
73
- var clone = Array(len);
74
- for (var i = 0; i < len; i++) {
75
- clone[i] = inner.array[i];
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 { a2s, s2a } from "../shared/utils.js";
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 inputDataArray = array.map(function (str) { return s2a(str); });
8
- sortInner(inputDataArray, sorting);
9
- return inputDataArray.map(function (input) { return a2s(input); });
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
  };
@@ -1,6 +1,6 @@
1
- import type { InputData } from "../types.d.ts";
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: InputData[], sorting: Sorting) => InputData[];
6
- export type Heapify = (array: InputData[], len: number, i: number, sorting: Sorting) => void;
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 { a2s, s2a } from "../shared/utils.js";
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 = s2a(str);
7
- var _a = sqrtInner(inputInner, precision ? s2a(precision) : undefined), outputInner = _a[0], decimals = _a[1];
8
- var outputStr = a2s(outputInner);
9
- return round(outputStr, { decimals: decimals + 1 });
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
  };
@@ -1,3 +1,3 @@
1
- import type { InputData } from "../types.d.ts";
2
- export type SqrtInner = (input: InputData, prec?: InputData, max?: number) => [InputData, number];
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
- import { cloneInner } from "../shared/utils.js";
4
- var halfInner = {
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 = cloneInner(input);
8
+ var guess = input;
20
9
  for (var i = 0; i < max; i++) {
21
10
  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];
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.intLength * -1];
23
+ return [guess, prec[1]];
34
24
  };
@@ -1,2 +0,0 @@
1
- import type { InputData } from "../../types.d.ts";
2
- export type AddInner = (L: [number[], number], R: [number[], number], isNegative: boolean) => InputData;
@@ -1,5 +0,0 @@
1
- import type { Route } from "../../types.d.ts";
2
- import type { AddInner } from "./types.d.ts";
3
- /** This function adds 2 numbers (as array). */
4
- export declare const addInner: AddInner;
5
- export declare const addRoute: Route;
@@ -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,2 +0,0 @@
1
- import type { InputData } from "../../types.d.ts";
2
- export type MulInner = (arrL: [number[], number], arrR: [number[], number], isNegative: boolean) => InputData;
@@ -1,5 +0,0 @@
1
- import type { Route } from "../../types.d.ts";
2
- import type { MulInner } from "./types.d.ts";
3
- /** This function multiplies 2 numbers (as array). */
4
- export declare const mulInner: MulInner;
5
- export declare const mulRoute: Route;