big.esm 1.1.2 → 1.2.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,16 @@
1
+ # [1.2.0](https://github.com/dschewchenko/big.esm/compare/v1.1.2...v1.2.0) (2023-05-19)
2
+
3
+
4
+ ### Features
5
+
6
+ * add new utility functions: from-string, is-big-value ([3eb674e](https://github.com/dschewchenko/big.esm/commit/3eb674ed53345537db203136ebb5879f8235d37b))
7
+ * make mutable option for math operations ([267aaef](https://github.com/dschewchenko/big.esm/commit/267aaef28af70c7c39e015e961c027bf8786569b))
8
+
9
+
10
+ ### Performance Improvements
11
+
12
+ * optimize big instance init for some input types ([9e9ddd2](https://github.com/dschewchenko/big.esm/commit/9e9ddd21adb5590a19395da5fb0b9a5ee66378e4))
13
+
1
14
  ## [1.1.2](https://github.com/dschewchenko/big.esm/compare/v1.1.1...v1.1.2) (2023-05-14)
2
15
 
3
16
 
package/README.md CHANGED
@@ -117,66 +117,13 @@ Compares two `Big` instances. Returns `-1` if `a < b`, `0` if `a == b` and `1` i
117
117
 
118
118
  ## Benchmark
119
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 | 40.267ms | 24.225ms | -66.22% slower |
125
- | add - smallInt | 24.570ms | 16.543ms | -48.52% slower |
126
- | subtract - smallInt | 22.152ms | 16.455ms | -34.63% slower |
127
- | multiply - smallInt | 23.394ms | 25.183ms | 7.10% faster |
128
- | divide - smallInt | 68.486ms | 232.593ms | 70.56% faster |
129
- | power of 4 - smallInt | 124.721ms | 63.314ms | -96.99% slower |
130
- | square root - smallInt | 5608.635ms | 1489.536ms | -276.54% slower |
131
- | init - smallNumber | 43.917ms | 38.125ms | -15.19% slower |
132
- | add - smallNumber | 22.342ms | 26.888ms | 16.91% faster |
133
- | subtract - smallNumber | 21.692ms | 27.138ms | 20.07% faster |
134
- | multiply - smallNumber | 27.452ms | 56.488ms | 51.40% faster |
135
- | divide - smallNumber | 62.497ms | 318.407ms | 80.37% faster |
136
- | power of 4 - smallNumber | 153.008ms | 156.773ms | 2.40% faster |
137
- | square root - smallNumber | 5557.749ms | 1558.616ms | -256.58% slower |
138
- | init - bigInt | 50.074ms | 36.019ms | -39.02% slower |
139
- | add - bigInt | 27.391ms | 24.467ms | -11.95% slower |
140
- | subtract - bigInt | 27.009ms | 24.022ms | -12.43% slower |
141
- | multiply - bigInt | 33.851ms | 120.812ms | 71.98% faster |
142
- | divide - bigInt | 77.744ms | 714.170ms | 89.11% faster |
143
- | power of 4 - bigInt | 248.630ms | 498.119ms | 50.09% faster |
144
- | square root - bigInt | 16917.234ms | 5955.270ms | -184.07% slower |
145
- | init - bigNumber | 60.159ms | 85.812ms | 29.89% faster |
146
- | add - bigNumber | 33.340ms | 56.803ms | 41.31% faster |
147
- | subtract - bigNumber | 34.255ms | 54.496ms | 37.14% faster |
148
- | multiply - bigNumber | 50.376ms | 452.010ms | 88.86% faster |
149
- | divide - bigNumber | 80.627ms | 1376.583ms | 94.14% faster |
150
- | power of 4 - bigNumber | 553.183ms | 2005.740ms | 72.42% faster |
151
- | square root - bigNumber | 17023.515ms | 5104.271ms | -233.52% slower |
152
-
153
- Note 1: Square root is slower because it uses Newton's method with variable root(quadratic, cubic, etc.ій) and high precision.
154
- The more precise the result, the more iterations are required.
155
-
156
- Node 2: On small numbers big.esm is slower because it uses bigint, which is slower on initialization.
157
- And each operation returns a new instance of Big, without mutating the original instance.
158
-
159
- #### Test data:
160
-
161
- ```js
162
- const smallIntA = 12345;
163
- const smallIntB = 45678;
164
- const smallNumberA = 12345.67809;
165
- const smallNumberB = 78901.23456;
166
- const bigIntA = "12345678901234567890";
167
- const bigIntB = "67890123456789012345";
168
- const bigNumberA = "12345678901234567890.12345678901234567890"
169
- const bigNumberB = "67890123456789012345.67890123456789012345";
170
- ```
171
-
172
- Benchmarked on MacBook Pro M1 2021.
173
-
174
- Code for benchmarking is in the `benchmark.js` file. You can run it with `npm run benchmark`.
120
+ Benchmark results are available [here](https://dschewchenko.github.io/big.esm/benchmark.html).
175
121
 
176
122
  ## Roadmap
177
123
 
178
124
  - [x] Generate documentation from JSDoc
179
- - [ ] Add formatting options and remove trailing zeros from toString method
125
+ - [x] Remove trailing zeros from toString method
126
+ - [ ] Add formatting options
180
127
  - [ ] Add more mathematical functions
181
128
 
182
129
  ## License
@@ -185,4 +132,4 @@ MIT
185
132
 
186
133
  ## Copyright
187
134
 
188
- © 2023 dschewchenko
135
+ © 2023 dschewchenko
package/dist/big.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { PossibleNumber } from "./types";
1
+ import type { BigObject, PossibleNumber } from "./types";
2
2
  /**
3
3
  * The Big class for working with large numbers and fractions using BigInt.
4
4
  *
@@ -18,19 +18,19 @@ export declare class Big {
18
18
  /**
19
19
  * The value of the Big instance.
20
20
  */
21
- readonly value: bigint;
21
+ value: bigint;
22
22
  /**
23
23
  * The scale of the Big instance, representing the number of decimal places.
24
24
  */
25
- readonly scale: number;
25
+ scale: number;
26
26
  /**
27
27
  * Creates a new Big instance.
28
28
  *
29
- * @param {Big} value - The value to create the Big instance from.
29
+ * @param {Big | BigObject} value - The value to create the Big instance from.
30
30
  * @throws {TypeError} - If the value is not a number, string, BigInt, or Big instance.
31
31
  * @return {Big} - A new Big instance.
32
32
  */
33
- constructor(value: Big);
33
+ constructor(value: Big | BigObject);
34
34
  /**
35
35
  * Creates a new Big instance.
36
36
  *
package/dist/big.esm.js CHANGED
@@ -1,9 +1,37 @@
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 {
1
+ var O = Object.defineProperty;
2
+ var M = (e, t, n) => t in e ? O(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var I = (e, t, n) => (M(e, typeof t != "symbol" ? t + "" : t, n), n);
4
+ const D = (e) => typeof e == "bigint" || typeof e == "number" || typeof e == "string" && !isNaN(Number(e)), A = /0+$/, R = (e) => e.replace(A, "");
5
+ const B = BigInt(0), d = BigInt(1), p = BigInt(10);
6
+ function a(e, t) {
7
+ return e instanceof v ? e : new v(e, t);
8
+ }
9
+ function _(e, t = !1, n = !0) {
10
+ const r = typeof e == "number" ? e.toString() : e;
11
+ if (!isFinite(Number(r)))
12
+ throw new Error("Invalid number");
13
+ if (Number(r) === 0)
14
+ return n ? a(B) : { value: B, scale: 0 };
15
+ let [s, i] = r.split("e");
16
+ const g = s.startsWith("-") ? "-" : "";
17
+ g && (s = s.slice(1));
18
+ let [u, l = ""] = s.split(".");
19
+ t && (l = "");
20
+ const o = parseInt(i);
21
+ if (o > 0) {
22
+ const f = Math.min(o, l.length);
23
+ u += l.slice(0, f), l = l.slice(f), f < o && (u += "0".repeat(o - f));
24
+ } else if (o < 0) {
25
+ const f = Math.min(-o, u.length);
26
+ l = u.slice(-f) + l, u = u.slice(0, u.length - f), f < -o && (l = "0".repeat(-o - f) + l);
27
+ }
28
+ const h = BigInt(g + u + l), c = l.length;
29
+ return n ? a(h, c) : { value: h, scale: c };
30
+ }
31
+ function j(e) {
32
+ return e === null || typeof e != "object" ? !1 : "value" in e && "scale" in e && typeof e.value == "bigint" && typeof e.scale == "number";
33
+ }
34
+ class v {
7
35
  /**
8
36
  * Implements the overloaded constructors for the Big class.
9
37
  * @param {BigValue} value - The value to create the Big instance from. Can be a number, string, BigInt, or another Big instance.
@@ -11,20 +39,25 @@ class B {
11
39
  * @throws {TypeError} - If the value is not a number, string, BigInt, or Big instance.
12
40
  * @return {Big} - A new Big instance.
13
41
  */
14
- constructor(t, e) {
42
+ constructor(t, n) {
15
43
  /**
16
44
  * The value of the Big instance.
17
45
  */
18
- m(this, "value");
46
+ I(this, "value");
19
47
  /**
20
48
  * The scale of the Big instance, representing the number of decimal places.
21
49
  */
22
- m(this, "scale");
23
- if (t instanceof B)
50
+ I(this, "scale");
51
+ if (t instanceof v || j(t))
24
52
  this.value = t.value, this.scale = t.scale;
25
- else if (b(t)) {
26
- let [r, i = ""] = t.toString().split(".");
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;
53
+ else if (D(t)) {
54
+ let r = n !== void 0;
55
+ if (r && (typeof n != "number" && (n = Number(n)), r = !isNaN(n), n = r ? Math.floor(n) : 0), typeof t == "bigint")
56
+ this.value = t, this.scale = r ? n : 0;
57
+ else {
58
+ const { value: s, scale: i } = _(t, r, !1);
59
+ this.value = s, this.scale = r ? n : i;
60
+ }
28
61
  } else
29
62
  throw new TypeError("Invalid type provided to Big constructor.");
30
63
  }
@@ -43,15 +76,15 @@ class B {
43
76
  * @return {string} - A string representation of the Big instance.
44
77
  */
45
78
  toString(t = !0) {
46
- let e = this.value.toString();
47
- const r = e.startsWith("-") ? "-" : "";
48
- r && (e = e.substring(1));
49
- let i = "";
50
- if (this.scale > g) {
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);
79
+ let n = this.value.toString();
80
+ const r = n.startsWith("-") ? "-" : "";
81
+ r && (n = n.substring(1));
82
+ let s = "";
83
+ if (this.scale > B) {
84
+ const i = n.length > this.scale ? n.length - Number(this.scale) : 0;
85
+ s = n.slice(i).padStart(Number(this.scale), "0"), t && (s = R(s)), s.length > 0 && (s = `.${s}`), n = n.slice(0, i);
53
86
  }
54
- return `${r}${e || "0"}${i}`;
87
+ return `${r}${n || "0"}${s}`;
55
88
  }
56
89
  /**
57
90
  * Returns the JSON representation of the Big instance.
@@ -62,106 +95,113 @@ class B {
62
95
  return this.toString();
63
96
  }
64
97
  }
65
- function o(n, t) {
66
- return n instanceof B ? n : new B(n, t);
67
- }
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];
98
+ function E(e, t, n = !1) {
99
+ const r = e.scale - t.scale;
100
+ if (r === 0)
101
+ return [e, t];
102
+ const s = p ** BigInt(Math.abs(r));
103
+ if (n)
104
+ return r > 0 ? (t.value *= s, t.scale = e.scale) : (e.value *= s, e.scale = t.scale), [e, t];
105
+ const i = r > 0 ? e : a(e.value * s, t.scale), g = r < 0 ? t : a(t.value * s, e.scale);
106
+ return [i, g];
75
107
  }
76
- function v(n, t) {
77
- const [e, r] = w(n, t), i = e.value + r.value;
78
- return o(i, e.scale);
108
+ function y(e, t, n = !1) {
109
+ const [r, s] = E(e, t), i = r.value + s.value;
110
+ return n ? (e.value = i, e.scale = r.scale, e) : a(i, r.scale);
79
111
  }
80
- function E(n, t) {
81
- const [e, r] = w(n, t), i = e.value - r.value;
82
- return o(i, e.scale);
112
+ function N(e, t, n = !1) {
113
+ const [r, s] = E(e, t, n), i = r.value - s.value;
114
+ return n ? (e.value = i, e) : a(i, r.scale);
83
115
  }
84
- function d(n, t) {
85
- const e = n.value * t.value, r = n.scale + t.scale;
86
- return o(e, r);
116
+ function w(e, t, n = !1) {
117
+ const r = e.value * t.value, s = e.scale + t.scale;
118
+ return n ? (e.value = r, e.scale = s, e) : a(r, s);
87
119
  }
88
- const I = (n) => n.value === g;
89
- function h(n, t, e = 20, r = "half-up") {
90
- if (I(t))
120
+ const S = (e) => e.value === B;
121
+ function m(e, t, n = 20, r = "half-up", s = !1) {
122
+ if (S(t))
91
123
  throw new Error("Division by zero is not allowed.");
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;
124
+ const i = Math.max(e.scale, t.scale), g = Math.max(0, n), u = e.value * p ** BigInt(i + g - e.scale), l = t.value * p ** BigInt(i - t.scale), o = u / l, h = u % l;
125
+ let c;
94
126
  switch (r) {
95
127
  case "down":
96
- l = u;
128
+ c = o;
97
129
  break;
98
130
  case "up":
99
- l = c !== g ? u + BigInt(1) : u + g;
131
+ c = h !== B ? o + d : o + B;
100
132
  break;
101
133
  case "half-up":
102
134
  default:
103
- l = c * BigInt(2) >= s ? u + BigInt(1) : u;
135
+ c = h * BigInt(2) >= l ? o + d : o;
104
136
  break;
105
137
  }
106
- return o(l.toString(), a);
138
+ return s ? (e.value = c, e.scale = g, e) : a(c, g);
107
139
  }
108
- function L(n, t) {
109
- if (I(t))
140
+ function G(e, t, n = !1) {
141
+ if (S(t))
110
142
  throw new Error("Division by zero is not allowed.");
111
- const e = h(n, t, 0, "down"), r = d(e, t);
112
- return E(n, r);
143
+ const r = m(e, t, 0, "down");
144
+ if (n)
145
+ return w(r, t, !0), N(e, r, !0);
146
+ const s = w(r, t);
147
+ return N(e, s);
113
148
  }
114
- function y(n, t) {
149
+ function b(e, t = 2, n = !1) {
115
150
  if (!Number.isInteger(t) || t < 0)
116
151
  throw new Error("The exponent must be a non-negative integer.");
117
- let e = o(1), r = o(n);
152
+ let r = a(1), s = a(e);
118
153
  for (t = Math.abs(t); t > 0; )
119
- t % 2 === 1 && (e = d(e, r)), r = d(r, r), t = Math.floor(t / 2);
120
- return e;
154
+ t % 2 === 1 && (r = w(r, s, n)), s = w(s, s, n), t = Math.floor(t / 2);
155
+ return n ? (e.value = r.value, e.scale = r.scale, e) : r;
121
156
  }
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;
157
+ const P = (e, t) => {
158
+ const [n, r] = E(e, t);
159
+ return n.value > r.value ? 1 : n.value < r.value ? -1 : 0;
125
160
  };
126
- function P(n) {
127
- const t = n.value < g ? -n.value : n.value;
128
- return o(t, n.scale);
161
+ function $(e, t = !1) {
162
+ const n = e.value < B ? -e.value : e.value;
163
+ return t ? (e.value = n, e) : a(n, e.scale);
129
164
  }
130
- function _(n, t = 2, e = 20) {
131
- if (I(n))
132
- return o(0);
133
- if (n.valueOf() === BigInt(1))
134
- return o(1);
135
- if (n.value < g && t % 2 === 0)
165
+ function Z(e, t = 2, n = 20, r = !1) {
166
+ if (S(e))
167
+ return a(0);
168
+ if (e.valueOf() === BigInt(1))
169
+ return a(1);
170
+ if (e.value < B && t % 2 === 0)
136
171
  throw new Error("The root must be an odd integer when the number is negative.");
137
172
  if (!Number.isInteger(t) || t < 2)
138
173
  throw new Error("The root must be an integer and greater than 1");
139
- if (e < 0)
174
+ if (n < 0)
140
175
  throw new Error("The precision must be a non-negative integer.");
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;
176
+ const s = a(n === 0 ? "1" : "0." + "0".repeat(n - 1) + "1"), i = a(t), g = a(t - 1), u = a(2);
177
+ let l = m(y(e, m(e, u, n)), u, n), o;
143
178
  do {
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);
179
+ o = l;
180
+ const T = y(w(g, l), m(e, b(l, t - 1), n));
181
+ l = m(T, i, n);
182
+ } while (P($(N(o, l)), s) === 1);
183
+ const h = l.toString();
184
+ if (r)
185
+ return e.value = l.value, e.scale = n, e;
186
+ const c = h.indexOf("."), f = c !== -1 ? h.slice(0, c + n + 1) : h;
187
+ return a(f);
150
188
  }
151
- const $ = (n) => new B(n.value, n.scale);
189
+ const k = (e) => new v(e.value, e.scale);
152
190
  export {
153
- B as Big,
154
- P as absBig,
155
- v as addBig,
156
- w as alignScale,
157
- $ as cloneBig,
158
- D as compareBig,
159
- o as createBig,
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
191
+ v as Big,
192
+ $ as absBig,
193
+ y as addBig,
194
+ E as alignScale,
195
+ k as cloneBig,
196
+ P as compareBig,
197
+ a as createBig,
198
+ m as divBig,
199
+ _ as fromString,
200
+ j as isBigObject,
201
+ D as isNumericValue,
202
+ G as modBig,
203
+ w as mulBig,
204
+ b as powBig,
205
+ Z as sqrtBig,
206
+ N as subBig
167
207
  };
@@ -1 +1 @@
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"})});
1
+ (function(s,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(s=typeof globalThis<"u"?globalThis:s||self,c((s.big=s.big||{},s.big.esm={})))})(this,function(s){"use strict";var G=Object.defineProperty;var V=(s,c,d)=>c in s?G(s,c,{enumerable:!0,configurable:!0,writable:!0,value:d}):s[c]=d;var O=(s,c,d)=>(V(s,typeof c!="symbol"?c+"":c,d),d);const c=e=>typeof e=="bigint"||typeof e=="number"||typeof e=="string"&&!isNaN(Number(e)),d=/0+$/,_=e=>e.replace(d,""),v=BigInt(0),b=BigInt(1),p=BigInt(10);function a(e,t){return e instanceof w?e:new w(e,t)}function M(e,t=!1,n=!0){const i=typeof e=="number"?e.toString():e;if(!isFinite(Number(i)))throw new Error("Invalid number");if(Number(i)===0)return n?a(v):{value:v,scale:0};let[r,u]=i.split("e");const B=r.startsWith("-")?"-":"";B&&(r=r.slice(1));let[f,l=""]=r.split(".");t&&(l="");const o=parseInt(u);if(o>0){const h=Math.min(o,l.length);f+=l.slice(0,h),l=l.slice(h),h<o&&(f+="0".repeat(o-h))}else if(o<0){const h=Math.min(-o,f.length);l=f.slice(-h)+l,f=f.slice(0,f.length-h),h<-o&&(l="0".repeat(-o-h)+l)}const m=BigInt(B+f+l),g=l.length;return n?a(m,g):{value:m,scale:g}}function j(e){return e===null||typeof e!="object"?!1:"value"in e&&"scale"in e&&typeof e.value=="bigint"&&typeof e.scale=="number"}class w{constructor(t,n){O(this,"value");O(this,"scale");if(t instanceof w||j(t))this.value=t.value,this.scale=t.scale;else if(c(t)){let i=n!==void 0;if(i&&(typeof n!="number"&&(n=Number(n)),i=!isNaN(n),n=i?Math.floor(n):0),typeof t=="bigint")this.value=t,this.scale=i?n:0;else{const{value:r,scale:u}=M(t,i,!1);this.value=r,this.scale=i?n:u}}else throw new TypeError("Invalid type provided to Big constructor.")}valueOf(){return this.value}toString(t=!0){let n=this.value.toString();const i=n.startsWith("-")?"-":"";i&&(n=n.substring(1));let r="";if(this.scale>v){const u=n.length>this.scale?n.length-Number(this.scale):0;r=n.slice(u).padStart(Number(this.scale),"0"),t&&(r=_(r)),r.length>0&&(r=`.${r}`),n=n.slice(0,u)}return`${i}${n||"0"}${r}`}toJSON(){return this.toString()}}function S(e,t,n=!1){const i=e.scale-t.scale;if(i===0)return[e,t];const r=p**BigInt(Math.abs(i));if(n)return i>0?(t.value*=r,t.scale=e.scale):(e.value*=r,e.scale=t.scale),[e,t];const u=i>0?e:a(e.value*r,t.scale),B=i<0?t:a(t.value*r,e.scale);return[u,B]}function y(e,t,n=!1){const[i,r]=S(e,t),u=i.value+r.value;return n?(e.value=u,e.scale=i.scale,e):a(u,i.scale)}function E(e,t,n=!1){const[i,r]=S(e,t,n),u=i.value-r.value;return n?(e.value=u,e):a(u,i.scale)}function I(e,t,n=!1){const i=e.value*t.value,r=e.scale+t.scale;return n?(e.value=i,e.scale=r,e):a(i,r)}const T=e=>e.value===v;function N(e,t,n=20,i="half-up",r=!1){if(T(t))throw new Error("Division by zero is not allowed.");const u=Math.max(e.scale,t.scale),B=Math.max(0,n),f=e.value*p**BigInt(u+B-e.scale),l=t.value*p**BigInt(u-t.scale),o=f/l,m=f%l;let g;switch(i){case"down":g=o;break;case"up":g=m!==v?o+b:o+v;break;case"half-up":default:g=m*BigInt(2)>=l?o+b:o;break}return r?(e.value=g,e.scale=B,e):a(g,B)}function P(e,t,n=!1){if(T(t))throw new Error("Division by zero is not allowed.");const i=N(e,t,0,"down");if(n)return I(i,t,!0),E(e,i,!0);const r=I(i,t);return E(e,r)}function D(e,t=2,n=!1){if(!Number.isInteger(t)||t<0)throw new Error("The exponent must be a non-negative integer.");let i=a(1),r=a(e);for(t=Math.abs(t);t>0;)t%2===1&&(i=I(i,r,n)),r=I(r,r,n),t=Math.floor(t/2);return n?(e.value=i.value,e.scale=i.scale,e):i}const A=(e,t)=>{const[n,i]=S(e,t);return n.value>i.value?1:n.value<i.value?-1:0};function R(e,t=!1){const n=e.value<v?-e.value:e.value;return t?(e.value=n,e):a(n,e.scale)}function $(e,t=2,n=20,i=!1){if(T(e))return a(0);if(e.valueOf()===BigInt(1))return a(1);if(e.value<v&&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 r=a(n===0?"1":"0."+"0".repeat(n-1)+"1"),u=a(t),B=a(t-1),f=a(2);let l=N(y(e,N(e,f,n)),f,n),o;do{o=l;const F=y(I(B,l),N(e,D(l,t-1),n));l=N(F,u,n)}while(A(R(E(o,l)),r)===1);const m=l.toString();if(i)return e.value=l.value,e.scale=n,e;const g=m.indexOf("."),h=g!==-1?m.slice(0,g+n+1):m;return a(h)}const q=e=>new w(e.value,e.scale);s.Big=w,s.absBig=R,s.addBig=y,s.alignScale=S,s.cloneBig=q,s.compareBig=A,s.createBig=a,s.divBig=N,s.fromString=M,s.isBigObject=j,s.isNumericValue=c,s.modBig=P,s.mulBig=I,s.powBig=D,s.sqrtBig=$,s.subBig=E,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
package/dist/index.d.ts CHANGED
@@ -9,7 +9,9 @@ export { powBig } from "./operations/pow";
9
9
  export { sqrtBig } from "./operations/sqrt";
10
10
  export { absBig } from "./operations/abs";
11
11
  export { compareBig } from "./operations/compare";
12
+ export { isBigObject } from "./utils/is-big-object";
12
13
  export { isNumericValue } from "./utils/numeric";
13
14
  export { alignScale } from "./utils/align-scale";
14
15
  export { createBig } from "./utils/create.ts";
15
16
  export { cloneBig } from "./utils/clone.ts";
17
+ export { fromString } from "./utils/from-string.ts";
@@ -3,6 +3,7 @@ import type { Big } from "../big";
3
3
  * Returns the absolute value of a Big instance.
4
4
  *
5
5
  * @param {Big} big - The Big instance.
6
+ * @param {boolean} [mutable=false] - Whether to mutate the Big instance. Defaults to false.
6
7
  * @returns {Big} The absolute value of the Big instance.
7
8
  *
8
9
  * @category Operations
@@ -12,4 +13,4 @@ import type { Big } from "../big";
12
13
  * absBig(new Big(-1.1)); // new Big instance with value 1.1
13
14
  * absBig(new Big(1)); // new Big instance with value 1
14
15
  */
15
- export declare function absBig(big: Big): Big;
16
+ export declare function absBig(big: Big, mutable?: boolean): Big;
@@ -4,6 +4,7 @@ import type { Big } from "../big";
4
4
  *
5
5
  * @param {Big} a - The first value to add.
6
6
  * @param {Big} b - The second value to add.
7
+ * @param {boolean} [mutable=false] - Whether to mutate the first value. Defaults to false.
7
8
  * @returns {Big} The sum of the two values.
8
9
  *
9
10
  * @category Operations
@@ -13,4 +14,4 @@ import type { Big } from "../big";
13
14
  * addBig(new Big(1.1), new Big(2.2)); // new Big instance with value 3.3
14
15
  * addBig(new Big(1.1), new Big(2)); // new Big instance with value 3.1
15
16
  */
16
- export declare function addBig(a: Big, b: Big): Big;
17
+ export declare function addBig(a: Big, b: Big, mutable?: boolean): Big;
@@ -7,6 +7,7 @@ import type { Big } from "../big";
7
7
  * @param {Big} divisor - The divisor Big instance.
8
8
  * @param {number} [precision] - The number of decimal places to keep in the result (optional). Defaults to the maximum scale of both Big instances. If the precision is lower than the scale of the result, the result will be rounded. If input numbers are without scale, the precision will be 0, so the result will be rounded to an integer.
9
9
  * @param {string} [roundingMode="half-up"] - The rounding mode to apply ("half-up", "down" or "up"). Defaults to "half-up".
10
+ * @param {boolean} [mutable=false] - Whether to mutate the dividend or not. Defaults to false.
10
11
  * @returns {Big} - A new Big instance representing the result of the division.
11
12
  * @throws {Error} Division by zero is not allowed.
12
13
  *
@@ -21,4 +22,4 @@ import type { Big } from "../big";
21
22
  * console.log(divBig(big1, big2, 2, "down").toString()); // "3.33"
22
23
  * console.log(divBig(big1, big2, 2, "up").toString()); // "3.34"
23
24
  */
24
- export declare function divBig(dividend: Big, divisor: Big, precision?: number, roundingMode?: RoundingMode): Big;
25
+ export declare function divBig(dividend: Big, divisor: Big, precision?: number, roundingMode?: RoundingMode, mutable?: boolean): Big;
@@ -4,6 +4,7 @@ import type { Big } from "../big";
4
4
  *
5
5
  * @param dividend The Big instance representing the dividend.
6
6
  * @param divisor The Big instance representing the divisor.
7
+ * @param {boolean} [mutable=false] - Whether to mutate the first Big instance. Defaults to false.
7
8
  * @returns A new Big instance representing the remainder of the division.
8
9
  * @throws {Error} Division by zero is not allowed.
9
10
  *
@@ -16,4 +17,4 @@ import type { Big } from "../big";
16
17
  * console.log(modBig(big2, big1).toString()); // "3"
17
18
  * console.log(modBig(big1, big1).toString()); // "0"
18
19
  */
19
- export declare function modBig(dividend: Big, divisor: Big): Big;
20
+ export declare function modBig(dividend: Big, divisor: Big, mutable?: boolean): Big;
@@ -4,6 +4,7 @@ import type { Big } from "../big";
4
4
  *
5
5
  * @param {Big} a - The first Big instance.
6
6
  * @param {Big} b - The second Big instance.
7
+ * @param {boolean} [mutable=false] - Whether to mutate the first Big instance. Defaults to false.
7
8
  * @returns {Big} The result of the multiplication.
8
9
  *
9
10
  * @category Operations
@@ -13,4 +14,4 @@ import type { Big } from "../big";
13
14
  * mulBig(new Big(1.1), new Big(2.2)); // new Big instance with value 2.42
14
15
  * mulBig(new Big(1.1), new Big(2)); // new Big instance with value 2.2
15
16
  */
16
- export declare function mulBig(a: Big, b: Big): Big;
17
+ export declare function mulBig(a: Big, b: Big, mutable?: boolean): Big;
@@ -3,7 +3,8 @@ import type { Big } from "../big";
3
3
  * Raises a Big number to the power of another Big number.
4
4
  *
5
5
  * @param {Big} big - The base Big number.
6
- * @param {number} exp - The exponent (non-negative integer).
6
+ * @param {number} exp - The exponent (non-negative integer). Defaults to 2.
7
+ * @param {boolean} [mutable=false] - Whether to mutate the first Big instance. Defaults to false.
7
8
  * @returns {Big} - The result of a raised to the power of b.
8
9
  * @throws {Error} - If the exponent is not a non-negative integer.
9
10
  *
@@ -16,4 +17,4 @@ import type { Big } from "../big";
16
17
  * powBig(new Big(2), 3.3); // throws Error
17
18
  * powBig(new Big(2), -3); // throws Error
18
19
  */
19
- export declare function powBig(big: Big, exp: number): Big;
20
+ export declare function powBig(big: Big, exp?: number, mutable?: boolean): Big;
@@ -7,6 +7,7 @@ import type { Big } from "../big";
7
7
  * @param {Big} big - The Big instance.
8
8
  * @param {number} root - The root to calculate. Must be an integer. Defaults to 2.
9
9
  * @param {number} precision - The number of decimal places to keep in the result (optional). Defaults to 20.
10
+ * @param {boolean} [mutable=false] - Whether to mutate the first Big instance. Defaults to false.
10
11
  * @returns {Big} The nth root of the Big instance with the specified precision and rounding mode.
11
12
  * @throws {Error} - If the root is not an integer.
12
13
  *
@@ -24,4 +25,4 @@ import type { Big } from "../big";
24
25
  * console.log(sqrtBig(Big(16), -2).toString()); // throws Error
25
26
  * console.log(sqrtBig(Big(16), 4, -1).toString()); // throws Error
26
27
  */
27
- export declare function sqrtBig(big: Big, root?: number, precision?: number): Big;
28
+ export declare function sqrtBig(big: Big, root?: number, precision?: number, mutable?: boolean): Big;
@@ -4,6 +4,7 @@ import type { Big } from "../big";
4
4
  *
5
5
  * @param {Big} a - The first Big instance.
6
6
  * @param {Big} b - The second Big instance.
7
+ * @param {boolean} [mutable=false] - Whether to mutate the first Big instance. Defaults to false.
7
8
  * @returns {Big} The result of the subtraction.
8
9
  *
9
10
  * @category Operations
@@ -12,4 +13,4 @@ import type { Big } from "../big";
12
13
  * subBig(new Big(1), new Big(2)); // new Big instance with value -1
13
14
  * subBig(new Big(1.1), new Big(2.2)); // new Big instance with value -1.1
14
15
  */
15
- export declare function subBig(a: Big, b: Big): Big;
16
+ export declare function subBig(a: Big, b: Big, mutable?: boolean): Big;
package/dist/types.d.ts CHANGED
@@ -1,4 +1,13 @@
1
1
  import type { Big } from "./big";
2
+ /**
3
+ * The object type for the Big class.
4
+ *
5
+ * @category Types
6
+ */
7
+ export type BigObject = {
8
+ value: bigint;
9
+ scale: number;
10
+ };
2
11
  /**
3
12
  * Possible number types for processing.
4
13
  *
@@ -10,7 +19,7 @@ export type PossibleNumber = number | string | bigint;
10
19
  *
11
20
  * @category Types
12
21
  */
13
- export type BigValue = PossibleNumber | Big;
22
+ export type BigValue = PossibleNumber | Big | BigObject;
14
23
  /**
15
24
  * The rounding mode for division operations.
16
25
  *
@@ -9,6 +9,7 @@ import type { Big } from "../big";
9
9
  *
10
10
  * @param {Big} a - The first Big instance.
11
11
  * @param {Big} b - The second Big instance.
12
+ * @param {boolean} [mutable=false] - Whether to mutate the both Big instances. Defaults to false.
12
13
  * @returns {[Big, Big]} A tuple of two Big instances with the same scale.
13
14
  *
14
15
  * @category Utilities
@@ -20,4 +21,4 @@ import type { Big } from "../big";
20
21
  * console.log(x.toString()); // "123.456000"
21
22
  * console.log(y.toString()); // "123.456789"
22
23
  */
23
- export declare function alignScale(a: Big, b: Big): [Big, Big];
24
+ export declare function alignScale(a: Big, b: Big, mutable?: boolean): [Big, Big];
@@ -10,3 +10,16 @@ export declare const DEFAULT_PRECISION = 20;
10
10
  * @category Constants
11
11
  */
12
12
  export declare const ZERO_BIGINT: bigint;
13
+ /**
14
+ * One as a BigInt.
15
+ *
16
+ * @category Constants
17
+ */
18
+ export declare const ONE_BIGINT: bigint;
19
+ /**
20
+ * Ten as a BigInt.
21
+ * Often used as scale multiplier to adjust the scale of a Big instance.
22
+ *
23
+ * @category Constants
24
+ */
25
+ export declare const TEN_BIGINT: bigint;
@@ -1,14 +1,14 @@
1
1
  import { Big } from "../big.ts";
2
- import type { PossibleNumber } from "../types.ts";
2
+ import type { BigObject, PossibleNumber } from "../types.ts";
3
3
  /**
4
4
  * Creates a new Big instance from the given value.
5
5
  * If the value is already a Big instance, it returns the value itself.
6
6
  * Use this function instead of the Big constructor to create a Big instance from a value.
7
7
  *
8
- * @param {Big} value - The value to create the Big instance from.
8
+ * @param {Big | BigObject} value - The value to create the Big instance from.
9
9
  * @return {Big} - A new Big instance.
10
10
  */
11
- export declare function createBig(value: Big): Big;
11
+ export declare function createBig(value: Big | BigObject): Big;
12
12
  /**
13
13
  * Creates a new Big instance from the given value.
14
14
  * If the value is already a Big instance, it returns the value itself.
@@ -27,6 +27,8 @@ export declare function createBig(value: PossibleNumber): Big;
27
27
  * @param {number | string | undefined} [scale] - The scale for the value (optional), representing the number of decimal places. If scale provided and value has a fractional part, the fractional part will be removed.
28
28
  * @return {Big} - A new Big instance.
29
29
  *
30
+ * @category Utilities
31
+ *
30
32
  * @example
31
33
  * const big1 = createBig(123.456); // 123.456
32
34
  * const big2 = createBig("123.456"); // 123.456
@@ -0,0 +1,23 @@
1
+ import { Big } from "../big.ts";
2
+ import { BigObject } from "../types.ts";
3
+ /**
4
+ * Convert a number or string to normal string number. Supports scientific notation.
5
+ *
6
+ * @param {string | number} num - The number to convert
7
+ * @param {boolean} [ignoreFraction=false] - Whether to ignore the fractional part of the number. Defaults to false.
8
+ * @param {boolean} [returnInstance=false] - Whether to return a Big instance or BigObject. Defaults to true.
9
+ *
10
+ * @return {Big | BigObject} - A Big instance or BigObject representing the number
11
+ * @throws {Error} - If the number is too big or too small
12
+ * @throws {Error} - If the number is invalid
13
+ *
14
+ * @category Utilities
15
+ *
16
+ * @example
17
+ * fromString("123.456"); // Big { value: 123456n, scale: 3 }
18
+ * fromString("123.456", true); // Big { value: 123n, scale: 0 }
19
+ * fromString("123.456", false, false); // { value: 123456n, scale: 3 }
20
+ * fromString("1.5e3"); // Big { value: 1500n, scale: 0 }
21
+ *
22
+ */
23
+ export declare function fromString<Type extends boolean>(num: string | number, ignoreFraction?: boolean, returnInstance?: Type): Type extends true ? Big : BigObject;
@@ -0,0 +1,20 @@
1
+ import type { BigObject } from "../types.ts";
2
+ import type { Big } from "../big.ts";
3
+ /**
4
+ * Determines if the value is a BigObject. A BigObject is an object with a value property of type bigint and a scale property of type number. It usually represents a Big instance.
5
+ *
6
+ * @param {Big | BigObject} value - The value to check. Can be a Big instance or a BigObject.
7
+ * @return {boolean} - True if the value is a BigObject, false otherwise.
8
+ *
9
+ * @category Utilities
10
+ *
11
+ * @example
12
+ * isBigObject({ value: 123456n, scale: 2 }); // true
13
+ * isBigObject({ value: 123456n, scale: 2, foo: "bar" }); // true
14
+ * isBigObject({ value: 123456n }); // false
15
+ * isBigObject({ scale: 2 }); // false
16
+ * isBigObject({}); // false
17
+ * isBigObject(123456n); // false
18
+ * isBigObject({ value: "abc", scale: "2"}); // false
19
+ */
20
+ export declare function isBigObject(value: Big | BigObject | unknown): value is BigObject;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "big.esm",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "A small, fast JavaScript library for arbitrary-precision decimal arithmetic.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -11,10 +11,10 @@
11
11
  "test:ui": "vitest --ui",
12
12
  "test:run": "vitest run",
13
13
  "coverage": "vitest run --coverage",
14
- "docs": "typedoc --out docs src/index.ts",
14
+ "docs": "typedoc --out docs",
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
+ "benchmark": "node --expose-gc ./benchmark.js",
18
18
  "semantic-release": "semantic-release",
19
19
  "prepare": "is-ci || husky install"
20
20
  },
@@ -50,7 +50,6 @@
50
50
  "node": ">=16"
51
51
  },
52
52
  "devDependencies": {
53
- "big.js": "^6.2.1",
54
53
  "@commitlint/cli": "^17.6.3",
55
54
  "@commitlint/config-conventional": "^17.6.3",
56
55
  "@semantic-release/changelog": "^6.0.3",
@@ -61,6 +60,7 @@
61
60
  "@typescript-eslint/parser": "^5.59.5",
62
61
  "@vitest/coverage-c8": "^0.31.0",
63
62
  "@vitest/ui": "^0.31.0",
63
+ "big.js": "^6.2.1",
64
64
  "eslint": "^8.40.0",
65
65
  "eslint-config-prettier": "^8.8.0",
66
66
  "eslint-plugin-prettier": "^4.2.1",