big.esm 1.0.1 → 1.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.1.0](https://github.com/dschewchenko/big.esm/compare/v1.0.2...v1.1.0) (2023-05-14)
2
+
3
+
4
+ ### Features
5
+
6
+ * to string must return value without zeros by default ([1938f08](https://github.com/dschewchenko/big.esm/commit/1938f0885a478b768f90c797bae53b6320e8b683))
7
+
8
+ ## [1.0.2](https://github.com/dschewchenko/big.esm/compare/v1.0.1...v1.0.2) (2023-05-13)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * update readme and add benchmark ([8e9c2fc](https://github.com/dschewchenko/big.esm/commit/8e9c2fccf23887ca2699d6120dce42733e25945d))
14
+
1
15
  ## [1.0.1](https://github.com/dschewchenko/big.esm/compare/v1.0.0...v1.0.1) (2023-05-13)
2
16
 
3
17
 
package/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  Library for working with large numbers and fractions using `BigInt`.
4
4
  It provides advanced functionality for performing arithmetic operations with precision and decimal handling.
5
+ [Documentation](https://dschewchenko.github.io/big.esm/).
5
6
 
6
7
  100% test coverage.
7
8
 
@@ -13,10 +14,11 @@ Fully written in TypeScript, so it provides type definitions out of the box.
13
14
 
14
15
  Tree-shaking is supported.
15
16
 
16
- Full bundle size(ES) — 5.48 kB minified and 1.98 kB gzipped.
17
- `sqrtBig` and `powBig` are half of this size.
17
+ Full bundle size(ESM) — 3.12 kB minified and 1.42 kB gzipped.
18
+ `powBig` operation is half of this size, 1.5kB/0.7kB(minified/gzip),
19
+ because it's big and reuses most of the operations from `big.esm`.
18
20
 
19
- Full bundle size(UMD) — 3.52 kB minified and 1.58 kB gzipped.
21
+ Full bundle size(UMD) — 3.43 kB minified and 1.57 kB gzipped.
20
22
 
21
23
  ## Installation
22
24
 
@@ -63,26 +65,15 @@ Creates a new `Big` instance from a string, number or `BigInt`. Optionally, you
63
65
  Alias for `new Big(value)`.
64
66
  But it does not support the scale parameter and if the value is instance of `Big` it will be returned without changes.
65
67
 
66
- ```js
67
- const a = createBig("12345678910.12345678910"); // value = 1234567891012345678910 ,scale = 11
68
- const b = createBig(12345678910.12345678910); // value = 1234567891012345678910 ,scale = 11
69
- const c = createBig(1234567891012345678910n); // value = 1234567891012345678910 ,scale = 0
70
- ```
71
-
72
- ### `cloneBig(a: Big): Big`
68
+ #### `cloneBig(a: Big): Big`
73
69
 
74
70
  Creates a new `Big` instance from another `Big` instance.
75
71
 
76
- ```js
77
- const a = createBig("12345678910.12345678910");
78
- const b = cloneBig(a);
79
- ```
80
-
81
- ### `alignScale(a: Big, b: Big): [Big, Big]`
72
+ #### `alignScale(a: Big, b: Big): [Big, Big]`
82
73
 
83
74
  Aligns the scale of two `Big` instances. The scale of the result will be equal to the maximum scale of the two numbers.
84
75
 
85
- ### `isNumericValue(value: any): boolean`
76
+ #### `isNumericValue(value: any): boolean`
86
77
 
87
78
  Checks if the value is a valid numeric value for creating a `Big` instance.
88
79
 
@@ -120,6 +111,58 @@ Calculates the square root of `a`.
120
111
 
121
112
  Returns the absolute value of `a`.
122
113
 
114
+ #### `compareBig(a: Big, b: Big): -1 | 0 | 1`
115
+
116
+ Compares two `Big` instances. Returns `-1` if `a < b`, `0` if `a == b` and `1` if `a > b`.
117
+
118
+ ## Benchmark
119
+
120
+ Compare `big.esm` with `big.js`. 100_000 iterations for each operation.
121
+
122
+ | Operation | big.esm | big.js | Difference |
123
+ |--------------------------|-----------|------------|-----------------|
124
+ | init - smallInt | 39.856ms | 22.169ms | -79.78% slower |
125
+ | add - smallInt | 24.227ms | 15.125ms | -60.17% slower |
126
+ | subtract - smallInt | 22.181ms | 15.094ms | -46.95% slower |
127
+ | multiply - smallInt | 22.276ms | 18.538ms | -20.16% slower |
128
+ | divide - smallInt | 66.923ms | 179.699ms | 62.76% faster |
129
+ | power of 4 - smallInt | 113.714ms | 39.359ms | -188.92% slower |
130
+ | init - smallNumber | 44.606ms | 37.136ms | -20.12% slower |
131
+ | add - smallNumber | 21.719ms | 21.857ms | 0.63% faster |
132
+ | subtract - smallNumber | 21.090ms | 24.064ms | 12.36% faster |
133
+ | multiply - smallNumber | 23.246ms | 35.010ms | 33.60% faster |
134
+ | divide - smallNumber | 60.102ms | 189.603ms | 68.30% faster |
135
+ | power of 4 - smallNumber | 136.611ms | 68.859ms | -98.39% slower |
136
+ | init - bigInt | 50.278ms | 36.880ms | -36.33% slower |
137
+ | add - bigInt | 27.485ms | 25.402ms | -8.20% slower |
138
+ | subtract - bigInt | 27.402ms | 24.622ms | -11.29% slower |
139
+ | multiply - bigInt | 33.650ms | 121.126ms | 72.22% faster |
140
+ | divide - bigInt | 79.523ms | 602.879ms | 86.81% faster |
141
+ | power of 4 - bigInt | 248.514ms | 499.246ms | 50.22% faster |
142
+ | init - bigNumber | 55.661ms | 77.789ms | 28.45% faster |
143
+ | add - bigNumber | 29.755ms | 47.843ms | 37.81% faster |
144
+ | subtract - bigNumber | 29.107ms | 47.313ms | 38.48% faster |
145
+ | multiply - bigNumber | 39.976ms | 266.814ms | 85.02% faster |
146
+ | divide - bigNumber | 78.278ms | 931.610ms | 91.60% faster |
147
+ | power of 4 - bigNumber | 376.169ms | 1132.657ms | 66.79% faster |
148
+
149
+ #### Test data:
150
+
151
+ ```js
152
+ const smallIntA = 123;
153
+ const smallIntB = 456;
154
+ const smallNumberA = 123.456;
155
+ const smallNumberB = 789.012;
156
+ const bigIntA = "12345678901234567890";
157
+ const bigIntB = "67890123456789012345";
158
+ const bigNumberA = "12345678901234567890.1234567890";
159
+ const bigNumberB = "67890123456789012345.6789012345";
160
+ ```
161
+
162
+ Benchmarked on MacBook Pro M1 2021.
163
+
164
+ Code for benchmarking is in the `benchmark.js` file. You can run it with `npm run benchmark`.
165
+
123
166
  ## Roadmap
124
167
 
125
168
  - [x] Generate documentation from JSDoc
package/dist/big.d.ts CHANGED
@@ -57,9 +57,10 @@ export declare class Big {
57
57
  /**
58
58
  * Returns a string representation of the Big instance.
59
59
  *
60
+ * @param {boolean} [shouldTrimZeros=true] - Whether to trim trailing zeros from the fractional part. Defaults to true.
60
61
  * @return {string} - A string representation of the Big instance.
61
62
  */
62
- toString(): string;
63
+ toString(shouldTrimZeros?: boolean): string;
63
64
  /**
64
65
  * Returns the JSON representation of the Big instance.
65
66
  *
package/dist/big.esm.js CHANGED
@@ -1,11 +1,9 @@
1
- var y = Object.defineProperty;
2
- var T = (e, t, n) => t in e ? y(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var v = (e, t, n) => (T(e, typeof t != "symbol" ? t + "" : t, n), n);
4
- function D(e) {
5
- return typeof e == "bigint" || typeof e == "number" || typeof e == "string" && !isNaN(Number(e));
6
- }
7
- const p = 20, g = BigInt(0);
8
- class f {
1
+ var N = Object.defineProperty;
2
+ var T = (n, t, e) => t in n ? N(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
+ var m = (n, t, e) => (T(n, typeof t != "symbol" ? t + "" : t, e), e);
4
+ const b = (n) => typeof n == "bigint" || typeof n == "number" || typeof n == "string" && !isNaN(Number(n)), A = /0+$/, M = (n) => n.replace(A, "");
5
+ const g = BigInt(0);
6
+ class B {
9
7
  /**
10
8
  * Implements the overloaded constructors for the Big class.
11
9
  * @param {BigValue} value - The value to create the Big instance from. Can be a number, string, BigInt, or another Big instance.
@@ -13,23 +11,22 @@ class f {
13
11
  * @throws {TypeError} - If the value is not a number, string, BigInt, or Big instance.
14
12
  * @return {Big} - A new Big instance.
15
13
  */
16
- constructor(t, n) {
14
+ constructor(t, e) {
17
15
  /**
18
16
  * The value of the Big instance.
19
17
  */
20
- v(this, "value");
18
+ m(this, "value");
21
19
  /**
22
20
  * The scale of the Big instance, representing the number of decimal places.
23
21
  */
24
- v(this, "scale");
25
- if (t instanceof f)
22
+ m(this, "scale");
23
+ if (t instanceof B)
26
24
  this.value = t.value, this.scale = t.scale;
27
- else if (D(t)) {
25
+ else if (b(t)) {
28
26
  let [r, i = ""] = t.toString().split(".");
29
- n !== void 0 && (typeof n != "number" && (n = Number(n)), n = Math.floor(n), i && (i = ""), r.length < n && (r = r.padEnd(n, "0"))), this.value = BigInt(r + i), this.scale = i ? i.length : n ?? 0;
27
+ e !== void 0 && (typeof e != "number" && (e = Number(e)), e = Math.floor(e), i && (i = ""), r.length < e && (r = r.padEnd(e, "0"))), this.value = BigInt(r + i), this.scale = i ? i.length : e ?? 0;
30
28
  } else
31
29
  throw new TypeError("Invalid type provided to Big constructor.");
32
- return this;
33
30
  }
34
31
  /**
35
32
  * Returns the primitive value of the Big instance.
@@ -42,18 +39,19 @@ class f {
42
39
  /**
43
40
  * Returns a string representation of the Big instance.
44
41
  *
42
+ * @param {boolean} [shouldTrimZeros=true] - Whether to trim trailing zeros from the fractional part. Defaults to true.
45
43
  * @return {string} - A string representation of the Big instance.
46
44
  */
47
- toString() {
48
- let t = this.value.toString();
49
- const n = t.startsWith("-") ? "-" : "";
50
- n && (t = t.substring(1));
51
- let r = "";
45
+ toString(t = !0) {
46
+ let e = this.value.toString();
47
+ const r = e.startsWith("-") ? "-" : "";
48
+ r && (e = e.substring(1));
49
+ let i = "";
52
50
  if (this.scale > g) {
53
- const i = t.length > this.scale ? t.length - Number(this.scale) : 0;
54
- r = t.slice(i).padStart(Number(this.scale), "0"), r.length > 0 && (r = `.${r}`), t = t.slice(0, i);
51
+ const a = e.length > this.scale ? e.length - Number(this.scale) : 0;
52
+ i = e.slice(a).padStart(Number(this.scale), "0"), t && (i = M(i)), i.length > 0 && (i = `.${i}`), e = e.slice(0, a);
55
53
  }
56
- return `${n}${t || "0"}${r}`;
54
+ return `${r}${e || "0"}${i}`;
57
55
  }
58
56
  /**
59
57
  * Returns the JSON representation of the Big instance.
@@ -64,112 +62,106 @@ class f {
64
62
  return this.toString();
65
63
  }
66
64
  }
67
- function o(e, t) {
68
- return e instanceof f ? e : new f(e, t);
69
- }
70
- function m(e, t) {
71
- const n = e.scale - t.scale;
72
- if (n > 0) {
73
- const r = t.value * BigInt(10 ** n);
74
- return [e, o(r, e.scale)];
75
- }
76
- if (n < 0) {
77
- const r = e.value * BigInt(10 ** -n);
78
- return [o(r, t.scale), t];
79
- }
80
- return [e, t];
65
+ function o(n, t) {
66
+ return n instanceof B ? n : new B(n, t);
81
67
  }
82
- function S(e, t) {
83
- const [n, r] = m(e, t), i = n.value + r.value;
84
- return o(i, n.scale);
68
+ const O = BigInt(10);
69
+ function w(n, t) {
70
+ const e = n.scale - t.scale;
71
+ if (e === 0)
72
+ return [n, t];
73
+ const r = O ** BigInt(Math.abs(e)), i = e > 0 ? n : o(n.value * r, t.scale), a = e < 0 ? t : o(t.value * r, n.scale);
74
+ return [i, a];
85
75
  }
86
- function N(e, t) {
87
- const [n, r] = m(e, t), i = n.value - r.value;
88
- return o(i, n.scale);
76
+ function v(n, t) {
77
+ const [e, r] = w(n, t), i = e.value + r.value;
78
+ return o(i, e.scale);
89
79
  }
90
- function w(e, t) {
91
- const n = e.value * t.value, r = e.scale + t.scale;
92
- return o(n, r);
80
+ function E(n, t) {
81
+ const [e, r] = w(n, t), i = e.value - r.value;
82
+ return o(i, e.scale);
93
83
  }
94
- function d(e) {
95
- return e.value === g;
84
+ function d(n, t) {
85
+ const e = n.value * t.value, r = n.scale + t.scale;
86
+ return o(e, r);
96
87
  }
97
- function c(e, t, n = p, r = "half-up") {
98
- if (d(t))
88
+ const I = (n) => n.value === g;
89
+ function h(n, t, e = 20, r = "half-up") {
90
+ if (I(t))
99
91
  throw new Error("Division by zero is not allowed.");
100
- const i = Math.max(e.scale, t.scale), h = Math.max(0, n), B = e.value * BigInt(10) ** BigInt(i + h - e.scale), u = t.value * BigInt(10) ** BigInt(i - t.scale), a = B / u, l = B % u;
101
- let s;
92
+ const i = Math.max(n.scale, t.scale), a = Math.max(0, e), f = n.value * BigInt(10) ** BigInt(i + a - n.scale), s = t.value * BigInt(10) ** BigInt(i - t.scale), u = f / s, c = f % s;
93
+ let l;
102
94
  switch (r) {
103
95
  case "down":
104
- s = a;
96
+ l = u;
105
97
  break;
106
98
  case "up":
107
- s = l !== g ? a + BigInt(1) : a + g;
99
+ l = c !== g ? u + BigInt(1) : u + g;
108
100
  break;
109
101
  case "half-up":
110
102
  default:
111
- s = l * BigInt(2) >= u ? a + BigInt(1) : a;
103
+ l = c * BigInt(2) >= s ? u + BigInt(1) : u;
112
104
  break;
113
105
  }
114
- return o(s.toString(), h);
106
+ return o(l.toString(), a);
115
107
  }
116
- function V(e, t) {
117
- if (d(t))
108
+ function L(n, t) {
109
+ if (I(t))
118
110
  throw new Error("Division by zero is not allowed.");
119
- const n = c(e, t, 0, "down"), r = w(n, t);
120
- return N(e, r);
111
+ const e = h(n, t, 0, "down"), r = d(e, t);
112
+ return E(n, r);
121
113
  }
122
- function M(e, t) {
114
+ function y(n, t) {
123
115
  if (!Number.isInteger(t) || t < 0)
124
116
  throw new Error("The exponent must be a non-negative integer.");
125
- let n = o(1), r = o(e);
117
+ let e = o(1), r = o(n);
126
118
  for (t = Math.abs(t); t > 0; )
127
- t % 2 === 1 && (n = w(n, r)), r = w(r, r), t = Math.floor(t / 2);
128
- return n;
119
+ t % 2 === 1 && (e = d(e, r)), r = d(r, r), t = Math.floor(t / 2);
120
+ return e;
129
121
  }
130
- const O = (e, t) => {
131
- const [n, r] = m(e, t);
132
- return n.value > r.value ? 1 : n.value < r.value ? -1 : 0;
122
+ const D = (n, t) => {
123
+ const [e, r] = w(n, t);
124
+ return e.value > r.value ? 1 : e.value < r.value ? -1 : 0;
133
125
  };
134
- function A(e) {
135
- const t = e.value < BigInt(0) ? -e.value : e.value;
136
- return o(t, e.scale);
126
+ function P(n) {
127
+ const t = n.value < g ? -n.value : n.value;
128
+ return o(t, n.scale);
137
129
  }
138
- function $(e, t = 2, n = p) {
139
- if (d(e))
130
+ function _(n, t = 2, e = 20) {
131
+ if (I(n))
140
132
  return o(0);
141
- if (e.valueOf() === BigInt(1))
133
+ if (n.valueOf() === BigInt(1))
142
134
  return o(1);
143
- if (e.value < g && t % 2 === 0)
135
+ if (n.value < g && t % 2 === 0)
144
136
  throw new Error("The root must be an odd integer when the number is negative.");
145
137
  if (!Number.isInteger(t) || t < 2)
146
138
  throw new Error("The root must be an integer and greater than 1");
147
- if (n < 0)
139
+ if (e < 0)
148
140
  throw new Error("The precision must be a non-negative integer.");
149
- const r = n === 0 ? "1" : "0." + "0".repeat(n - 1) + "1", i = o(r), h = o(t), B = o(t - 1), u = o(2);
150
- let a = c(S(e, c(e, u, n)), u, n), l;
141
+ const r = o(e === 0 ? "1" : "0." + "0".repeat(e - 1) + "1"), i = o(t), a = o(t - 1), f = o(2);
142
+ let s = h(v(n, h(n, f, e)), f, e), u;
151
143
  do {
152
- l = a;
153
- const b = S(w(B, a), c(e, M(a, t - 1), n));
154
- a = c(b, h, n);
155
- } while (O(A(N(l, a)), i) === 1);
156
- const s = a.toString(), I = s.indexOf("."), E = I !== -1 ? s.slice(0, I + n + 1) : s;
157
- return o(E);
144
+ u = s;
145
+ const p = v(d(a, s), h(n, y(s, t - 1), e));
146
+ s = h(p, i, e);
147
+ } while (D(P(E(u, s)), r) === 1);
148
+ const c = s.toString(), l = c.indexOf("."), S = l !== -1 ? c.slice(0, l + e + 1) : c;
149
+ return o(S);
158
150
  }
159
- const k = (e) => new f(e.value, e.scale);
151
+ const $ = (n) => new B(n.value, n.scale);
160
152
  export {
161
- f as Big,
162
- A as absBig,
163
- S as addBig,
164
- m as alignScale,
165
- k as cloneBig,
166
- O as compareBig,
153
+ B as Big,
154
+ P as absBig,
155
+ v as addBig,
156
+ w as alignScale,
157
+ $ as cloneBig,
158
+ D as compareBig,
167
159
  o as createBig,
168
- c as divBig,
169
- D as isNumericValue,
170
- V as modBig,
171
- w as mulBig,
172
- M as powBig,
173
- $ as sqrtBig,
174
- N as subBig
160
+ h as divBig,
161
+ b as isNumericValue,
162
+ L as modBig,
163
+ d as mulBig,
164
+ y as powBig,
165
+ _ as sqrtBig,
166
+ E as subBig
175
167
  };
@@ -1 +1 @@
1
- (function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(r=typeof globalThis<"u"?globalThis:r||self,a((r.big=r.big||{},r.big.esm={})))})(this,function(r){"use strict";var j=Object.defineProperty;var q=(r,a,l)=>a in r?j(r,a,{enumerable:!0,configurable:!0,writable:!0,value:l}):r[a]=l;var y=(r,a,l)=>(q(r,typeof a!="symbol"?a+"":a,l),l);function a(e){return typeof e=="bigint"||typeof e=="number"||typeof e=="string"&&!isNaN(Number(e))}const l=20,B=BigInt(0);class g{constructor(t,n){y(this,"value");y(this,"scale");if(t instanceof g)this.value=t.value,this.scale=t.scale;else if(a(t)){let[i,u=""]=t.toString().split(".");n!==void 0&&(typeof n!="number"&&(n=Number(n)),n=Math.floor(n),u&&(u=""),i.length<n&&(i=i.padEnd(n,"0"))),this.value=BigInt(i+u),this.scale=u?u.length:n??0}else throw new TypeError("Invalid type provided to Big constructor.");return this}valueOf(){return this.value}toString(){let t=this.value.toString();const n=t.startsWith("-")?"-":"";n&&(t=t.substring(1));let i="";if(this.scale>B){const u=t.length>this.scale?t.length-Number(this.scale):0;i=t.slice(u).padStart(Number(this.scale),"0"),i.length>0&&(i=`.${i}`),t=t.slice(0,u)}return`${n}${t||"0"}${i}`}toJSON(){return this.toString()}}function o(e,t){return e instanceof g?e:new g(e,t)}function v(e,t){const n=e.scale-t.scale;if(n>0){const i=t.value*BigInt(10**n);return[e,o(i,e.scale)]}if(n<0){const i=e.value*BigInt(10**-n);return[o(i,t.scale),t]}return[e,t]}function S(e,t){const[n,i]=v(e,t),u=n.value+i.value;return o(u,n.scale)}function b(e,t){const[n,i]=v(e,t),u=n.value-i.value;return o(u,n.scale)}function d(e,t){const n=e.value*t.value,i=e.scale+t.scale;return o(n,i)}function N(e){return e.value===B}function f(e,t,n=l,i="half-up"){if(N(t))throw new Error("Division by zero is not allowed.");const u=Math.max(e.scale,t.scale),w=Math.max(0,n),I=e.value*BigInt(10)**BigInt(u+w-e.scale),h=t.value*BigInt(10)**BigInt(u-t.scale),s=I/h,m=I%h;let c;switch(i){case"down":c=s;break;case"up":c=m!==B?s+BigInt(1):s+B;break;case"half-up":default:c=m*BigInt(2)>=h?s+BigInt(1):s;break}return o(c.toString(),w)}function O(e,t){if(N(t))throw new Error("Division by zero is not allowed.");const n=f(e,t,0,"down"),i=d(n,t);return b(e,i)}function E(e,t){if(!Number.isInteger(t)||t<0)throw new Error("The exponent must be a non-negative integer.");let n=o(1),i=o(e);for(t=Math.abs(t);t>0;)t%2===1&&(n=d(n,i)),i=d(i,i),t=Math.floor(t/2);return n}const T=(e,t)=>{const[n,i]=v(e,t);return n.value>i.value?1:n.value<i.value?-1:0};function p(e){const t=e.value<BigInt(0)?-e.value:e.value;return o(t,e.scale)}function D(e,t=2,n=l){if(N(e))return o(0);if(e.valueOf()===BigInt(1))return o(1);if(e.value<B&&t%2===0)throw new Error("The root must be an odd integer when the number is negative.");if(!Number.isInteger(t)||t<2)throw new Error("The root must be an integer and greater than 1");if(n<0)throw new Error("The precision must be a non-negative integer.");const i=n===0?"1":"0."+"0".repeat(n-1)+"1",u=o(i),w=o(t),I=o(t-1),h=o(2);let s=f(S(e,f(e,h,n)),h,n),m;do{m=s;const V=S(d(I,s),f(e,E(s,t-1),n));s=f(V,w,n)}while(T(p(b(m,s)),u)===1);const c=s.toString(),M=c.indexOf("."),A=M!==-1?c.slice(0,M+n+1):c;return o(A)}const P=e=>new g(e.value,e.scale);r.Big=g,r.absBig=p,r.addBig=S,r.alignScale=v,r.cloneBig=P,r.compareBig=T,r.createBig=o,r.divBig=f,r.isNumericValue=a,r.modBig=O,r.mulBig=d,r.powBig=E,r.sqrtBig=D,r.subBig=b,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
1
+ (function(r,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(r=typeof globalThis<"u"?globalThis:r||self,s((r.big=r.big||{},r.big.esm={})))})(this,function(r){"use strict";var L=Object.defineProperty;var _=(r,s,g)=>s in r?L(r,s,{enumerable:!0,configurable:!0,writable:!0,value:g}):r[s]=g;var N=(r,s,g)=>(_(r,typeof s!="symbol"?s+"":s,g),g);const s=e=>typeof e=="bigint"||typeof e=="number"||typeof e=="string"&&!isNaN(Number(e)),g=/0+$/,M=e=>e.replace(g,""),h=BigInt(0);class B{constructor(n,t){N(this,"value");N(this,"scale");if(n instanceof B)this.value=n.value,this.scale=n.scale;else if(s(n)){let[i,o=""]=n.toString().split(".");t!==void 0&&(typeof t!="number"&&(t=Number(t)),t=Math.floor(t),o&&(o=""),i.length<t&&(i=i.padEnd(t,"0"))),this.value=BigInt(i+o),this.scale=o?o.length:t??0}else throw new TypeError("Invalid type provided to Big constructor.")}valueOf(){return this.value}toString(n=!0){let t=this.value.toString();const i=t.startsWith("-")?"-":"";i&&(t=t.substring(1));let o="";if(this.scale>h){const l=t.length>this.scale?t.length-Number(this.scale):0;o=t.slice(l).padStart(Number(this.scale),"0"),n&&(o=M(o)),o.length>0&&(o=`.${o}`),t=t.slice(0,l)}return`${i}${t||"0"}${o}`}toJSON(){return this.toString()}}function a(e,n){return e instanceof B?e:new B(e,n)}const O=BigInt(10);function I(e,n){const t=e.scale-n.scale;if(t===0)return[e,n];const i=O**BigInt(Math.abs(t)),o=t>0?e:a(e.value*i,n.scale),l=t<0?n:a(n.value*i,e.scale);return[o,l]}function S(e,n){const[t,i]=I(e,n),o=t.value+i.value;return a(o,t.scale)}function E(e,n){const[t,i]=I(e,n),o=t.value-i.value;return a(o,t.scale)}function w(e,n){const t=e.value*n.value,i=e.scale+n.scale;return a(t,i)}const b=e=>e.value===h;function d(e,n,t=20,i="half-up"){if(b(n))throw new Error("Division by zero is not allowed.");const o=Math.max(e.scale,n.scale),l=Math.max(0,t),v=e.value*BigInt(10)**BigInt(o+l-e.scale),u=n.value*BigInt(10)**BigInt(o-n.scale),c=v/u,m=v%u;let f;switch(i){case"down":f=c;break;case"up":f=m!==h?c+BigInt(1):c+h;break;case"half-up":default:f=m*BigInt(2)>=u?c+BigInt(1):c;break}return a(f.toString(),l)}function A(e,n){if(b(n))throw new Error("Division by zero is not allowed.");const t=d(e,n,0,"down"),i=w(t,n);return E(e,i)}function T(e,n){if(!Number.isInteger(n)||n<0)throw new Error("The exponent must be a non-negative integer.");let t=a(1),i=a(e);for(n=Math.abs(n);n>0;)n%2===1&&(t=w(t,i)),i=w(i,i),n=Math.floor(n/2);return t}const y=(e,n)=>{const[t,i]=I(e,n);return t.value>i.value?1:t.value<i.value?-1:0};function p(e){const n=e.value<h?-e.value:e.value;return a(n,e.scale)}function P(e,n=2,t=20){if(b(e))return a(0);if(e.valueOf()===BigInt(1))return a(1);if(e.value<h&&n%2===0)throw new Error("The root must be an odd integer when the number is negative.");if(!Number.isInteger(n)||n<2)throw new Error("The root must be an integer and greater than 1");if(t<0)throw new Error("The precision must be a non-negative integer.");const i=a(t===0?"1":"0."+"0".repeat(t-1)+"1"),o=a(n),l=a(n-1),v=a(2);let u=d(S(e,d(e,v,t)),v,t),c;do{c=u;const j=S(w(l,u),d(e,T(u,n-1),t));u=d(j,o,t)}while(y(p(E(c,u)),i)===1);const m=u.toString(),f=m.indexOf("."),R=f!==-1?m.slice(0,f+t+1):m;return a(R)}const D=e=>new B(e.value,e.scale);r.Big=B,r.absBig=p,r.addBig=S,r.alignScale=I,r.cloneBig=D,r.compareBig=y,r.createBig=a,r.divBig=d,r.isNumericValue=s,r.modBig=A,r.mulBig=w,r.powBig=T,r.sqrtBig=P,r.subBig=E,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
@@ -11,4 +11,4 @@ import type { Big } from "../big";
11
11
  * isZero(new Big(0)); // true
12
12
  * isZero(new Big(1)); // false
13
13
  */
14
- export declare function isZero(big: Big): boolean;
14
+ export declare const isZero: (big: Big) => boolean;
@@ -11,4 +11,4 @@
11
11
  * isNumericValue(1234567891012345678910n); // true
12
12
  * isNumericValue("123abc"); // false
13
13
  */
14
- export declare function isNumericValue(value: unknown): boolean;
14
+ export declare const isNumericValue: (value: unknown) => boolean;
@@ -0,0 +1 @@
1
+ export declare const trimZeros: (value: string) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "big.esm",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "A small, fast JavaScript library for arbitrary-precision decimal arithmetic.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -14,6 +14,7 @@
14
14
  "docs": "typedoc --out docs src/index.ts",
15
15
  "lint": "eslint --ext .ts,.js,.json ./src",
16
16
  "lint:fix": "eslint --fix --ext .ts,.js,.json ./src",
17
+ "benchmark": "node ./benchmark.js",
17
18
  "semantic-release": "semantic-release",
18
19
  "prepare": "is-ci || husky install"
19
20
  },
@@ -49,6 +50,7 @@
49
50
  "node": ">=16"
50
51
  },
51
52
  "devDependencies": {
53
+ "big.js": "^6.2.1",
52
54
  "@commitlint/cli": "^17.6.3",
53
55
  "@commitlint/config-conventional": "^17.6.3",
54
56
  "@semantic-release/changelog": "^6.0.3",