big.esm 1.3.1 → 2.0.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/dist/big.esm.js CHANGED
@@ -1,37 +1,42 @@
1
- var x = Object.defineProperty;
2
- var A = (e, t, n) => t in e ? x(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var I = (e, t, n) => (A(e, typeof t != "symbol" ? t + "" : t, n), n);
4
- const M = (e) => typeof e == "bigint" || typeof e == "number" || typeof e == "string" && !isNaN(Number(e)), _ = /0+$/, R = (e) => e.replace(_, "");
5
- const m = BigInt(0), y = BigInt(1), p = BigInt(10);
6
- function o(e, t) {
7
- return e instanceof w ? e : new w(e, t);
8
- }
9
- function j(e, t = !1, n = !0) {
10
- const r = e.toString();
11
- if (!Number.isFinite(Number(r)))
12
- throw new Error("Invalid number");
13
- if (Number(r) === 0)
14
- return n ? o(m) : { value: m, scale: 0 };
15
- let [s, i] = r.split("e");
16
- const g = s.startsWith("-") ? "-" : "";
17
- s = g ? s.slice(1) : s;
18
- let [u, l = ""] = s.split(".");
19
- l = t ? "" : l;
20
- const a = Number.parseInt(i);
21
- if (a > 0) {
22
- const f = Math.min(a, l.length);
23
- u += l.slice(0, f), l = l.slice(f), f < a && (u += "0".repeat(a - f));
24
- } else if (a < 0) {
25
- const f = Math.min(-a, u.length);
26
- l = u.slice(-f) + l, u = u.slice(0, u.length - f), f < -a && (l = "0".repeat(-a - f) + l);
27
- }
28
- const h = BigInt(g + u + l), c = l.length;
29
- return n ? o(h, c) : { value: h, scale: c };
30
- }
31
- function P(e) {
32
- return typeof e != "object" || e === null ? !1 : "value" in e && "scale" in e && typeof e.value == "bigint" && typeof e.scale == "number";
33
- }
34
- class w {
1
+ const h = 0n, m = 1n, I = 10n;
2
+ function D(e) {
3
+ if (e === null || typeof e != "object") return !1;
4
+ const t = e;
5
+ return typeof t.value == "bigint" && typeof t.scale == "number";
6
+ }
7
+ function f(e, t) {
8
+ return e instanceof B ? e : D(e) ? new B(e.value, e.scale) : t === void 0 ? new B(e) : new B(e, t);
9
+ }
10
+ function O(e, t = !1, n = !0) {
11
+ const r = typeof e == "number" ? String(e) : e, s = /^(-)?(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/.exec(r);
12
+ if (!s) throw new Error("Num");
13
+ const l = s[1] ? "-" : "";
14
+ let u = s[2] || "", i = s[3] || "";
15
+ if (u.length === 0 && i.length === 0)
16
+ throw new Error("Num");
17
+ t && (i = "");
18
+ const c = Number.parseInt(s[4] || "0", 10);
19
+ if (!Number.isFinite(c)) throw new Error("Exp");
20
+ if (c > 0) {
21
+ const w = Math.min(c, i.length);
22
+ u += i.slice(0, w), i = i.slice(w), w < c && (u += "0".repeat(c - w));
23
+ } else if (c < 0) {
24
+ const w = Math.min(-c, u.length);
25
+ i = u.slice(-w) + i, u = u.slice(0, u.length - w), w < -c && (i = "0".repeat(-c - w) + i);
26
+ }
27
+ const o = BigInt(l + u + i), a = i.length;
28
+ return o === h ? n ? f(h) : { value: h, scale: 0 } : n ? f(o, a) : { value: o, scale: a };
29
+ }
30
+ const T = /0+$/, _ = (e) => e.replace(T, "");
31
+ class B {
32
+ /**
33
+ * The value of the Big instance.
34
+ */
35
+ value;
36
+ /**
37
+ * The scale of the Big instance, representing the number of decimal places.
38
+ */
39
+ scale;
35
40
  /**
36
41
  * Implements the overloaded constructors for the Big class.
37
42
  * @param {BigValue} value - The value to create the Big instance from. Can be a number, string, BigInt, or another Big instance.
@@ -40,26 +45,27 @@ class w {
40
45
  * @return {Big} - A new Big instance.
41
46
  */
42
47
  constructor(t, n) {
43
- /**
44
- * The value of the Big instance.
45
- */
46
- I(this, "value");
47
- /**
48
- * The scale of the Big instance, representing the number of decimal places.
49
- */
50
- I(this, "scale");
51
- if (P(t))
48
+ if (t instanceof B) {
49
+ this.value = t.value, this.scale = t.scale;
50
+ return;
51
+ }
52
+ if (D(t)) {
52
53
  this.value = t.value, this.scale = t.scale;
53
- else if (M(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 } = j(t, r, !1);
59
- this.value = s, this.scale = r ? n : i;
60
- }
61
- } else
62
- throw new TypeError("Invalid type provided to Big constructor.");
54
+ return;
55
+ }
56
+ if (typeof t == "object" && t !== null)
57
+ throw new TypeError("Big");
58
+ let r = n !== void 0, s = 0;
59
+ if (r && (s = typeof n == "number" ? n : Number(n), r = !Number.isNaN(s), r && (s = Math.floor(s))), typeof t == "bigint") {
60
+ this.value = t, this.scale = r ? s : 0;
61
+ return;
62
+ }
63
+ if (typeof t == "number" || typeof t == "string") {
64
+ const { value: l, scale: u } = O(t, r, !1);
65
+ this.value = l, this.scale = r ? s : u;
66
+ return;
67
+ }
68
+ throw new TypeError("Big");
63
69
  }
64
70
  /**
65
71
  * Returns the primitive value of the Big instance.
@@ -80,9 +86,10 @@ class w {
80
86
  const r = n.startsWith("-") ? "-" : "";
81
87
  r && (n = n.substring(1));
82
88
  let s = "";
83
- if (this.scale > m) {
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);
89
+ const l = this.scale;
90
+ if (l > 0) {
91
+ const u = n.length > l ? n.length - l : 0;
92
+ s = n.slice(u).padStart(l, "0"), t && (s = _(s)), s.length > 0 && (s = `.${s}`), n = n.slice(0, u);
86
93
  }
87
94
  return `${r}${n || "0"}${s}`;
88
95
  }
@@ -95,113 +102,195 @@ class w {
95
102
  return this.toString();
96
103
  }
97
104
  }
98
- function S(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 : o(e.value * s, t.scale), g = r < 0 ? t : o(t.value * s, e.scale);
106
- return [i, g];
107
- }
108
- function O(e, t, n = !1) {
109
- const [r, s] = S(e, t), i = r.value + s.value;
110
- return n ? (e.value = i, e.scale = r.scale, e) : o(i, r.scale);
111
- }
112
- function N(e, t, n = !1) {
113
- const [r, s] = S(e, t, n), i = r.value - s.value;
114
- return n ? (e.value = i, e) : o(i, r.scale);
115
- }
116
- function E(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) : o(r, s);
119
- }
120
- const d = (e) => e.value === m;
121
- function B(e, t, n = 20, r = "half-up", s = !1) {
122
- if (d(t))
123
- throw new Error("Division by zero is not allowed.");
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), a = u / l, h = u % l;
125
- let c;
105
+ function V(e) {
106
+ return e.value = e.value < h ? -e.value : e.value, e;
107
+ }
108
+ const x = 256, g = [m];
109
+ function v(e) {
110
+ if (e < 0)
111
+ throw new Error("Exp");
112
+ if (e <= x) {
113
+ const t = g[e];
114
+ if (t !== void 0) return t;
115
+ for (let n = g.length; n <= e; n++)
116
+ g[n] = g[n - 1] * I;
117
+ return g[e];
118
+ }
119
+ return I ** BigInt(e);
120
+ }
121
+ function N(e, t) {
122
+ const n = e.scale - t.scale;
123
+ if (n === 0)
124
+ return e.value += t.value, e;
125
+ const r = v(Math.abs(n));
126
+ return n > 0 ? (e.value += t.value * r, e) : (e.value = e.value * r + t.value, e.scale = t.scale, e);
127
+ }
128
+ const M = (e, t) => {
129
+ const n = e.scale - t.scale;
130
+ if (n === 0)
131
+ return e.value > t.value ? 1 : e.value < t.value ? -1 : 0;
132
+ const r = v(Math.abs(n)), s = n > 0 ? e.value : e.value * r, l = n < 0 ? t.value : t.value * r;
133
+ return s > l ? 1 : s < l ? -1 : 0;
134
+ }, S = (e) => e.value === h;
135
+ function E(e, t, n = 20, r = "half-up") {
136
+ if (S(t))
137
+ throw new Error("Div0");
138
+ const s = Math.max(e.scale, t.scale), l = Math.max(0, n), u = e.value * v(s + l - e.scale), i = t.value * v(s - t.scale), c = u / i, o = u % i;
139
+ let a = c;
126
140
  switch (r) {
127
141
  case "down":
128
- c = a;
129
142
  break;
130
143
  case "up":
131
- c = h !== m ? a + y : a + m;
144
+ a = o !== h ? c + m : c;
132
145
  break;
133
- case "half-up":
134
146
  default:
135
- c = h * BigInt(2) >= l ? a + y : a;
147
+ a = o * 2n >= i ? c + m : c;
136
148
  break;
137
149
  }
138
- return s ? (e.value = c, e.scale = g, e) : o(c, g);
150
+ return e.value = a, e.scale = l, e;
139
151
  }
140
- function V(e, t, n = !1) {
141
- if (d(t))
142
- throw new Error("Division by zero is not allowed.");
143
- const r = B(e, t, 0, "down");
144
- if (n)
145
- return E(r, t, !0), N(e, r, !0);
146
- const s = E(r, t);
147
- return N(e, s);
152
+ function F(e, t) {
153
+ return M(e, t) >= 0 ? e : t;
148
154
  }
149
- function $(e, t = 2, n = !1) {
155
+ function G(e, t) {
156
+ return M(e, t) > 0 ? t : e;
157
+ }
158
+ const p = (e) => new B(e.value, e.scale);
159
+ function y(e, t) {
160
+ return e.value = e.value * t.value, e.scale = e.scale + t.scale, e;
161
+ }
162
+ function P(e, t) {
163
+ const n = e.scale - t.scale;
164
+ if (n === 0)
165
+ return e.value -= t.value, e;
166
+ const r = v(Math.abs(n));
167
+ return n > 0 ? (e.value -= t.value * r, e) : (e.value = e.value * r - t.value, e.scale = t.scale, e);
168
+ }
169
+ function C(e, t) {
170
+ if (S(t))
171
+ throw new Error("Div0");
172
+ const n = p(e);
173
+ return E(n, t, 0, "down"), y(n, t), P(e, n);
174
+ }
175
+ function d(e, t = 2) {
150
176
  if (!Number.isInteger(t) || t < 0)
151
- throw new Error("The exponent must be a non-negative integer.");
152
- const r = e.value ** BigInt(t), s = e.scale * t;
153
- return n ? (e.value = r, e.scale = s, e) : o(r, s);
177
+ throw new Error("Exp");
178
+ return e.value = e.value ** BigInt(t), e.scale = e.scale * t, e;
154
179
  }
155
- const T = (e, t) => {
156
- const [n, r] = S(e, t);
157
- return n.value > r.value ? 1 : n.value < r.value ? -1 : 0;
158
- };
159
- function F(e, t = !1) {
160
- const n = e.value < m ? -e.value : e.value;
161
- return t ? (e.value = n, e) : o(n, e.scale);
162
- }
163
- function Z(e, t = 2, n = 20, r = !1) {
164
- if (d(e))
165
- return o(0);
166
- if (e.valueOf() === BigInt(1))
167
- return o(1);
168
- if (e.value < m && t % 2 === 0)
169
- throw new Error("The root must be an odd integer when the number is negative.");
180
+ function R(e, t = 2, n = 20) {
181
+ if (n < 0) throw new Error("Prec");
182
+ const r = Math.max(0, n);
183
+ if (S(e))
184
+ return e.value = h, e.scale = r, e;
185
+ if (e.value === m)
186
+ return e.value = m, e.scale = r, e;
187
+ if (e.value < h && t % 2 === 0)
188
+ throw new Error("Root");
170
189
  if (!Number.isInteger(t) || t < 2)
171
- throw new Error("The root must be an integer and greater than 1");
172
- if (n < 0)
173
- throw new Error("The precision must be a non-negative integer.");
174
- const s = o(n === 0 ? "1" : "0." + "0".repeat(n - 1) + "1"), i = o(t), g = o(t - 1), u = o(2);
175
- let l = B(O(e, B(e, u, n)), u, n), a;
176
- do {
177
- a = l;
178
- const D = O(E(g, l), B(e, $(l, t - 1), n));
179
- l = B(D, i, n);
180
- } while (T(F(N(a, l)), s) === 1);
181
- const h = l.toString();
182
- if (r)
183
- return e.value = l.value, e.scale = n, e;
184
- const c = h.indexOf("."), f = c !== -1 ? h.slice(0, c + n + 1) : h;
185
- return o(f);
186
- }
187
- const v = (e) => new w(e.value, e.scale), k = (e, t, n = !1) => T(e, t) > 0 ? n ? t : v(t) : n ? e : v(e), q = (e, t, n = !1) => T(e, t) >= 0 ? n ? e : v(e) : n ? t : v(t);
190
+ throw new Error("Root");
191
+ if (t === 2)
192
+ return e.value = A(e, r), e.scale = r, e;
193
+ const s = p(e);
194
+ return q(e, s, t, r), e;
195
+ }
196
+ function q(e, t, n, r) {
197
+ const s = f(n), l = f(n - 1), u = f(2), i = p(t);
198
+ E(i, u, r), e.value = t.value, e.scale = t.scale, N(e, i), E(e, u, r);
199
+ const c = p(e), o = p(e), a = p(t);
200
+ for (; ; ) {
201
+ if (c.value = e.value, c.scale = e.scale, y(c, l), o.value = e.value, o.scale = e.scale, d(o, n - 1), a.value = t.value, a.scale = t.scale, E(a, o, r), N(c, a), E(c, s, r), k(e.value - c.value) <= m) {
202
+ e.value = c.value, e.scale = r;
203
+ return;
204
+ }
205
+ e.value = c.value, e.scale = c.scale;
206
+ }
207
+ }
208
+ function A(e, t) {
209
+ const r = 2 * (t + 1) - e.scale, s = e.value < h ? -e.value : e.value, l = r >= 0 ? s * v(r) : s / v(-r), u = $(l), i = u % I;
210
+ let c = u / I;
211
+ return i >= 5n && (c += m), c;
212
+ }
213
+ function j(e) {
214
+ const t = e.toString(16), n = t.charCodeAt(0), r = n < 58 ? n - 48 : n - 87;
215
+ return (t.length - 1) * 4 + (32 - Math.clz32(r));
216
+ }
217
+ function $(e) {
218
+ if (e < h)
219
+ throw new Error("Sqrt");
220
+ if (e < 2n) return e;
221
+ const t = j(e);
222
+ let n = m << BigInt(t + 1 >> 1), r = n + e / n >> m;
223
+ for (; r < n; )
224
+ n = r, r = n + e / n >> m;
225
+ return n;
226
+ }
227
+ function k(e) {
228
+ return e < h ? -e : e;
229
+ }
230
+ class L {
231
+ current;
232
+ constructor(t) {
233
+ this.current = f(t);
234
+ }
235
+ add(t) {
236
+ return N(this.current, f(t)), this;
237
+ }
238
+ sub(t) {
239
+ return P(this.current, f(t)), this;
240
+ }
241
+ mul(t) {
242
+ return y(this.current, f(t)), this;
243
+ }
244
+ div(t, n, r) {
245
+ return E(this.current, f(t), n, r), this;
246
+ }
247
+ mod(t) {
248
+ return C(this.current, f(t)), this;
249
+ }
250
+ pow(t = 2) {
251
+ return d(this.current, t), this;
252
+ }
253
+ sqrt(t = 2, n) {
254
+ return R(this.current, t, n), this;
255
+ }
256
+ value() {
257
+ return this.current;
258
+ }
259
+ toString(t = !0) {
260
+ return this.current.toString(t);
261
+ }
262
+ }
263
+ function Z(e) {
264
+ return new L(e);
265
+ }
266
+ function U(e, t) {
267
+ const n = e.scale - t.scale;
268
+ if (n === 0)
269
+ return [e, t];
270
+ const r = v(Math.abs(n));
271
+ return n > 0 ? (t.value *= r, t.scale = e.scale, [e, t]) : (e.value *= r, e.scale = t.scale, [e, t]);
272
+ }
273
+ const W = (e) => typeof e == "bigint" || typeof e == "number" || typeof e == "string" && !Number.isNaN(Number(e));
188
274
  export {
189
- w as Big,
190
- F as absBig,
191
- O as addBig,
192
- S as alignScale,
193
- v as cloneBig,
194
- T as compareBig,
195
- o as createBig,
196
- B as divBig,
197
- j as fromString,
198
- P as isBigObject,
199
- M as isNumericValue,
200
- q as maxBig,
201
- k as minBig,
202
- V as modBig,
203
- E as mulBig,
204
- $ as powBig,
205
- Z as sqrtBig,
206
- N as subBig
275
+ B as Big,
276
+ L as BigPipe,
277
+ V as absBig,
278
+ N as addBig,
279
+ U as alignScale,
280
+ p as cloneBig,
281
+ M as compareBig,
282
+ f as createBig,
283
+ E as divBig,
284
+ O as fromString,
285
+ D as isBigObject,
286
+ W as isNumericValue,
287
+ F as maxBig,
288
+ G as minBig,
289
+ C as modBig,
290
+ y as mulBig,
291
+ Z as pipeBig,
292
+ v as pow10,
293
+ d as powBig,
294
+ R as sqrtBig,
295
+ P as subBig
207
296
  };
@@ -1 +1 @@
1
- (function(r,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(r=typeof globalThis<"u"?globalThis:r||self,o((r.big=r.big||{},r.big.esm={})))})(this,function(r){"use strict";var Z=Object.defineProperty;var k=(r,o,d)=>o in r?Z(r,o,{enumerable:!0,configurable:!0,writable:!0,value:d}):r[o]=d;var D=(r,o,d)=>(k(r,typeof o!="symbol"?o+"":o,d),d);const o=e=>typeof e=="bigint"||typeof e=="number"||typeof e=="string"&&!isNaN(Number(e)),d=/0+$/,P=e=>e.replace(d,""),v=BigInt(0),M=BigInt(1),T=BigInt(10);function u(e,t){return e instanceof N?e:new N(e,t)}function A(e,t=!1,n=!0){const i=e.toString();if(!Number.isFinite(Number(i)))throw new Error("Invalid number");if(Number(i)===0)return n?u(v):{value:v,scale:0};let[s,a]=i.split("e");const h=s.startsWith("-")?"-":"";s=h?s.slice(1):s;let[f,l=""]=s.split(".");l=t?"":l;const c=Number.parseInt(a);if(c>0){const B=Math.min(c,l.length);f+=l.slice(0,B),l=l.slice(B),B<c&&(f+="0".repeat(c-B))}else if(c<0){const B=Math.min(-c,f.length);l=f.slice(-B)+l,f=f.slice(0,f.length-B),B<-c&&(l="0".repeat(-c-B)+l)}const m=BigInt(h+f+l),g=l.length;return n?u(m,g):{value:m,scale:g}}function _(e){return typeof e!="object"||e===null?!1:"value"in e&&"scale"in e&&typeof e.value=="bigint"&&typeof e.scale=="number"}class N{constructor(t,n){D(this,"value");D(this,"scale");if(_(t))this.value=t.value,this.scale=t.scale;else if(o(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:s,scale:a}=A(t,i,!1);this.value=s,this.scale=i?n:a}}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 s="";if(this.scale>v){const a=n.length>this.scale?n.length-Number(this.scale):0;s=n.slice(a).padStart(Number(this.scale),"0"),t&&(s=P(s)),s.length>0&&(s=`.${s}`),n=n.slice(0,a)}return`${i}${n||"0"}${s}`}toJSON(){return this.toString()}}function S(e,t,n=!1){const i=e.scale-t.scale;if(i===0)return[e,t];const s=T**BigInt(Math.abs(i));if(n)return i>0?(t.value*=s,t.scale=e.scale):(e.value*=s,e.scale=t.scale),[e,t];const a=i>0?e:u(e.value*s,t.scale),h=i<0?t:u(t.value*s,e.scale);return[a,h]}function O(e,t,n=!1){const[i,s]=S(e,t),a=i.value+s.value;return n?(e.value=a,e.scale=i.scale,e):u(a,i.scale)}function E(e,t,n=!1){const[i,s]=S(e,t,n),a=i.value-s.value;return n?(e.value=a,e):u(a,i.scale)}function p(e,t,n=!1){const i=e.value*t.value,s=e.scale+t.scale;return n?(e.value=i,e.scale=s,e):u(i,s)}const j=e=>e.value===v;function I(e,t,n=20,i="half-up",s=!1){if(j(t))throw new Error("Division by zero is not allowed.");const a=Math.max(e.scale,t.scale),h=Math.max(0,n),f=e.value*T**BigInt(a+h-e.scale),l=t.value*T**BigInt(a-t.scale),c=f/l,m=f%l;let g;switch(i){case"down":g=c;break;case"up":g=m!==v?c+M:c+v;break;case"half-up":default:g=m*BigInt(2)>=l?c+M:c;break}return s?(e.value=g,e.scale=h,e):u(g,h)}function V(e,t,n=!1){if(j(t))throw new Error("Division by zero is not allowed.");const i=I(e,t,0,"down");if(n)return p(i,t,!0),E(e,i,!0);const s=p(i,t);return E(e,s)}function b(e,t=2,n=!1){if(!Number.isInteger(t)||t<0)throw new Error("The exponent must be a non-negative integer.");const i=e.value**BigInt(t),s=e.scale*t;return n?(e.value=i,e.scale=s,e):u(i,s)}const y=(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):u(n,e.scale)}function $(e,t=2,n=20,i=!1){if(j(e))return u(0);if(e.valueOf()===BigInt(1))return u(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 s=u(n===0?"1":"0."+"0".repeat(n-1)+"1"),a=u(t),h=u(t-1),f=u(2);let l=I(O(e,I(e,f,n)),f,n),c;do{c=l;const G=O(p(h,l),I(e,b(l,t-1),n));l=I(G,a,n)}while(y(R(E(c,l)),s)===1);const m=l.toString();if(i)return e.value=l.value,e.scale=n,e;const g=m.indexOf("."),B=g!==-1?m.slice(0,g+n+1):m;return u(B)}const w=e=>new N(e.value,e.scale),q=(e,t,n=!1)=>y(e,t)>0?n?t:w(t):n?e:w(e),F=(e,t,n=!1)=>y(e,t)>=0?n?e:w(e):n?t:w(t);r.Big=N,r.absBig=R,r.addBig=O,r.alignScale=S,r.cloneBig=w,r.compareBig=y,r.createBig=u,r.divBig=I,r.fromString=A,r.isBigObject=_,r.isNumericValue=o,r.maxBig=F,r.minBig=q,r.modBig=V,r.mulBig=p,r.powBig=b,r.sqrtBig=$,r.subBig=E,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
1
+ (function(c,I){typeof exports=="object"&&typeof module<"u"?I(exports):typeof define=="function"&&define.amd?define(["exports"],I):(c=typeof globalThis<"u"?globalThis:c||self,I((c.big=c.big||{},c.big.esm={})))})(this,(function(c){"use strict";const f=0n,g=1n,S=10n;function M(e){if(e===null||typeof e!="object")return!1;const t=e;return typeof t.value=="bigint"&&typeof t.scale=="number"}function a(e,t){return e instanceof v?e:M(e)?new v(e.value,e.scale):t===void 0?new v(e):new v(e,t)}function _(e,t=!1,n=!0){const r=typeof e=="number"?String(e):e,i=/^(-)?(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/.exec(r);if(!i)throw new Error("Num");const o=i[1]?"-":"";let u=i[2]||"",l=i[3]||"";if(u.length===0&&l.length===0)throw new Error("Num");t&&(l="");const s=Number.parseInt(i[4]||"0",10);if(!Number.isFinite(s))throw new Error("Exp");if(s>0){const w=Math.min(s,l.length);u+=l.slice(0,w),l=l.slice(w),w<s&&(u+="0".repeat(s-w))}else if(s<0){const w=Math.min(-s,u.length);l=u.slice(-w)+l,u=u.slice(0,u.length-w),w<-s&&(l="0".repeat(-s-w)+l)}const h=BigInt(o+u+l),m=l.length;return h===f?n?a(f):{value:f,scale:0}:n?a(h,m):{value:h,scale:m}}const R=/0+$/,A=e=>e.replace(R,"");class v{value;scale;constructor(t,n){if(t instanceof v){this.value=t.value,this.scale=t.scale;return}if(M(t)){this.value=t.value,this.scale=t.scale;return}if(typeof t=="object"&&t!==null)throw new TypeError("Big");let r=n!==void 0,i=0;if(r&&(i=typeof n=="number"?n:Number(n),r=!Number.isNaN(i),r&&(i=Math.floor(i))),typeof t=="bigint"){this.value=t,this.scale=r?i:0;return}if(typeof t=="number"||typeof t=="string"){const{value:o,scale:u}=_(t,r,!1);this.value=o,this.scale=r?i:u;return}throw new TypeError("Big")}valueOf(){return this.value}toString(t=!0){let n=this.value.toString();const r=n.startsWith("-")?"-":"";r&&(n=n.substring(1));let i="";const o=this.scale;if(o>0){const u=n.length>o?n.length-o:0;i=n.slice(u).padStart(o,"0"),t&&(i=A(i)),i.length>0&&(i=`.${i}`),n=n.slice(0,u)}return`${r}${n||"0"}${i}`}toJSON(){return this.toString()}}function V(e){return e.value=e.value<f?-e.value:e.value,e}const $=256,N=[g];function B(e){if(e<0)throw new Error("Exp");if(e<=$){const t=N[e];if(t!==void 0)return t;for(let n=N.length;n<=e;n++)N[n]=N[n-1]*S;return N[e]}return S**BigInt(e)}function p(e,t){const n=e.scale-t.scale;if(n===0)return e.value+=t.value,e;const r=B(Math.abs(n));return n>0?(e.value+=t.value*r,e):(e.value=e.value*r+t.value,e.scale=t.scale,e)}const P=(e,t)=>{const n=e.scale-t.scale;if(n===0)return e.value>t.value?1:e.value<t.value?-1:0;const r=B(Math.abs(n)),i=n>0?e.value:e.value*r,o=n<0?t.value:t.value*r;return i>o?1:i<o?-1:0},D=e=>e.value===f;function E(e,t,n=20,r="half-up"){if(D(t))throw new Error("Div0");const i=Math.max(e.scale,t.scale),o=Math.max(0,n),u=e.value*B(i+o-e.scale),l=t.value*B(i-t.scale),s=u/l,h=u%l;let m=s;switch(r){case"down":break;case"up":m=h!==f?s+g:s;break;default:m=h*2n>=l?s+g:s;break}return e.value=m,e.scale=o,e}function k(e,t){return P(e,t)>=0?e:t}function L(e,t){return P(e,t)>0?t:e}const d=e=>new v(e.value,e.scale);function y(e,t){return e.value=e.value*t.value,e.scale=e.scale+t.scale,e}function O(e,t){const n=e.scale-t.scale;if(n===0)return e.value-=t.value,e;const r=B(Math.abs(n));return n>0?(e.value-=t.value*r,e):(e.value=e.value*r-t.value,e.scale=t.scale,e)}function j(e,t){if(D(t))throw new Error("Div0");const n=d(e);return E(n,t,0,"down"),y(n,t),O(e,n)}function T(e,t=2){if(!Number.isInteger(t)||t<0)throw new Error("Exp");return e.value=e.value**BigInt(t),e.scale=e.scale*t,e}function q(e,t=2,n=20){if(n<0)throw new Error("Prec");const r=Math.max(0,n);if(D(e))return e.value=f,e.scale=r,e;if(e.value===g)return e.value=g,e.scale=r,e;if(e.value<f&&t%2===0)throw new Error("Root");if(!Number.isInteger(t)||t<2)throw new Error("Root");if(t===2)return e.value=G(e,r),e.scale=r,e;const i=d(e);return F(e,i,t,r),e}function F(e,t,n,r){const i=a(n),o=a(n-1),u=a(2),l=d(t);E(l,u,r),e.value=t.value,e.scale=t.scale,p(e,l),E(e,u,r);const s=d(e),h=d(e),m=d(t);for(;;){if(s.value=e.value,s.scale=e.scale,y(s,o),h.value=e.value,h.scale=e.scale,T(h,n-1),m.value=t.value,m.scale=t.scale,E(m,h,r),p(s,m),E(s,i,r),W(e.value-s.value)<=g){e.value=s.value,e.scale=r;return}e.value=s.value,e.scale=s.scale}}function G(e,t){const r=2*(t+1)-e.scale,i=e.value<f?-e.value:e.value,o=r>=0?i*B(r):i/B(-r),u=U(o),l=u%S;let s=u/S;return l>=5n&&(s+=g),s}function Z(e){const t=e.toString(16),n=t.charCodeAt(0),r=n<58?n-48:n-87;return(t.length-1)*4+(32-Math.clz32(r))}function U(e){if(e<f)throw new Error("Sqrt");if(e<2n)return e;const t=Z(e);let n=g<<BigInt(t+1>>1),r=n+e/n>>g;for(;r<n;)n=r,r=n+e/n>>g;return n}function W(e){return e<f?-e:e}class C{current;constructor(t){this.current=a(t)}add(t){return p(this.current,a(t)),this}sub(t){return O(this.current,a(t)),this}mul(t){return y(this.current,a(t)),this}div(t,n,r){return E(this.current,a(t),n,r),this}mod(t){return j(this.current,a(t)),this}pow(t=2){return T(this.current,t),this}sqrt(t=2,n){return q(this.current,t,n),this}value(){return this.current}toString(t=!0){return this.current.toString(t)}}function H(e){return new C(e)}function z(e,t){const n=e.scale-t.scale;if(n===0)return[e,t];const r=B(Math.abs(n));return n>0?(t.value*=r,t.scale=e.scale,[e,t]):(e.value*=r,e.scale=t.scale,[e,t])}const J=e=>typeof e=="bigint"||typeof e=="number"||typeof e=="string"&&!Number.isNaN(Number(e));c.Big=v,c.BigPipe=C,c.absBig=V,c.addBig=p,c.alignScale=z,c.cloneBig=d,c.compareBig=P,c.createBig=a,c.divBig=E,c.fromString=_,c.isBigObject=M,c.isNumericValue=J,c.maxBig=k,c.minBig=L,c.modBig=j,c.mulBig=y,c.pipeBig=H,c.pow10=B,c.powBig=T,c.sqrtBig=q,c.subBig=O,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})}));
package/dist/index.d.ts CHANGED
@@ -1,19 +1,22 @@
1
- export { Big } from "./big";
2
- export type { BigValue, PossibleNumber, RoundingMode, CompareResult } from "./types";
3
- export { addBig } from "./operations/add";
4
- export { subBig } from "./operations/sub";
5
- export { mulBig } from "./operations/mul";
6
- export { divBig } from "./operations/div";
7
- export { modBig } from "./operations/mod";
8
- export { powBig } from "./operations/pow";
9
- export { sqrtBig } from "./operations/sqrt";
10
- export { absBig } from "./operations/abs";
11
- export { compareBig } from "./operations/compare";
12
- export { minBig } from "./operations/min";
13
- export { maxBig } from "./operations/max";
14
- export { isBigObject } from "./utils/is-big-object";
15
- export { isNumericValue } from "./utils/numeric";
16
- export { alignScale } from "./utils/align-scale";
17
- export { createBig } from "./utils/create";
18
- export { cloneBig } from "./utils/clone";
19
- export { fromString } from "./utils/from-string";
1
+ export { Big } from './big';
2
+ export { absBig } from './operations/abs';
3
+ export { addBig } from './operations/add';
4
+ export { compareBig } from './operations/compare';
5
+ export { divBig } from './operations/div';
6
+ export { maxBig } from './operations/max';
7
+ export { minBig } from './operations/min';
8
+ export { modBig } from './operations/mod';
9
+ export { mulBig } from './operations/mul';
10
+ export { powBig } from './operations/pow';
11
+ export { sqrtBig } from './operations/sqrt';
12
+ export { subBig } from './operations/sub';
13
+ export type { BigPipeline } from './pipeline';
14
+ export { BigPipe, pipeBig } from './pipeline';
15
+ export type { BigValue, CompareResult, PossibleNumber, RoundingMode } from './types';
16
+ export { alignScale } from './utils/align-scale';
17
+ export { cloneBig } from './utils/clone';
18
+ export { createBig } from './utils/create';
19
+ export { fromString } from './utils/from-string';
20
+ export { isBigObject } from './utils/is-big-object';
21
+ export { isNumericValue } from './utils/numeric';
22
+ export { pow10 } from './utils/pow10';
@@ -1,16 +1,5 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
- * Returns the absolute value of a Big instance.
4
- *
5
- * @param {Big} big - The Big instance.
6
- * @param {boolean} [mutable=false] - Whether to mutate the Big instance. Defaults to false.
7
- * @returns {Big} The absolute value of the Big instance.
8
- *
9
- * @category Operations
10
- *
11
- * @example
12
- * absBig(new Big(-1)); // new Big instance with value 1
13
- * absBig(new Big(-1.1)); // new Big instance with value 1.1
14
- * absBig(new Big(1)); // new Big instance with value 1
3
+ * Returns the absolute value and mutates the given Big instance.
15
4
  */
16
- export declare function absBig(big: Big, mutable?: boolean): Big;
5
+ export declare function absBig(big: Big): Big;
@@ -1,17 +1,5 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
- * Adds two big values.
4
- *
5
- * @param {Big} a - The first value to add.
6
- * @param {Big} b - The second value to add.
7
- * @param {boolean} [mutable=false] - Whether to mutate the first value. Defaults to false.
8
- * @returns {Big} The sum of the two values.
9
- *
10
- * @category Operations
11
- *
12
- * @example
13
- * addBig(new Big(1), new Big(2)); // new Big instance with value 3
14
- * addBig(new Big(1.1), new Big(2.2)); // new Big instance with value 3.3
15
- * addBig(new Big(1.1), new Big(2)); // new Big instance with value 3.1
3
+ * Adds two Big instances and mutates the first one.
16
4
  */
17
- export declare function addBig(a: Big, b: Big, mutable?: boolean): Big;
5
+ export declare function addBig(a: Big, b: Big): Big;
@@ -1,5 +1,5 @@
1
- import type { Big } from "../big";
2
- import type { CompareResult } from "../types";
1
+ import { Big } from '../big';
2
+ import { CompareResult } from '../types';
3
3
  /**
4
4
  * Compares two Big numbers and returns a CompareResult indicating the relation between them.
5
5
  *
@@ -1,14 +1,13 @@
1
- import type { RoundingMode } from "../types";
2
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
+ import { RoundingMode } from '../types';
3
3
  /**
4
- * Divides two Big instances and returns the result as a new Big instance.
4
+ * Divides two Big instances and mutates the dividend with the result.
5
5
  *
6
6
  * @param {Big} dividend - The dividend Big instance.
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.
11
- * @returns {Big} - A new Big instance representing the result of the division.
10
+ * @returns {Big} - The mutated dividend instance.
12
11
  * @throws {Error} Division by zero is not allowed.
13
12
  *
14
13
  * @category Operations
@@ -22,4 +21,4 @@ import type { Big } from "../big";
22
21
  * console.log(divBig(big1, big2, 2, "down").toString()); // "3.33"
23
22
  * console.log(divBig(big1, big2, 2, "up").toString()); // "3.34"
24
23
  */
25
- export declare function divBig(dividend: Big, divisor: Big, precision?: number, roundingMode?: RoundingMode, mutable?: boolean): Big;
24
+ export declare function divBig(dividend: Big, divisor: Big, precision?: number, roundingMode?: RoundingMode): Big;
@@ -1,19 +1,17 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Returns the maximum of two Big numbers.
4
4
  *
5
5
  * @param {Big} a - The first Big number.
6
6
  * @param {Big} b - The second Big number.
7
- * @param {boolean} [mutable=false] - Whether to return same Big instance. Defaults to false.
8
7
  *
9
- * @returns {Big} - The maximum of the two Big numbers. If the two Big numbers are equal, the first Big number is returned.
8
+ * @returns {Big} - Returns either `a` or `b`. If the two numbers are equal, `a` is returned.
10
9
  *
11
10
  * @category Operations
12
11
  *
13
12
  * @example
14
- * maxBig(new Big(1), new Big(2)); // new Big instance with value 2 (second Big number)
15
- * maxBig(new Big(2), new Big(1)); // new Big instance with value 2 (first Big number)
16
- * maxBig(new Big(1), new Big(1)); // new Big instance with value 1 (first Big number)
17
- * maxBig(new Big(1), new Big(1), true); // first Big number with value 1 and same instance
13
+ * maxBig(new Big(1), new Big(2)); // returns the second Big number
14
+ * maxBig(new Big(2), new Big(1)); // returns the first Big number
15
+ * maxBig(new Big(1), new Big(1)); // returns the first Big number
18
16
  */
19
- export declare const maxBig: (a: Big, b: Big, mutable?: boolean) => Big;
17
+ export declare function maxBig(a: Big, b: Big): Big;