@numio/bigmath 2.1.2 → 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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ### 2.1.3
2
+ Optimize performance for small numbers
3
+
1
4
  ### 2.1.2
2
5
  Renamed main.ts files
3
6
  Changed import path in test files
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.2",
4
+ "version": "2.1.3",
5
5
  "keywords": [
6
6
  "precision",
7
7
  "arithmetic",
@@ -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.");