@numio/bigmath 2.1.1 → 2.1.3

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 (49) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/index.d.ts +20 -16
  3. package/index.js +20 -16
  4. package/package.json +1 -1
  5. package/src/abs/{main.d.ts → abs.d.ts} +1 -0
  6. package/src/abs/{main.js → abs.js} +1 -0
  7. package/src/cbrt/{main.js → cbrt.js} +1 -1
  8. package/src/compare/isEqual.d.ts +3 -0
  9. package/src/compare/isEqual.js +6 -0
  10. package/src/compare/isLeftGreater.d.ts +3 -0
  11. package/src/compare/isLeftGreater.js +6 -0
  12. package/src/compare/isLeftGreaterOrEqual.d.ts +3 -0
  13. package/src/compare/isLeftGreaterOrEqual.js +6 -0
  14. package/src/compare/max.d.ts +3 -0
  15. package/src/compare/max.js +8 -0
  16. package/src/compare/min.d.ts +3 -0
  17. package/src/compare/min.js +8 -0
  18. package/src/shared/utils.js +24 -18
  19. package/src/sqrt/{main.js → sqrt.js} +1 -1
  20. package/src/toBase/{main.d.ts → toBase.d.ts} +1 -0
  21. package/src/toBase/{main.js → toBase.js} +1 -0
  22. package/src/compare/main.d.ts +0 -11
  23. package/src/compare/main.js +0 -26
  24. package/src/shared/constant.d.ts +0 -6
  25. package/src/shared/constant.js +0 -6
  26. /package/src/IQR/{main.d.ts → iqr.d.ts} +0 -0
  27. /package/src/IQR/{main.js → iqr.js} +0 -0
  28. /package/src/MAD/{main.d.ts → mad.d.ts} +0 -0
  29. /package/src/MAD/{main.js → mad.js} +0 -0
  30. /package/src/cbrt/{main.d.ts → cbrt.d.ts} +0 -0
  31. /package/src/mean/{main.d.ts → mean.d.ts} +0 -0
  32. /package/src/mean/{main.js → mean.js} +0 -0
  33. /package/src/operations/add/{main.d.ts → add.d.ts} +0 -0
  34. /package/src/operations/add/{main.js → add.js} +0 -0
  35. /package/src/operations/div/{main.d.ts → div.d.ts} +0 -0
  36. /package/src/operations/div/{main.js → div.js} +0 -0
  37. /package/src/operations/mul/{main.d.ts → mul.d.ts} +0 -0
  38. /package/src/operations/mul/{main.js → mul.js} +0 -0
  39. /package/src/operations/sub/{main.d.ts → sub.d.ts} +0 -0
  40. /package/src/operations/sub/{main.js → sub.js} +0 -0
  41. /package/src/pipe/{main.d.ts → pipe.d.ts} +0 -0
  42. /package/src/pipe/{main.js → pipe.js} +0 -0
  43. /package/src/quartile/{main.d.ts → quartile.d.ts} +0 -0
  44. /package/src/quartile/{main.js → quartile.js} +0 -0
  45. /package/src/round/{main.d.ts → round.d.ts} +0 -0
  46. /package/src/round/{main.js → round.js} +0 -0
  47. /package/src/sort/{main.d.ts → sort.d.ts} +0 -0
  48. /package/src/sort/{main.js → sort.js} +0 -0
  49. /package/src/sqrt/{main.d.ts → sqrt.d.ts} +0 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,29 @@
1
+ ### 2.1.3
2
+ Optimize performance for small numbers
3
+
4
+ ### 2.1.2
5
+ Renamed main.ts files
6
+ Changed import path in test files
7
+ Change build config - run test on JSR and NPM versions
8
+
9
+ ### 2.1.1
10
+ Fixed lib crash (NPM version)
11
+
12
+ ### 2.1.0
13
+ Added `toBase` - convert number to another base
14
+ Added `abs` - absolute value
15
+ Added `isLeftGreaterOrEqual` - Is the left-hand side value greater than or equal to the right-hand side value
16
+ Added `cbrt` - cube root of a number
17
+
18
+ Added `Pipe().abs` - absolute value in pipe
19
+ Added `Pipe().resultToBase` - convert number to another base in pipe
20
+
21
+ ### 2.0.0
22
+ The performance was improved. Adding, subtracting, dividing, and multiplying are now **2** to **5** times faster than before.\
23
+ Adding, subtracting, dividing, and multiplying support operations on HEX, octal, binary, and decimal numbers. Mixed-type calculations are allowed, with the final result converted to decimal.
24
+
25
+ ### 1.1.0
26
+ Added `sqrt` - square root of a number\
27
+ Added `isEqual` and `isLeftGreater` - to compare 2 numbers.\
28
+ Added `MAD` - Median Absolute Deviation.\
29
+ Added `IQR` - Interquartile Range.
package/index.d.ts CHANGED
@@ -1,17 +1,21 @@
1
- import { add } from "./src/operations/add/main.d.ts";
2
- import { mul } from "./src/operations/mul/main.d.ts";
3
- import { sub } from "./src/operations/sub/main.d.ts";
4
- import { div } from "./src/operations/div/main.d.ts";
5
- import { round } from "./src/round/main.d.ts";
6
- import { Pipe } from "./src/pipe/main.d.ts";
7
- import { quartile } from "./src/quartile/main.d.ts";
8
- import { sort } from "./src/sort/main.d.ts";
9
- import { mean } from "./src/mean/main.d.ts";
10
- import { isEqual, isLeftGreater, isLeftGreaterOrEqual, max, min } from "./src/compare/main.d.ts";
11
- import { IQR } from "./src/IQR/main.d.ts";
12
- import { MAD } from "./src/MAD/main.d.ts";
13
- import { sqrt } from "./src/sqrt/main.d.ts";
14
- import { cbrt } from "./src/cbrt/main.d.ts";
15
- import { abs } from "./src/abs/main.d.ts";
16
- import { toBase } from "./src/toBase/main.d.ts";
1
+ import { add } from "./src/operations/add/add.d.ts";
2
+ import { mul } from "./src/operations/mul/mul.d.ts";
3
+ import { sub } from "./src/operations/sub/sub.d.ts";
4
+ import { div } from "./src/operations/div/div.d.ts";
5
+ import { round } from "./src/round/round.d.ts";
6
+ import { Pipe } from "./src/pipe/pipe.d.ts";
7
+ import { quartile } from "./src/quartile/quartile.d.ts";
8
+ import { sort } from "./src/sort/sort.d.ts";
9
+ import { mean } from "./src/mean/mean.d.ts";
10
+ import { isEqual } from "./src/compare/isEqual.d.ts";
11
+ import { min } from "./src/compare/min.d.ts";
12
+ import { max } from "./src/compare/max.d.ts";
13
+ import { isLeftGreaterOrEqual } from "./src/compare/isLeftGreaterOrEqual.d.ts";
14
+ import { isLeftGreater } from "./src/compare/isLeftGreater.d.ts";
15
+ import { IQR } from "./src/IQR/iqr.d.ts";
16
+ import { MAD } from "./src/MAD/mad.d.ts";
17
+ import { sqrt } from "./src/sqrt/sqrt.d.ts";
18
+ import { cbrt } from "./src/cbrt/cbrt.d.ts";
19
+ import { abs } from "./src/abs/abs.d.ts";
20
+ import { toBase } from "./src/toBase/toBase.d.ts";
17
21
  export { abs, add, cbrt, div, IQR, isEqual, isLeftGreater, isLeftGreaterOrEqual, MAD, max, mean, min, mul, Pipe, quartile, round, sort, sqrt, sub, toBase, };
package/index.js CHANGED
@@ -1,17 +1,21 @@
1
- import { add } from "./src/operations/add/main.js";
2
- import { mul } from "./src/operations/mul/main.js";
3
- import { sub } from "./src/operations/sub/main.js";
4
- import { div } from "./src/operations/div/main.js";
5
- import { round } from "./src/round/main.js";
6
- import { Pipe } from "./src/pipe/main.js";
7
- import { quartile } from "./src/quartile/main.js";
8
- import { sort } from "./src/sort/main.js";
9
- import { mean } from "./src/mean/main.js";
10
- import { isEqual, isLeftGreater, isLeftGreaterOrEqual, max, min, } from "./src/compare/main.js";
11
- import { IQR } from "./src/IQR/main.js";
12
- import { MAD } from "./src/MAD/main.js";
13
- import { sqrt } from "./src/sqrt/main.js";
14
- import { cbrt } from "./src/cbrt/main.js";
15
- import { abs } from "./src/abs/main.js";
16
- import { toBase } from "./src/toBase/main.js";
1
+ import { add } from "./src/operations/add/add.js";
2
+ import { mul } from "./src/operations/mul/mul.js";
3
+ import { sub } from "./src/operations/sub/sub.js";
4
+ import { div } from "./src/operations/div/div.js";
5
+ import { round } from "./src/round/round.js";
6
+ import { Pipe } from "./src/pipe/pipe.js";
7
+ import { quartile } from "./src/quartile/quartile.js";
8
+ import { sort } from "./src/sort/sort.js";
9
+ import { mean } from "./src/mean/mean.js";
10
+ import { isEqual } from "./src/compare/isEqual.js";
11
+ import { min } from "./src/compare/min.js";
12
+ import { max } from "./src/compare/max.js";
13
+ import { isLeftGreaterOrEqual } from "./src/compare/isLeftGreaterOrEqual.js";
14
+ import { isLeftGreater } from "./src/compare/isLeftGreater.js";
15
+ import { IQR } from "./src/IQR/iqr.js";
16
+ import { MAD } from "./src/MAD/mad.js";
17
+ import { sqrt } from "./src/sqrt/sqrt.js";
18
+ import { cbrt } from "./src/cbrt/cbrt.js";
19
+ import { abs } from "./src/abs/abs.js";
20
+ import { toBase } from "./src/toBase/toBase.js";
17
21
  export { abs, add, cbrt, div, IQR, isEqual, isLeftGreater, isLeftGreaterOrEqual, MAD, max, mean, min, mul, Pipe, quartile, round, sort, sqrt, sub, toBase, };
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.1.1",
4
+ "version": "2.1.3",
5
5
  "keywords": [
6
6
  "precision",
7
7
  "arithmetic",
@@ -1,2 +1,3 @@
1
1
  import type { Abs } from "./types.d.ts";
2
+ /** Get absolute value of a number */
2
3
  export declare const abs: Abs;
@@ -1,5 +1,6 @@
1
1
  import { bi2s, s2bi } from "../shared/utils.js";
2
2
  import { absInner } from "./utils.js";
3
+ /** Get absolute value of a number */
3
4
  export const abs = (value) => {
4
5
  const [bi, fpe] = absInner(s2bi(value));
5
6
  return bi2s(bi, fpe);
@@ -1,4 +1,4 @@
1
- import { round } from "../round/main.js";
1
+ import { round } from "../round/round.js";
2
2
  import { bi2s, s2bi } from "../shared/utils.js";
3
3
  import { cbrtInner } from "./utils.js";
4
4
  /** Find cube root of a number */
@@ -0,0 +1,3 @@
1
+ import type { IsEqual } from "./types.d.ts";
2
+ /** This function returns if left and right values are equal */
3
+ export declare const isEqual: IsEqual;
@@ -0,0 +1,6 @@
1
+ import { s2bi } from "../shared/utils.js";
2
+ import { isEqualInner } from "./utils.js";
3
+ /** This function returns if left and right values are equal */
4
+ export const isEqual = ({ left, right }) => {
5
+ return isEqualInner({ left: s2bi(left), right: s2bi(right) });
6
+ };
@@ -0,0 +1,3 @@
1
+ import type { IsLeftGreater } from "./types.d.ts";
2
+ /** This function returns if left value is greater than right value */
3
+ export declare const isLeftGreater: IsLeftGreater;
@@ -0,0 +1,6 @@
1
+ import { s2bi } from "../shared/utils.js";
2
+ import { isLeftGreaterInner } from "./utils.js";
3
+ /** This function returns if left value is greater than right value */
4
+ export const isLeftGreater = ({ left, right }) => {
5
+ return isLeftGreaterInner({ left: s2bi(left), right: s2bi(right) });
6
+ };
@@ -0,0 +1,3 @@
1
+ import type { IsLeftGreaterOrEqual } from "./types.d.ts";
2
+ /** This function returns if left value is greater than right value or equal*/
3
+ export declare const isLeftGreaterOrEqual: IsLeftGreaterOrEqual;
@@ -0,0 +1,6 @@
1
+ import { s2bi } from "../shared/utils.js";
2
+ import { isLeftGreaterOrEqualInner } from "./utils.js";
3
+ /** This function returns if left value is greater than right value or equal*/
4
+ export const isLeftGreaterOrEqual = ({ left, right }) => {
5
+ return isLeftGreaterOrEqualInner({ left: s2bi(left), right: s2bi(right) });
6
+ };
@@ -0,0 +1,3 @@
1
+ import type { Max } from "./types.d.ts";
2
+ /** This function returns max value. */
3
+ export declare const max: Max;
@@ -0,0 +1,8 @@
1
+ import { bi2s, s2bi } from "../shared/utils.js";
2
+ import { maxInner } from "./utils.js";
3
+ /** This function returns max value. */
4
+ export const max = (array) => {
5
+ const arrayInner = array.map((str) => s2bi(str));
6
+ const [bi, fpe] = maxInner(arrayInner);
7
+ return bi2s(bi, fpe);
8
+ };
@@ -0,0 +1,3 @@
1
+ import type { Min } from "./types.d.ts";
2
+ /** This function returns min value. */
3
+ export declare const min: Min;
@@ -0,0 +1,8 @@
1
+ import { bi2s, s2bi } from "../shared/utils.js";
2
+ import { minInner } from "./utils.js";
3
+ /** This function returns min value. */
4
+ export const min = (array) => {
5
+ const arrayInner = array.map((str) => s2bi(str));
6
+ const [bi, fpe] = minInner(arrayInner);
7
+ return bi2s(bi, fpe);
8
+ };
@@ -20,40 +20,46 @@ export const s2bi = (str) => {
20
20
  const fpi = str.indexOf(".");
21
21
  if (fpi === -1)
22
22
  return [BigInt(str), 0];
23
+ if (str.length < 15 && str[0] !== "0") {
24
+ return [
25
+ BigInt(+(str.slice(0, fpi) + str.slice(fpi + 1))),
26
+ str.length - 1 - fpi,
27
+ ];
28
+ }
23
29
  return [
24
30
  BigInt(str.slice(0, fpi) + str.slice(fpi + 1)),
25
- fpi === -1 ? 0 : str.length - 1 - fpi,
31
+ str.length - 1 - fpi,
26
32
  ];
27
33
  };
28
34
  export const calcInner = (array, op, def) => {
29
- let bigInt = def ? def[0] : array[0][0];
30
- let fpe = def ? def[1] : array[0][1];
35
+ let totalBi = def ? def[0] : array[0][0];
36
+ let totalFpe = def ? def[1] : array[0][1];
31
37
  const opm = op(1n, 1n);
32
38
  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);
39
+ const [bi, fpe] = array[i];
40
+ if (fpe === 0 && totalFpe === 0) {
41
+ totalBi = op(totalBi, bi);
36
42
  continue;
37
43
  }
38
44
  if (opm === 1n) {
39
- bigInt = op(bigInt, bigCurrent);
40
- fpe += dpLen;
45
+ totalBi = op(totalBi, bi);
46
+ totalFpe += fpe;
41
47
  }
42
48
  else {
43
- if (fpe < dpLen) {
44
- const fpDiff = dpLen - fpe;
45
- bigInt = op(bigInt * (10n ** BigInt(fpDiff)), bigCurrent);
49
+ if (totalFpe < fpe) {
50
+ const fpDiff = fpe - totalFpe;
51
+ totalBi = op(totalBi * (10n ** BigInt(fpDiff)), bi);
46
52
  }
47
- if (fpe > dpLen) {
48
- bigInt = op(bigInt, bigCurrent * (10n ** BigInt(fpe - dpLen)));
53
+ if (totalFpe > fpe) {
54
+ totalBi = op(totalBi, bi * (10n ** BigInt(totalFpe - fpe)));
49
55
  }
50
- if (fpe === dpLen)
51
- bigInt = op(bigInt, bigCurrent);
52
- if (fpe < dpLen)
53
- fpe = dpLen;
56
+ if (totalFpe === fpe)
57
+ totalBi = op(totalBi, bi);
58
+ if (totalFpe < fpe)
59
+ totalFpe = fpe;
54
60
  }
55
61
  }
56
- return [bigInt, fpe];
62
+ return [totalBi, totalFpe];
57
63
  };
58
64
  export const fillHead = (len, fpe, isNeg, hasBefore) => {
59
65
  let head = (isNeg ? "-" : "") + (hasBefore ? "" : "0.");
@@ -1,4 +1,4 @@
1
- import { round } from "../round/main.js";
1
+ import { round } from "../round/round.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 */
@@ -1,2 +1,3 @@
1
1
  import type { ToBase } from "./types.d.ts";
2
+ /** Convert number to another base */
2
3
  export declare const toBase: ToBase;
@@ -1,3 +1,4 @@
1
+ /** Convert number to another base */
1
2
  export const toBase = ({ value, toBase }) => {
2
3
  return BigInt(value).toString(toBase);
3
4
  };
@@ -1,11 +0,0 @@
1
- import type { IsEqual, IsLeftGreater, IsLeftGreaterOrEqual, Max, Min } from "./types.d.ts";
2
- /** This function returns max value. */
3
- export declare const max: Max;
4
- /** This function returns min value. */
5
- export declare const min: Min;
6
- /** This function returns if left value is greater than right value */
7
- export declare const isLeftGreater: IsLeftGreater;
8
- /** This function returns if left value is greater than right value */
9
- export declare const isLeftGreaterOrEqual: IsLeftGreaterOrEqual;
10
- /** This function returns if left and right values are equal */
11
- export declare const isEqual: IsEqual;
@@ -1,26 +0,0 @@
1
- import { bi2s, s2bi } from "../shared/utils.js";
2
- import { isEqualInner, isLeftGreaterInner, isLeftGreaterOrEqualInner, maxInner, minInner, } from "./utils.js";
3
- /** This function returns max value. */
4
- export const max = (array) => {
5
- const arrayInner = array.map((str) => s2bi(str));
6
- const [bi, fpe] = maxInner(arrayInner);
7
- return bi2s(bi, fpe);
8
- };
9
- /** This function returns min value. */
10
- export const min = (array) => {
11
- const arrayInner = array.map((str) => s2bi(str));
12
- const [bi, fpe] = minInner(arrayInner);
13
- return bi2s(bi, fpe);
14
- };
15
- /** This function returns if left value is greater than right value */
16
- export const isLeftGreater = ({ left, right }) => {
17
- return isLeftGreaterInner({ left: s2bi(left), right: s2bi(right) });
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
- };
23
- /** This function returns if left and right values are equal */
24
- export const isEqual = ({ left, right }) => {
25
- return isEqualInner({ left: s2bi(left), right: s2bi(right) });
26
- };
@@ -1,6 +0,0 @@
1
- export declare const NIL: {
2
- array: number[];
3
- isFloat: boolean;
4
- isNegative: boolean;
5
- intLength: number;
6
- };
@@ -1,6 +0,0 @@
1
- export const NIL = {
2
- array: [48],
3
- isFloat: false,
4
- isNegative: false,
5
- intLength: 1,
6
- };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes