@numio/bigmath 2.0.0 → 2.1.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 (47) hide show
  1. package/README.md +58 -3
  2. package/package.json +1 -1
  3. package/src/IQR/main.js +3 -3
  4. package/src/IQR/utils.js +8 -9
  5. package/src/MAD/main.js +3 -3
  6. package/src/MAD/utils.js +5 -5
  7. package/src/abs/main.d.ts +2 -0
  8. package/src/abs/main.js +6 -0
  9. package/src/abs/types.d.ts +3 -0
  10. package/src/abs/utils.d.ts +2 -0
  11. package/src/abs/utils.js +3 -0
  12. package/src/cbrt/main.d.ts +3 -0
  13. package/src/cbrt/main.js +9 -0
  14. package/src/cbrt/types.d.ts +3 -0
  15. package/src/cbrt/utils.d.ts +2 -0
  16. package/src/cbrt/utils.js +22 -0
  17. package/src/compare/main.d.ts +3 -1
  18. package/src/compare/main.js +13 -11
  19. package/src/compare/types.d.ts +11 -3
  20. package/src/compare/utils.d.ts +2 -1
  21. package/src/compare/utils.js +24 -23
  22. package/src/mean/main.js +3 -3
  23. package/src/mean/utils.js +3 -3
  24. package/src/operations/add/main.js +3 -3
  25. package/src/operations/div/main.js +3 -4
  26. package/src/operations/div/utils.js +12 -12
  27. package/src/operations/mul/main.js +3 -3
  28. package/src/operations/sub/main.js +2 -2
  29. package/src/pipe/main.d.ts +4 -3
  30. package/src/pipe/main.js +49 -34
  31. package/src/pipe/utils.js +17 -21
  32. package/src/quartile/main.js +3 -3
  33. package/src/quartile/utils.js +8 -8
  34. package/src/round/constants.js +7 -7
  35. package/src/round/main.js +16 -17
  36. package/src/round/utils.js +24 -25
  37. package/src/shared/constant.js +1 -1
  38. package/src/shared/utils.js +25 -25
  39. package/src/sort/constants.js +3 -3
  40. package/src/sort/main.js +3 -7
  41. package/src/sort/utils.js +10 -12
  42. package/src/sqrt/main.js +4 -5
  43. package/src/sqrt/types.d.ts +2 -2
  44. package/src/sqrt/utils.js +7 -11
  45. package/src/toBase/main.d.ts +2 -0
  46. package/src/toBase/main.js +3 -0
  47. package/src/toBase/types.d.ts +4 -0
package/README.md CHANGED
@@ -21,6 +21,8 @@
21
21
  * **Round Based on Significant Figures:** Control rounding based on the number of significant figures, crucial for scientific and engineering applications.
22
22
  * **Calculate Roots:**
23
23
  * **Calculate Square Root (`sqrt`):** Compute the square root of a number with arbitrary precision. You can also specify the desired precision of the result.
24
+ * **NEW! Calculate Cube Root (`cbrt`):** Determine the cube root of a number with arbitrary precision, allowing you to specify the desired accuracy.
25
+
24
26
  * **Chain Operations with Pipe:** Simplify complex calculations by chaining arithmetic operations in a readable and intuitive manner.
25
27
  * **Analyze Data Distribution:**
26
28
  * **Calculate Quartiles (Q1, Q2, Q3):** Understand the spread and central tendency of your numerical data, helping identify outliers and the shape of the distribution.
@@ -29,9 +31,12 @@
29
31
  * **Compare Numbers:**
30
32
  * **Check for Equality (`isEqual`):** Accurately determine if two arbitrary-precision numbers are equal.
31
33
  * **Check if Left is Greater (`isLeftGreater`):** Precisely compare two arbitrary-precision numbers to see if the left operand is greater than the right.
34
+ * **NEW! Check if Left is Greater or Equal (`isLeftGreaterOrEqual`):** Precisely compare two arbitrary-precision numbers to determine if the left operand is greater than or equal to the right.
32
35
  * **Sort Numbers Accurately:** Sort arrays of numbers, including negative and decimal values, in ascending or descending order, correctly handling string representations of numbers that JavaScript's native sort might misinterpret.
33
36
  * **Calculate Central Tendency:** Easily compute the mean (average) of a set of numbers.
34
37
  * **Identify Extremes:** Find the maximum and minimum values within an array of numbers.
38
+ * **NEW! Calculate Absolute Value (`abs`):** Determine the non-negative value of a number, regardless of its sign.
39
+ * **NEW! Convert Number to Another Base (`toBase`):** Seamlessly convert numbers between decimal, hexadecimal (HEX), binary, and octal representations.
35
40
 
36
41
  ## When is @numio/bigmath essential?
37
42
 
@@ -208,18 +213,24 @@ round("1.000119", { decimals: 2, sigFig: true }); // 1
208
213
  ### Pipe
209
214
 
210
215
  ```javascript
211
- import { pipe } from "@numio/bigmath";
216
+ import { Pipe } from "@numio/bigmath";
212
217
 
213
218
  const addNums = ["1", "2", "3"];
214
219
  const subNums = ["0.2", "0.3"];
215
220
  const divNums = ["4"];
216
221
  const mulNums = ["2", "5", "0.2"];
217
222
 
218
- pipe.add(addNums) // 6
223
+ new Pipe().add(addNums) // 6
219
224
  .div(divNums) // 6 / 4 = 1.5
220
225
  .sub(subNums) // 1.5 - 0.2 - 0.3 = 1
221
226
  .mul(mulNums) // 1 * 2 * 5 * 0.2 = 2
222
227
  .calc() // convert end result to readable string
228
+
229
+ new Pipe().sub(["1", "5"]) // 1 - 5 = -4
230
+ .abs() // 4 - returns absolute value
231
+
232
+ new Pipe().add(["10", "5"]) // 11 + 5 = 15
233
+ .resultToBase(16) // f - returns result converted to base 16
223
234
  ```
224
235
 
225
236
  ### Quartile
@@ -289,6 +300,16 @@ isLeftGreater({left: "0.1", right: "0.1"}) // false;
289
300
  isLeftGreater({left: "0.1", right: "-0.1"}) // true;
290
301
  ```
291
302
 
303
+ ### IsLeftGreaterOrEqual
304
+ ```javascript
305
+ import { isLeftGreaterOrEqual } from "@numio/bigmath";
306
+
307
+ isLeftGreaterOrEqual({left: "0.1", right: "2"}) // false;
308
+ isLeftGreaterOrEqual({left: "2", right: "0.1"}) // true;
309
+ isLeftGreaterOrEqual({left: "0.1", right: "0.1"}) // true;
310
+ isLeftGreaterOrEqual({left: "0.1", right: "-0.1"}) // true;
311
+ ```
312
+
292
313
  ### MAD - Median Absolute Deviation
293
314
  ```javascript
294
315
  import { MAD } from "@numio/bigmath";
@@ -315,12 +336,46 @@ sqrt("3", "0.01") // 1.732;
315
336
  sqrt("3", "0.000000000000000000001") // 1.732050807568877293527;
316
337
  ```
317
338
 
339
+ ### CBRT - cube root of a number
340
+ ```javascript
341
+ import { cbrt } from "@numio/bigmath";
342
+
343
+ cbrt("37") // 3;
344
+ cbrt("1000") // 10;
345
+ cbrt("15"), // 2.4662120743305
346
+
347
+ // you can change precision of a result (second parameter),
348
+ cbrt("15", "0.001") // 2.466
349
+ ```
350
+
351
+ ### ABS - absolute value
352
+ ```javascript
353
+ import { abs } from "@numio/bigmath";
354
+
355
+ abs("-1") // 1;
356
+ abs("1") // 1;
357
+ ```
358
+
359
+ ### toBase - convert number to another base
360
+ ```javascript
361
+ import { toBase } from "@numio/bigmath";
362
+
363
+ toBase({ value: "11", toBase: 16 }) // b
364
+ // 0x - HEX
365
+ toBase({ value: "0xb", toBase: 10 }) // 11
366
+ // 0b - binary
367
+ toBase({ value: "0b101", toBase: 10 }) // 5
368
+ toBase({ value: "0b1101", toBase: 16 }) // d
369
+ // 0o - octal
370
+ toBase({ value: "0o11", toBase: 10 }) // 9
371
+ ```
372
+
318
373
  Does not have a limitation on the number of digits. You can use any length you'd
319
374
  like
320
375
 
321
376
  ```javascript
322
377
  // NO precision loss using numeric literals with more than 15 significant digits.
323
- const int = sub(
378
+ const int = add(
324
379
  "999999999999999999999999999999999999999999999999999999999999999",
325
380
  "2",
326
381
  ); // "1000000000000000000000000000000000000000000000000000000000000001"
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.0",
4
+ "version": "2.1.0",
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 var IQR = function (array, sigNum) {
5
- var arrayInner = array.map(function (str) { return s2bi(str); });
6
- var _a = IQRInner(arrayInner, sigNum), bi = _a[0], fpe = _a[1];
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 var MIN_LENGTH_FOR_MAD = 30;
6
- export var IQRInner = function (array, sigNum) {
7
- if (sigNum === void 0) { sigNum = false; }
8
- var _a = quartileInner(array), Q1 = _a.Q1, Q3 = _a.Q3;
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
- var isEqual = isEqualInner({ left: Q3, right: Q1 });
12
- var MAD = MADInner(array);
13
- var isNil = isEqualInner({ left: MAD, right: [0n, 0] });
14
- var nonNilMAD = isNil ? [1n, 0] : MAD;
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 var MAD = function (array) {
5
- var arrayInner = array.map(function (str) { return s2bi(str); });
6
- var _a = MADInner(arrayInner), bi = _a[0], fpe = _a[1];
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 var MADInner = function (array) {
6
- var median = quartileInner(array).Q2;
7
- var madArray = array.map(function (el) {
8
- var _a = new PipeInner().sub([el, median]).calc(), bi = _a[0], fpe = _a[1];
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
- var sorted = sortInner(madArray, ASC);
11
+ const sorted = sortInner(madArray, ASC);
12
12
  return quartileInner(sorted).Q2;
13
13
  };
@@ -0,0 +1,2 @@
1
+ import type { Abs } from "./types.d.ts";
2
+ export declare const abs: Abs;
@@ -0,0 +1,6 @@
1
+ import { bi2s, s2bi } from "../shared/utils.js";
2
+ import { absInner } from "./utils.js";
3
+ export const abs = (value) => {
4
+ const [bi, fpe] = absInner(s2bi(value));
5
+ return bi2s(bi, fpe);
6
+ };
@@ -0,0 +1,3 @@
1
+ import type { BI } from "../shared/types.d.ts";
2
+ export type Abs = (value: string) => string;
3
+ export type AbsInner = (value: BI) => BI;
@@ -0,0 +1,2 @@
1
+ import type { AbsInner } from "./types.d.ts";
2
+ export declare const absInner: AbsInner;
@@ -0,0 +1,3 @@
1
+ export const absInner = ([bi, fpe]) => {
2
+ return [bi < 0n ? bi * -1n : bi, fpe];
3
+ };
@@ -0,0 +1,3 @@
1
+ import type { Cbrt } from "./types.d.ts";
2
+ /** Find cube root of a number */
3
+ export declare const cbrt: Cbrt;
@@ -0,0 +1,9 @@
1
+ import { round } from "../round/main.js";
2
+ import { bi2s, s2bi } from "../shared/utils.js";
3
+ import { cbrtInner } from "./utils.js";
4
+ /** Find cube root of a number */
5
+ export const cbrt = (value, precision) => {
6
+ const valueInner = s2bi(value);
7
+ const [[bi, fpe], decimals] = cbrtInner(valueInner, precision ? s2bi(precision) : undefined);
8
+ return round(bi2s(bi, fpe), { decimals });
9
+ };
@@ -0,0 +1,3 @@
1
+ import type { BI } from "../shared/types.d.ts";
2
+ export type CbrtInner = (value: BI, prec?: BI, max?: number) => [BI, number];
3
+ export type Cbrt = (value: string, precision?: string) => string;
@@ -0,0 +1,2 @@
1
+ import type { CbrtInner } from "./types.d.ts";
2
+ export declare const cbrtInner: CbrtInner;
@@ -0,0 +1,22 @@
1
+ import { isLeftGreaterInner } from "../compare/utils.js";
2
+ import { divInner } from "../operations/div/utils.js";
3
+ import { PipeInner } from "../pipe/utils.js";
4
+ import { calcInner } from "../shared/utils.js";
5
+ export const cbrtInner = (value, prec = [1n, 13], max = 100) => {
6
+ let guess = value;
7
+ for (let i = 0; i < max; i++) {
8
+ const mul = calcInner([guess, guess], (a, b) => a * b);
9
+ const nextGuess = new PipeInner().add([
10
+ calcInner([guess, [2n, 0]], (a, b) => a * b),
11
+ divInner([value, mul], 20),
12
+ ]).div([[3n, 0]]).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]];
18
+ }
19
+ guess = nextGuess;
20
+ }
21
+ return [guess, prec[1]];
22
+ };
@@ -1,9 +1,11 @@
1
- import type { IsEqual, IsLeftGreater, Max, Min } from "./types.d.ts";
1
+ import type { IsEqual, IsLeftGreater, IsLeftGreaterOrEqual, Max, Min } from "./types.d.ts";
2
2
  /** This function returns max value. */
3
3
  export declare const max: Max;
4
4
  /** This function returns min value. */
5
5
  export declare const min: Min;
6
6
  /** This function returns if left value is greater than right value */
7
7
  export declare const isLeftGreater: IsLeftGreater;
8
+ /** This function returns if left value is greater than right value */
9
+ export declare const isLeftGreaterOrEqual: IsLeftGreaterOrEqual;
8
10
  /** This function returns if left and right values are equal */
9
11
  export declare const isEqual: IsEqual;
@@ -1,24 +1,26 @@
1
1
  import { bi2s, s2bi } from "../shared/utils.js";
2
- import { isEqualInner, isLeftGreaterInner, maxInner, minInner, } from "./utils.js";
2
+ import { isEqualInner, isLeftGreaterInner, isLeftGreaterOrEqualInner, maxInner, minInner, } from "./utils.js";
3
3
  /** This function returns max value. */
4
- export var max = function (array) {
5
- var arrayInner = array.map(function (str) { return s2bi(str); });
6
- var _a = maxInner(arrayInner), bi = _a[0], fpe = _a[1];
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 var min = function (array) {
11
- var arrayInner = array.map(function (str) { return s2bi(str); });
12
- var _a = minInner(arrayInner), bi = _a[0], fpe = _a[1];
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 var isLeftGreater = function (_a) {
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
  };
19
+ /** This function returns if left value is greater than right value */
20
+ export const isLeftGreaterOrEqual = ({ left, right }) => {
21
+ return isLeftGreaterOrEqualInner({ left: s2bi(left), right: s2bi(right) });
22
+ };
20
23
  /** This function returns if left and right values are equal */
21
- export var isEqual = function (_a) {
22
- var left = _a.left, right = _a.right;
24
+ export const isEqual = ({ left, right }) => {
23
25
  return isEqualInner({ left: s2bi(left), right: s2bi(right) });
24
26
  };
@@ -1,16 +1,24 @@
1
1
  import type { BI } from "../shared/types.d.ts";
2
2
  export type Min = (strs: string[]) => string;
3
3
  export type Max = Min;
4
- export type IsLeftGreater = (attr: {
4
+ export type IsLeftGreater = (arg: {
5
+ left: string;
6
+ right: string;
7
+ }) => boolean;
8
+ export type IsEqual = (arg: {
9
+ left: string;
10
+ right: string;
11
+ }) => boolean;
12
+ export type IsLeftGreaterOrEqual = (arg: {
5
13
  left: string;
6
14
  right: string;
7
15
  }) => boolean;
8
- export type IsEqual = IsLeftGreater;
9
16
  export type CompareInner = (left: BI, right: BI) => [BI, number];
10
17
  export type MinInner = (array: BI[]) => BI;
11
18
  export type MaxInner = MinInner;
12
- export type IsEqualInner = (attr: {
19
+ export type IsEqualInner = (arg: {
13
20
  left: BI;
14
21
  right: BI;
15
22
  }) => boolean;
16
23
  export type IsLeftGreaterInner = IsEqualInner;
24
+ export type IsLeftGreaterOrEqualInner = IsEqualInner;
@@ -1,6 +1,7 @@
1
- import type { CompareInner, IsEqualInner, IsLeftGreaterInner, MaxInner, MinInner } from "./types.d.ts";
1
+ import type { CompareInner, IsEqualInner, IsLeftGreaterInner, IsLeftGreaterOrEqualInner, MaxInner, MinInner } from "./types.d.ts";
2
2
  export declare const maxInner: MaxInner;
3
3
  export declare const minInner: MinInner;
4
4
  export declare const compareInner: CompareInner;
5
5
  export declare const isEqualInner: IsEqualInner;
6
6
  export declare const isLeftGreaterInner: IsLeftGreaterInner;
7
+ export declare const isLeftGreaterOrEqualInner: IsLeftGreaterOrEqualInner;
@@ -1,22 +1,22 @@
1
- export var maxInner = function (array) {
2
- var max = array[0];
3
- for (var i = 1; i < array.length; i++) {
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 var minInner = function (array) {
10
- var min = array[0];
11
- for (var i = 1; i < array.length; i++) {
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 var compareInner = function (l, r) {
18
- var biL = l[0], fpeL = l[1];
19
- var biR = r[0], fpeR = r[1];
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
- var fpeBiL = BigInt(fpeL);
40
- var fpeBiR = BigInt(fpeR);
41
- var max = fpeBiL > fpeBiR ? fpeBiL : fpeBiR;
42
- var powL = biL * Math.pow(10n, (max - fpeBiL));
43
- var powR = biR * Math.pow(10n, (max - fpeBiR));
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
- var fpeBiL = BigInt(fpeL);
59
- var fpeBiR = BigInt(fpeR);
60
- var max = fpeBiL > fpeBiR ? fpeBiL : fpeBiR;
61
- var powL = biL * Math.pow(10n, (max - fpeBiL));
62
- var powR = biR * Math.pow(10n, (max - fpeBiR));
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,12 @@ export var compareInner = function (l, r) {
67
67
  }
68
68
  return [l, 0];
69
69
  };
70
- export var isEqualInner = function (_a) {
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 var isLeftGreaterInner = function (_a) {
75
- var left = _a.left, right = _a.right;
73
+ export const isLeftGreaterInner = ({ left, right }) => {
76
74
  return compareInner(left, right)[1] > 0;
77
75
  };
76
+ export const isLeftGreaterOrEqualInner = ({ left, right }) => {
77
+ return compareInner(left, right)[1] >= 0;
78
+ };
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 var mean = function (array) {
5
- var arrayInner = array.map(function (str) { return s2bi(str); });
6
- var _a = meanInner(arrayInner), bi = _a[0], fpe = _a[1];
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 var meanInner = function (array) {
3
- var left = new PipeInner().add(array).calc();
4
- var right = [BigInt(array.length), 0];
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 var add = function (array) {
5
- var arrayInner = array.map(function (str) { return s2bi(str); });
6
- var _a = calcInner(arrayInner, function (a, b) { return a + b; }), bigInt = _a[0], fpe = _a[1];
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 var div = function (array, limit) {
5
- if (limit === void 0) { limit = 20; }
6
- var arrayInner = array.map(function (str) { return s2bi(str); });
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 var divInner = function (array, limit, def) {
2
- var bigInt = def ? def[0] : array[0][0];
3
- var fpe = def ? def[1] : array[0][1];
4
- var isNeg = bigInt < 0n;
5
- for (var i = def ? 0 : 1; i < array.length; i++) {
6
- var _a = array[i], bigCurrent = _a[0], dpLen = _a[1];
7
- var r = 0n;
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 *= Math.pow(10n, BigInt(dpLen - fpe));
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
- var q = bigInt / bigCurrent;
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
- var nextBigInt = r * 10n;
31
- var nextQ = nextBigInt / bigCurrent;
32
- var nextRemained = nextBigInt - nextQ * bigCurrent;
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 var mul = function (array) {
4
- var arrayInner = array.map(function (str) { return s2bi(str); });
5
- var _a = calcInner(arrayInner, function (a, b) { return a * b; }), bigInt = _a[0], fpe = _a[1];
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
- var arrayInner = array.map(function (str) { return s2bi(str); });
6
- var _a = calcInner(arrayInner, function (a, b) { return a - b; }), bigInt = _a[0], fpe = _a[1];
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,12 +1,13 @@
1
1
  import type { BI2S } from "../shared/types.d.ts";
2
+ /** Using this function you can chain operations (add, sub, div, mul). */
2
3
  export declare class Pipe {
3
4
  #private;
4
- constructor();
5
+ constructor(value?: string);
5
6
  add(array: string[]): Pipe;
6
7
  sub(array: string[]): Pipe;
7
8
  div(array: string[], limit?: number): Pipe;
8
9
  mul(array: string[]): Pipe;
10
+ abs(): Pipe;
9
11
  calc(): ReturnType<BI2S>;
12
+ resultToBase(radix: number): string;
10
13
  }
11
- /** Using this function you can chain operations (add, sub, div, mul). */
12
- export declare const pipe: Pipe;
package/src/pipe/main.js CHANGED
@@ -1,52 +1,67 @@
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
- };
6
1
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
2
  if (kind === "m") throw new TypeError("Private method is not writable");
8
3
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
4
  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");
10
5
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
6
  };
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
13
  import { bi2s, calcInner, s2bi } from "../shared/utils.js";
14
14
  import { divInner } from "../operations/div/utils.js";
15
- var Pipe = /** @class */ (function () {
16
- function Pipe() {
15
+ import { absInner } from "../abs/utils.js";
16
+ /** Using this function you can chain operations (add, sub, div, mul). */
17
+ export class Pipe {
18
+ constructor(value) {
17
19
  _Pipe_result.set(this, void 0);
20
+ value && (__classPrivateFieldSet(this, _Pipe_result, s2bi(value), "f"));
18
21
  }
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");
22
+ add(array) {
23
+ const arrayInner = array.map((str) => s2bi(str));
24
+ __classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, (a, b) => a + b, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
22
25
  return this;
23
- };
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");
26
+ }
27
+ sub(array) {
28
+ const arrayInner = array.map((str) => s2bi(str));
29
+ __classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, (a, b) => a - b, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
27
30
  return this;
28
- };
29
- Pipe.prototype.div = function (array, limit) {
30
- if (limit === void 0) { limit = 20; }
31
- var arrayInner = array.map(function (str) { return s2bi(str); });
31
+ }
32
+ div(array, limit = 20) {
33
+ const arrayInner = array.map((str) => s2bi(str));
32
34
  __classPrivateFieldSet(this, _Pipe_result, divInner(arrayInner, limit, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
33
35
  return this;
34
- };
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");
36
+ }
37
+ mul(array) {
38
+ const arrayInner = array.map((str) => s2bi(str));
39
+ __classPrivateFieldSet(this, _Pipe_result, calcInner(arrayInner, (a, b) => a * b, __classPrivateFieldGet(this, _Pipe_result, "f")), "f");
40
+ return this;
41
+ }
42
+ abs() {
43
+ if (!__classPrivateFieldGet(this, _Pipe_result, "f")) {
44
+ throw new Error("Cannot calculate the absolute value of an undefined.");
45
+ }
46
+ __classPrivateFieldSet(this, _Pipe_result, absInner(__classPrivateFieldGet(this, _Pipe_result, "f")), "f");
38
47
  return this;
39
- };
40
- Pipe.prototype.calc = function () {
48
+ }
49
+ calc() {
50
+ if (!__classPrivateFieldGet(this, _Pipe_result, "f")) {
51
+ throw new Error("Cannot calculate based on an undefined input.");
52
+ }
53
+ const [bi, fpe] = __classPrivateFieldGet(this, _Pipe_result, "f");
54
+ return bi2s(bi, fpe);
55
+ }
56
+ resultToBase(radix) {
41
57
  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");
45
- return result;
46
- };
47
- return Pipe;
48
- }());
49
- export { Pipe };
58
+ if (!__classPrivateFieldGet(this, _Pipe_result, "f")) {
59
+ throw new Error(`Cannot convert an undefined result to base ${radix}.`);
60
+ }
61
+ if (__classPrivateFieldGet(this, _Pipe_result, "f")[1] > 0) {
62
+ throw new Error(`Cannot convert non integer result to base ${radix}.`);
63
+ }
64
+ return (_a = __classPrivateFieldGet(this, _Pipe_result, "f")) === null || _a === void 0 ? void 0 : _a[0].toString(radix);
65
+ }
66
+ }
50
67
  _Pipe_result = new WeakMap();
51
- /** Using this function you can chain operations (add, sub, div, mul). */
52
- export var 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
- var PipeInner = /** @class */ (function () {
4
- function PipeInner() {
5
- }
6
- PipeInner.prototype.add = function (array) {
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
- PipeInner.prototype.sub = function (array) {
11
- this.result = calcInner(array, function (a, b) { return a - b; }, this.result);
8
+ }
9
+ sub(array) {
10
+ this.result = calcInner(array, (a, b) => a - b, this.result);
12
11
  return this;
13
- };
14
- PipeInner.prototype.div = function (array, limit) {
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
- PipeInner.prototype.mul = function (array) {
20
- this.result = calcInner(array, function (a, b) { return a * b; }, this.result);
16
+ }
17
+ mul(array) {
18
+ this.result = calcInner(array, (a, b) => a * b, this.result);
21
19
  return this;
22
- };
23
- PipeInner.prototype.calc = function () {
20
+ }
21
+ calc() {
24
22
  var _a;
25
- var res = (_a = this.result) !== null && _a !== void 0 ? _a : [0n, 0];
23
+ const res = (_a = this.result) !== null && _a !== void 0 ? _a : [0n, 0];
26
24
  return res;
27
- };
28
- return PipeInner;
29
- }());
30
- export { PipeInner };
25
+ }
26
+ }
@@ -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 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];
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),
@@ -1,17 +1,17 @@
1
1
  import { PipeInner } from "../pipe/utils.js";
2
- var meanQ = function (idx, array) {
3
- var left = new PipeInner().add([array[idx], array[idx - 1]]).calc();
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 var quartileInner = function (array) {
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
- var length = array.length;
11
- var Q2Idx = length >> 1;
12
- var Q1Idx = Q2Idx >> 1;
13
- var Q3Idx = length - Q1Idx;
14
- var isEvenHalf = Q2Idx % 2 !== 0;
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),
@@ -1,10 +1,10 @@
1
- export var UP = "up";
2
- export var DOWN = "down";
3
- export var HALF_UP = "half-up";
4
- export var HALF_DOWN = "half-down";
5
- export var HALF_EVEN = "half-even";
6
- export var HALF_ODD = "half-odd";
7
- export var ROUND_MODE = [
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 var round = function (value, options) {
5
- var _a;
6
- var _b, _c, _d, _e;
7
- var roundMode = (_b = options === null || options === void 0 ? void 0 : options.roundMode) !== null && _b !== void 0 ? _b : HALF_UP;
8
- var array = [];
9
- var sigFig = (_c = options === null || options === void 0 ? void 0 : options.sigFig) !== null && _c !== void 0 ? _c : false;
10
- var decimals = (_d = options === null || options === void 0 ? void 0 : options.decimals) !== null && _d !== void 0 ? _d : 0;
11
- var isFloat = false;
12
- var isNil = true;
13
- for (var i = 0; i < value.length; i++) {
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
- var next = (_e = value.charCodeAt(i + 1)) !== null && _e !== void 0 ? _e : 0;
25
- var prev = value.charCodeAt(i - 1);
26
- var curr = charCode === 46 ? prev : charCode;
27
- var _f = calcLast(curr, next, roundMode), last = _f[0], hasCarryOver = _f[1];
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
- _a = handleCarryOver(array, isFloat), array = _a[0], isFloat = _a[1];
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.apply(String, array);
43
+ : String.fromCharCode(...array);
45
44
  };
@@ -1,6 +1,6 @@
1
1
  import { DOWN, HALF_DOWN, HALF_EVEN, HALF_ODD, HALF_UP, UP, } from "./constants.js";
2
- export var handleTail = function (array, isFloat) {
3
- var lastIdx = array.length - 1;
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 var handleCarryOver = function (array, isFloat) {
11
+ export const handleCarryOver = (array, isFloat) => {
12
12
  var _a;
13
- var hasCarryOver = true;
14
- var idx = array.length - 1;
15
- var mapFloat = new Map([
16
- [57, function () {
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, function () {
20
+ [46, () => {
21
21
  array.pop();
22
22
  idx -= 1;
23
23
  isFloat = false;
24
24
  }],
25
- [0, function () {
25
+ [0, () => {
26
26
  array[idx] += 1;
27
27
  hasCarryOver = false;
28
28
  }],
29
29
  ]);
30
- var mapInt = new Map([
31
- [57, function () {
30
+ const mapInt = new Map([
31
+ [57, () => {
32
32
  array[idx] = 48;
33
33
  idx -= 1;
34
34
  }],
35
- [0, function () {
35
+ [0, () => {
36
36
  array[idx] += 1;
37
37
  hasCarryOver = false;
38
38
  }],
39
39
  ]);
40
40
  while (hasCarryOver && idx >= 0) {
41
- var map = isFloat ? mapFloat : mapInt;
42
- var key = map.has(array[idx]) ? array[idx] : 0;
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 var calcLast = function (current, next, roundMode) {
49
- var _a;
50
- var isEven = current % 2 === 1;
51
- var map = (_a = {},
52
- _a[UP] = current + (next > 48 ? 1 : 0),
53
- _a[DOWN] = current,
54
- _a[HALF_UP] = current + (next >= 53 ? 1 : 0),
55
- _a[HALF_DOWN] = current + (next > 53 ? 1 : 0),
56
- _a[HALF_EVEN] = current + ((isEven && next >= 53) ? 1 : 0),
57
- _a[HALF_ODD] = current + ((!isEven && next >= 53) ? 1 : 0),
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
  };
@@ -1,4 +1,4 @@
1
- export var NIL = {
1
+ export const NIL = {
2
2
  array: [48],
3
3
  isFloat: false,
4
4
  isNegative: false,
@@ -1,23 +1,23 @@
1
- export var bi2s = function (bigInt, fpe) {
1
+ export const bi2s = (bigInt, fpe) => {
2
2
  if (bigInt === 0n)
3
3
  return "0";
4
- var isNeg = bigInt < 0n;
4
+ const isNeg = bigInt < 0n;
5
5
  isNeg && (bigInt *= -1n);
6
- var dp = bigInt % (Math.pow(10n, BigInt(fpe)));
7
- var bigStr = bigInt.toString();
8
- var fpIdx = bigStr.length - fpe;
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
- var before = bigStr.slice(0, fpIdx);
12
- var after = bigStr.slice(fpIdx);
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("".concat(before, ".").concat(after)) : before);
15
+ (fpe > 0 && dp > 0 ? trimTail(`${before}.${after}`) : before);
16
16
  }
17
- return "".concat(fillHead(bigStr.length, fpe, isNeg, false)).concat(trimTail(after));
17
+ return `${fillHead(bigStr.length, fpe, isNeg, false)}${trimTail(after)}`;
18
18
  };
19
- export var s2bi = function (str) {
20
- var fpi = str.indexOf(".");
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 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];
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
- var fpDiff = dpLen - fpe;
45
- bigInt = op(bigInt * (Math.pow(10n, BigInt(fpDiff))), bigCurrent);
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 * (Math.pow(10n, BigInt(fpe - dpLen))));
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 var fillHead = function (len, fpe, isNeg, hasBefore) {
59
- var head = (isNeg ? "-" : "") + (hasBefore ? "" : "0.");
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 var trimTail = function (str) {
66
+ export const trimTail = (str) => {
67
67
  if (str[str.length - 1] !== "0")
68
68
  return str;
69
- var count = 0;
70
- for (var i = str.length - 1; i >= 0; i -= 1) {
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;
@@ -1,3 +1,3 @@
1
- export var ASC = "asc";
2
- export var DESC = "desc";
3
- export var sortingArray = [ASC, DESC];
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 var sort = function (array, sorting) {
6
- if (sorting === void 0) { sorting = ASC; }
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(function (_a) {
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
- var heapify = function (array, len, i, sorting) {
4
- var _a;
5
- var idxL = 2 * i + 1;
6
- var idxR = idxL + 1;
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
- _a = [array[idx], array[i]], array[i] = _a[0], array[idx] = _a[1];
20
+ [array[i], array[idx]] = [array[idx], array[i]];
22
21
  heapify(array, len, idx, sorting);
23
22
  }
24
23
  };
25
- export var sortInner = function (array, sorting) {
26
- var _a;
27
- var len = array.length;
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 (var i = len - 1; i >= 0; i--) {
32
- _a = [array[i], array[0]], array[0] = _a[0], array[i] = _a[1];
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,8 @@ 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 var sqrt = function (str, precision) {
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 });
5
+ export const sqrt = (value, precision) => {
6
+ const valueInner = s2bi(value);
7
+ const [[bi, fpe], decimals] = sqrtInner(valueInner, precision ? s2bi(precision) : undefined);
8
+ return round(bi2s(bi, fpe), { decimals });
10
9
  };
@@ -1,3 +1,3 @@
1
1
  import type { BI } from "../shared/types.d.ts";
2
- export type SqrtInner = (input: BI, prec?: BI, max?: number) => [BI, number];
3
- export type Sqrt = (str: string, precision?: string) => string;
2
+ export type SqrtInner = (value: BI, prec?: BI, max?: number) => [BI, number];
3
+ export type Sqrt = (value: string, precision?: string) => string;
package/src/sqrt/utils.js CHANGED
@@ -1,18 +1,14 @@
1
1
  import { isLeftGreaterInner } from "../compare/utils.js";
2
2
  import { PipeInner } from "../pipe/utils.js";
3
- var halfInner = [5n, 1];
4
- var precDef = [1n, 13];
5
- export var sqrtInner = function (input, prec, max) {
6
- if (prec === void 0) { prec = precDef; }
7
- if (max === void 0) { max = 100; }
8
- var guess = input;
9
- for (var i = 0; i < max; i++) {
10
- var nextGuess = new PipeInner()
11
- .div([input, guess])
3
+ export const sqrtInner = (value, prec = [1n, 13], max = 100) => {
4
+ let guess = value;
5
+ for (let i = 0; i < max; i++) {
6
+ const nextGuess = new PipeInner()
7
+ .div([value, guess])
12
8
  .add([guess])
13
- .mul([halfInner])
9
+ .mul([[5n, 1]])
14
10
  .calc();
15
- var _a = new PipeInner().sub([nextGuess, guess]).calc(), bi = _a[0], fpe = _a[1];
11
+ let [bi, fpe] = new PipeInner().sub([nextGuess, guess]).calc();
16
12
  if (bi < 0n)
17
13
  bi *= -1n;
18
14
  if (isLeftGreaterInner({ left: prec, right: [bi, fpe] })) {
@@ -0,0 +1,2 @@
1
+ import type { ToBase } from "./types.d.ts";
2
+ export declare const toBase: ToBase;
@@ -0,0 +1,3 @@
1
+ export const toBase = ({ value, toBase }) => {
2
+ return BigInt(value).toString(toBase);
3
+ };
@@ -0,0 +1,4 @@
1
+ export type ToBase = (arg: {
2
+ value: string;
3
+ toBase: number;
4
+ }) => string;