big.esm 1.3.2 → 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/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ # [2.0.0](https://github.com/dschewchenko/big.esm/compare/v1.3.2...v2.0.0) (2025-12-18)
2
+
3
+
4
+ * feat!: big.esm v2 rewrite core ([#31](https://github.com/dschewchenko/big.esm/issues/31)) ([5d05e63](https://github.com/dschewchenko/big.esm/commit/5d05e63b425eaf07722859bd1ff354ec97aaad13))
5
+
6
+
7
+ ### BREAKING CHANGES
8
+
9
+ * operations + alignScale now mutate inputs. Сlone with cloneBig for immutability.
10
+
1
11
  ## [1.3.2](https://github.com/dschewchenko/big.esm/compare/v1.3.1...v1.3.2) (2025-10-26)
2
12
 
3
13
 
package/README.md CHANGED
@@ -13,9 +13,6 @@ It provides advanced functionality for performing arithmetic operations with pre
13
13
  - Fully written in TypeScript, so it provides type definitions out of the box.
14
14
  - Tree-shaking is supported.
15
15
 
16
- Full bundle size (ESM) — 4.2 kB minified and 1.8 kB gzipped.
17
- `sqrtBig` operation is heavy (more than half of bundle), because it reuses most of the operations from `big.esm`.
18
-
19
16
  ## Installation
20
17
 
21
18
  Install `big.esm` using Bun:
@@ -39,22 +36,57 @@ yarn add big.esm
39
36
  ## Usage
40
37
 
41
38
  ```js
42
- import { createBig, addBig } from 'big.esm';
39
+ import { addBig, cloneBig, createBig } from "big.esm";
43
40
 
44
- const a = createBig("12345678910.12345678910"); // or new Big("12345678910.12345678910")
41
+ const a = createBig("12345678910.12345678910");
45
42
  const b = createBig("9876543210.9876543210");
46
- const result = addBig(a, b);
47
- console.log(result.toString()); // 22222222121.1111111101
43
+
44
+ addBig(a, b);
45
+ console.log(a.toString()); // 22222222121.1111111101
46
+
47
+ // If you need immutability, clone first:
48
+ const c = cloneBig(a);
49
+ addBig(c, b);
50
+ ```
51
+
52
+ Pipeline (chainable, mutates its internal `Big`):
53
+
54
+ ```js
55
+ import { pipeBig } from "big.esm";
56
+
57
+ console.log(
58
+ pipeBig("1.5e3").add(500).div(2, 0, "down").toString()
59
+ ); // 1000
48
60
  ```
49
61
 
62
+ ## Benchmarks
63
+
64
+ More info - [BENCHMARK.md](https://github.com/dschewchenko/big.esm/blob/main/BENCHMARK.md)
65
+
66
+ - **Speed (ops)**: `big.esm v2` vs `big.js` — `26.26x faster` (median; min `2.17x`, max `154.43x`) → `2.26x faster` than `big.esm v1` (mutable mode; median `11.63x` vs `big.js`).
67
+ - **Speed (pipeBig)**: `pipeBig` vs `big.js` — `29.8x faster` (median; min `2.84x`, max `148.27x`).
68
+ - **Size (full)**: `big.esm v2` — `5.25 KB` minified / `1.87 KB` brotli (≈`1.30x` / `1.46x` smaller than `big.js`).
69
+ - **Size (pipe-only)**: `bigPipe (v2)` — `4.40 KB` minified / `1.62 KB` brotli (≈`1.55x` / `1.68x` smaller than `big.js`).
70
+
50
71
  ## Compatibility
51
72
 
52
- `big.esm` is compatible with all modern browsers, Bun 1.0+, and Node.js 16+. It uses `BigInt` internally, so it is not compatible with older runtimes. On info from [caniuse.com](https://caniuse.com/#feat=bigint) `BigInt` is supported by 96.47% of all browsers(as of 2023-06-25).
73
+ `big.esm` is compatible with all modern browsers, Bun 1.0+, and Node.js 16+. It uses `BigInt` internally, so it is not compatible with older runtimes. On info from [caniuse.com](https://caniuse.com/#feat=bigint) `BigInt` is supported by 98.84% of all browsers(as of 2025-12-18).
53
74
 
54
75
  ## Notes
55
76
 
56
77
  - `big.esm` is not a drop-in replacement for `big.js`. It does not support the same API and does not have the same functionality. It is a completely different library.
57
- - `sqrtBig()` is heavy and slow operation. It's implemented using the Newton's method with nth root calculation. In most cases, there will be no problems.
78
+
79
+
80
+ ## Breaking changes (v2)
81
+
82
+ Starting from v2, the library is **mutable-first** to stay **small and fast** (fewer allocations, less GC, no runtime "clone vs mutate" checks in hot paths):
83
+
84
+ - Arithmetic helpers (`addBig`, `subBig`, `mulBig`, `divBig`, `modBig`, `powBig`, `sqrtBig`, `absBig`) **mutate the first argument** and return it.
85
+ - `alignScale(a, b)` **mutates** the operand with the smaller scale.
86
+ - `minBig`/`maxBig` return **one of the inputs** (no cloning).
87
+ - `pipeBig(big)` / `new BigPipe(big)` **new** api, **mutates** the passed `Big`.
88
+
89
+ If these changes are breaking for your codebase, clone before calling operations (`cloneBig(a)`) or adjust your code to work with mutation.
58
90
 
59
91
  ## API
60
92
 
@@ -62,15 +94,13 @@ More information in the [documentation](https://dschewchenko.github.io/big.esm/)
62
94
 
63
95
  ### Core
64
96
 
65
- #### `new Big(value: BigValue, scale?: number | string): Big`
66
-
67
- Creates a new `Big` instance from a string, number or `BigInt`. Optionally, you can specify the scale of the number. The scale is the number of digits to the right of the decimal point. If the scale is not specified, it will be calculated automatically, otherwise the number will be rounded to integer.
97
+ #### `createBig(value: BigValue, scale?: number | string): Big`
68
98
 
69
- ### Utilities
99
+ Creates a `Big` instance from a `string`, `number`, `bigint`, `Big`, or `BigObject`. Preferred constructor helper.
70
100
 
71
- #### `createBig(value: string | number | bigint, scale?: number | string): Big`
101
+ The scale is the number of digits to the right of the decimal point. If the scale is not specified, it will be calculated automatically, otherwise the number will be rounded to integer.
72
102
 
73
- Alias for `new Big(value, scale)`.
103
+ ### Utilities
74
104
 
75
105
  #### `cloneBig(a: Big): Big`
76
106
 
@@ -78,7 +108,7 @@ Creates a new `Big` instance from another `Big` instance.
78
108
 
79
109
  #### `alignScale(a: Big, b: Big): [Big, Big]`
80
110
 
81
- Aligns the scale of two `Big` instances. The scale of the result will be equal to the maximum scale of the two numbers.
111
+ Aligns the scale of two `Big` instances by mutating the one with the smaller scale. The scale of the result will be equal to the maximum scale of the two numbers
82
112
 
83
113
  #### `isNumericValue(value: any): boolean`
84
114
 
@@ -86,57 +116,61 @@ Checks if the value is a valid numeric value for creating a `Big` instance.
86
116
 
87
117
  ### Mathematical operations
88
118
 
89
- In math operations `mutable` option is used to specify whether to mutate the first argument or create a new instance. By default, a new instance is created.
90
-
91
- **Note:** In high-load applications, it is recommended to use the `mutable` option to reduce memory usage and improve performance.
119
+ Math operations mutate the first argument and return it.
92
120
 
93
- #### `addBig(a: Big, b: Big, mutable = false): Big`
121
+ #### `addBig(a: Big, b: Big): Big`
94
122
 
95
- Adds two `Big` instances.
123
+ Adds `b` to `a` (mutates `a`).
96
124
 
97
- #### `subBig(a: Big, b: Big, mutable = false): Big`
125
+ #### `subBig(a: Big, b: Big): Big`
98
126
 
99
- Subtracts two `Big` instances.
127
+ Subtracts `b` from `a` (mutates `a`).
100
128
 
101
- #### `mulBig(a: Big, b: Big, mutable = false): Big`
129
+ #### `mulBig(a: Big, b: Big): Big`
102
130
 
103
- Multiplies two `Big` instances.
131
+ Multiplies `a` by `b` (mutates `a`).
104
132
 
105
- #### `divBig(a: Big, b: Big, precision = 20, roundingMode = "half-up", mutable = false): Big`
133
+ #### `divBig(a: Big, b: Big, precision = 20, roundingMode = "half-up"): Big`
106
134
 
107
- Divides two `Big` instances. The default precision is 20. The default rounding mode is "half-up".
135
+ Divides `a` by `b` (mutates `a`).
108
136
 
109
- #### `modBig(a: Big, b: Big, mutable = false): Big`
137
+ #### `modBig(a: Big, b: Big): Big`
110
138
 
111
- Calculates the remainder of dividing two `Big` instances.
139
+ Calculates `a % b` (mutates `a`).
112
140
 
113
- #### `powBig(a: Big, exp: number, mutable = false): Big`
141
+ #### `powBig(a: Big, exp: number): Big`
114
142
 
115
- Raises `a` to the power of `b`.
143
+ Raises `a` to `exp` (mutates `a`).
116
144
 
117
- #### `sqrtBig(a: Big, root = 2, precision = 20, mutable = false): Big`
145
+ #### `sqrtBig(a: Big, root = 2, precision = 20): Big`
118
146
 
119
- Calculates the root of `a`. The default root is 2, which means the square root. The default precision is 20.
147
+ Calculates the root of `a` (mutates `a`).
120
148
 
121
- #### `absBig(a: Big, mutable = false): Big`
149
+ #### `absBig(a: Big): Big`
122
150
 
123
- Returns the absolute value of `a`.
151
+ Replaces `a` with `|a|` (mutates `a`).
124
152
 
125
153
  #### `compareBig(a: Big, b: Big): -1 | 0 | 1`
126
154
 
127
155
  Compares two `Big` instances. Returns `-1` if `a < b`, `0` if `a == b` and `1` if `a > b`.
128
156
 
129
- #### `minBig(a: Big, b: Big, mutable = false): Big`
157
+ #### `minBig(a: Big, b: Big): Big`
158
+
159
+ Returns the minimum of `a` and `b` by reference (no cloning).
160
+
161
+ #### `maxBig(a: Big, b: Big): Big`
162
+
163
+ Returns the maximum of `a` and `b` by reference (no cloning).
130
164
 
131
- Returns the minimum of two `Big` instances. If the numbers are equal, returns the first number.
165
+ ### Pipeline
132
166
 
133
- #### `maxBig(a: Big, b: Big, mutable = false): Big`
167
+ #### `pipeBig(initial: BigValue): BigPipeline`
134
168
 
135
- Returns the maximum of two `Big` instances. If the numbers are equal, returns the first number.
169
+ Creates a chainable pipeline that mutates its internal `Big` in-place.
136
170
 
137
- ## Benchmark
171
+ #### `new BigPipe(initial: BigValue): BigPipe`
138
172
 
139
- Benchmark results are available [here](https://github.com/dschewchenko/big.esm/blob/master/BENCHMARK.md).
173
+ Class form of the pipeline.
140
174
 
141
175
  ## Roadmap
142
176
 
package/dist/big.esm.js CHANGED
@@ -1,32 +1,34 @@
1
- const m = BigInt(0), y = BigInt(1), p = BigInt(10);
2
- function o(e, t) {
3
- return e instanceof w ? e : new w(e, t);
4
- }
5
- function x(e, t = !1, n = !0) {
6
- const r = e.toString();
7
- if (!Number.isFinite(Number(r))) throw new Error("Invalid number");
8
- if (Number(r) === 0) return n ? o(m) : { value: m, scale: 0 };
9
- let [s, a] = r.split("e");
10
- const g = a ?? "0", h = s.startsWith("-") ? "-" : "";
11
- s = h ? s.slice(1) : s;
12
- let [i, l = ""] = s.split(".");
13
- l = t ? "" : l;
14
- const u = Number.parseInt(g, 10);
15
- if (u > 0) {
16
- const c = Math.min(u, l.length);
17
- i += l.slice(0, c), l = l.slice(c), c < u && (i += "0".repeat(u - c));
18
- } else if (u < 0) {
19
- const c = Math.min(-u, i.length);
20
- l = i.slice(-c) + l, i = i.slice(0, i.length - c), c < -u && (l = "0".repeat(-u - c) + l);
21
- }
22
- const f = BigInt(h + i + l), v = l.length;
23
- return n ? o(f, v) : { value: f, scale: v };
24
- }
1
+ const h = 0n, m = 1n, I = 10n;
25
2
  function D(e) {
26
- return typeof e != "object" || e === null ? !1 : "value" in e && "scale" in e && typeof e.value == "bigint" && typeof e.scale == "number";
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);
27
9
  }
28
- const A = (e) => typeof e == "bigint" || typeof e == "number" || typeof e == "string" && !Number.isNaN(Number(e)), M = /0+$/, _ = (e) => e.replace(M, "");
29
- class w {
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 {
30
32
  /**
31
33
  * The value of the Big instance.
32
34
  */
@@ -43,18 +45,27 @@ class w {
43
45
  * @return {Big} - A new Big instance.
44
46
  */
45
47
  constructor(t, n) {
46
- if (D(t))
48
+ if (t instanceof B) {
49
+ this.value = t.value, this.scale = t.scale;
50
+ return;
51
+ }
52
+ if (D(t)) {
47
53
  this.value = t.value, this.scale = t.scale;
48
- else if (A(t)) {
49
- let r = n !== void 0;
50
- if (r && (typeof n != "number" && (n = Number(n)), r = !Number.isNaN(n), n = r ? Math.floor(n) : 0), typeof t == "bigint")
51
- this.value = t, this.scale = r ? n : 0;
52
- else {
53
- const { value: s, scale: a } = x(t, r, !1);
54
- this.value = s, this.scale = r ? n : a;
55
- }
56
- } else
57
- 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");
58
69
  }
59
70
  /**
60
71
  * Returns the primitive value of the Big instance.
@@ -75,9 +86,10 @@ class w {
75
86
  const r = n.startsWith("-") ? "-" : "";
76
87
  r && (n = n.substring(1));
77
88
  let s = "";
78
- if (this.scale > m) {
79
- const a = n.length > this.scale ? n.length - Number(this.scale) : 0;
80
- s = n.slice(a).padStart(Number(this.scale), "0"), t && (s = _(s)), s.length > 0 && (s = `.${s}`), n = n.slice(0, a);
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);
81
93
  }
82
94
  return `${r}${n || "0"}${s}`;
83
95
  }
@@ -90,109 +102,195 @@ class w {
90
102
  return this.toString();
91
103
  }
92
104
  }
93
- function R(e, t = !1) {
94
- const n = e.value < m ? -e.value : e.value;
95
- return t ? (e.value = n, e) : o(n, e.scale);
105
+ function V(e) {
106
+ return e.value = e.value < h ? -e.value : e.value, e;
96
107
  }
97
- function S(e, t, n = !1) {
98
- const r = e.scale - t.scale;
99
- if (r === 0)
100
- return [e, t];
101
- const s = p ** BigInt(Math.abs(r));
102
- if (n)
103
- return r > 0 ? (t.value *= s, t.scale = e.scale) : (e.value *= s, e.scale = t.scale), [e, t];
104
- const a = r > 0 ? e : o(e.value * s, t.scale), g = r < 0 ? t : o(t.value * s, e.scale);
105
- return [a, g];
106
- }
107
- function O(e, t, n = !1) {
108
- const [r, s] = S(e, t), a = r.value + s.value;
109
- return n ? (e.value = a, e.scale = r.scale, e) : o(a, r.scale);
110
- }
111
- const d = (e, t) => {
112
- const [n, r] = S(e, t);
113
- return n.value > r.value ? 1 : n.value < r.value ? -1 : 0;
114
- }, T = (e) => e.value === m;
115
- function B(e, t, n = 20, r = "half-up", s = !1) {
116
- if (T(t))
117
- throw new Error("Division by zero is not allowed.");
118
- const a = Math.max(e.scale, t.scale), g = Math.max(0, n), h = e.value * p ** BigInt(a + g - e.scale), i = t.value * p ** BigInt(a - t.scale), l = h / i, u = h % i;
119
- let f = l;
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;
120
140
  switch (r) {
121
141
  case "down":
122
142
  break;
123
143
  case "up":
124
- f = u !== m ? l + y : l + m;
144
+ a = o !== h ? c + m : c;
125
145
  break;
126
146
  default:
127
- f = u * BigInt(2) >= i ? l + y : l;
147
+ a = o * 2n >= i ? c + m : c;
128
148
  break;
129
149
  }
130
- return s ? (e.value = f, e.scale = g, e) : o(f, g);
150
+ return e.value = a, e.scale = l, e;
151
+ }
152
+ function F(e, t) {
153
+ return M(e, t) >= 0 ? e : t;
154
+ }
155
+ function G(e, t) {
156
+ return M(e, t) > 0 ? t : e;
131
157
  }
132
- const I = (e) => new w(e.value, e.scale), P = (e, t, n = !1) => d(e, t) >= 0 ? n ? e : I(e) : n ? t : I(t), $ = (e, t, n = !1) => d(e, t) > 0 ? n ? t : I(t) : n ? e : I(e);
133
- function N(e, t, n = !1) {
134
- const r = e.value * t.value, s = e.scale + t.scale;
135
- return n ? (e.value = r, e.scale = s, e) : o(r, s);
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;
136
161
  }
137
- function E(e, t, n = !1) {
138
- const [r, s] = S(e, t, n), a = r.value - s.value;
139
- return n ? (e.value = a, e) : o(a, r.scale);
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);
140
168
  }
141
- function F(e, t, n = !1) {
142
- if (T(t))
143
- throw new Error("Division by zero is not allowed.");
144
- const r = B(e, t, 0, "down");
145
- if (n)
146
- return N(r, t, !0), E(e, r, !0);
147
- const s = N(r, t);
148
- return E(e, s);
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);
149
174
  }
150
- function j(e, t = 2, n = !1) {
175
+ function d(e, t = 2) {
151
176
  if (!Number.isInteger(t) || t < 0)
152
- throw new Error("The exponent must be a non-negative integer.");
153
- const r = e.value ** BigInt(t), s = e.scale * t;
154
- return n ? (e.value = r, e.scale = s, e) : o(r, s);
155
- }
156
- function G(e, t = 2, n = 20, r = !1) {
157
- if (T(e)) return o(0);
158
- if (e.valueOf() === BigInt(1)) return o(1);
159
- if (e.value < m && t % 2 === 0)
160
- throw new Error("The root must be an odd integer when the number is negative.");
161
- if (!Number.isInteger(t) || t < 2) throw new Error("The root must be an integer and greater than 1");
162
- if (n < 0) throw new Error("The precision must be a non-negative integer.");
163
- const s = o(n === 0 ? "1" : `0.${"0".repeat(n - 1)}1`), a = o(t), g = o(t - 1), h = o(2);
164
- let i = B(O(e, B(e, h, n)), h, n), l;
165
- do {
166
- l = i;
167
- const c = O(
168
- N(g, i),
169
- B(e, j(i, t - 1), n)
170
- );
171
- i = B(c, a, n);
172
- } while (d(R(E(l, i)), s) === 1);
173
- const u = i.toString();
174
- if (r)
175
- return e.value = i.value, e.scale = n, e;
176
- const f = u.indexOf("."), v = f !== -1 ? u.slice(0, f + n + 1) : u;
177
- return o(v);
177
+ throw new Error("Exp");
178
+ return e.value = e.value ** BigInt(t), e.scale = e.scale * t, e;
179
+ }
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");
189
+ if (!Number.isInteger(t) || t < 2)
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]);
178
272
  }
273
+ const W = (e) => typeof e == "bigint" || typeof e == "number" || typeof e == "string" && !Number.isNaN(Number(e));
179
274
  export {
180
- w as Big,
181
- R as absBig,
182
- O as addBig,
183
- S as alignScale,
184
- I as cloneBig,
185
- d as compareBig,
186
- o as createBig,
187
- B as divBig,
188
- x as fromString,
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,
189
285
  D as isBigObject,
190
- A as isNumericValue,
191
- P as maxBig,
192
- $ as minBig,
193
- F as modBig,
194
- N as mulBig,
195
- j as powBig,
196
- G as sqrtBig,
197
- E as subBig
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
198
296
  };
@@ -1 +1 @@
1
- (function(s,I){typeof exports=="object"&&typeof module<"u"?I(exports):typeof define=="function"&&define.amd?define(["exports"],I):(s=typeof globalThis<"u"?globalThis:s||self,I((s.big=s.big||{},s.big.esm={})))})(this,(function(s){"use strict";const m=BigInt(0),j=BigInt(1),T=BigInt(10);function c(e,t){return e instanceof w?e:new w(e,t)}function D(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?c(m):{value:m,scale:0};let[r,a]=i.split("e");const B=a??"0",h=r.startsWith("-")?"-":"";r=h?r.slice(1):r;let[u,l=""]=r.split(".");l=t?"":l;const o=Number.parseInt(B,10);if(o>0){const f=Math.min(o,l.length);u+=l.slice(0,f),l=l.slice(f),f<o&&(u+="0".repeat(o-f))}else if(o<0){const f=Math.min(-o,u.length);l=u.slice(-f)+l,u=u.slice(0,u.length-f),f<-o&&(l="0".repeat(-o-f)+l)}const g=BigInt(h+u+l),y=l.length;return n?c(g,y):{value:g,scale:y}}function M(e){return typeof e!="object"||e===null?!1:"value"in e&&"scale"in e&&typeof e.value=="bigint"&&typeof e.scale=="number"}const A=e=>typeof e=="bigint"||typeof e=="number"||typeof e=="string"&&!Number.isNaN(Number(e)),R=/0+$/,$=e=>e.replace(R,"");class w{value;scale;constructor(t,n){if(M(t))this.value=t.value,this.scale=t.scale;else if(A(t)){let i=n!==void 0;if(i&&(typeof n!="number"&&(n=Number(n)),i=!Number.isNaN(n),n=i?Math.floor(n):0),typeof t=="bigint")this.value=t,this.scale=i?n:0;else{const{value:r,scale:a}=D(t,i,!1);this.value=r,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 r="";if(this.scale>m){const a=n.length>this.scale?n.length-Number(this.scale):0;r=n.slice(a).padStart(Number(this.scale),"0"),t&&(r=$(r)),r.length>0&&(r=`.${r}`),n=n.slice(0,a)}return`${i}${n||"0"}${r}`}toJSON(){return this.toString()}}function _(e,t=!1){const n=e.value<m?-e.value:e.value;return t?(e.value=n,e):c(n,e.scale)}function N(e,t,n=!1){const i=e.scale-t.scale;if(i===0)return[e,t];const r=T**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 a=i>0?e:c(e.value*r,t.scale),B=i<0?t:c(t.value*r,e.scale);return[a,B]}function O(e,t,n=!1){const[i,r]=N(e,t),a=i.value+r.value;return n?(e.value=a,e.scale=i.scale,e):c(a,i.scale)}const S=(e,t)=>{const[n,i]=N(e,t);return n.value>i.value?1:n.value<i.value?-1:0},b=e=>e.value===m;function v(e,t,n=20,i="half-up",r=!1){if(b(t))throw new Error("Division by zero is not allowed.");const a=Math.max(e.scale,t.scale),B=Math.max(0,n),h=e.value*T**BigInt(a+B-e.scale),u=t.value*T**BigInt(a-t.scale),l=h/u,o=h%u;let g=l;switch(i){case"down":break;case"up":g=o!==m?l+j:l+m;break;default:g=o*BigInt(2)>=u?l+j:l;break}return r?(e.value=g,e.scale=B,e):c(g,B)}const d=e=>new w(e.value,e.scale),V=(e,t,n=!1)=>S(e,t)>=0?n?e:d(e):n?t:d(t),q=(e,t,n=!1)=>S(e,t)>0?n?t:d(t):n?e:d(e);function E(e,t,n=!1){const i=e.value*t.value,r=e.scale+t.scale;return n?(e.value=i,e.scale=r,e):c(i,r)}function p(e,t,n=!1){const[i,r]=N(e,t,n),a=i.value-r.value;return n?(e.value=a,e):c(a,i.scale)}function F(e,t,n=!1){if(b(t))throw new Error("Division by zero is not allowed.");const i=v(e,t,0,"down");if(n)return E(i,t,!0),p(e,i,!0);const r=E(i,t);return p(e,r)}function P(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),r=e.scale*t;return n?(e.value=i,e.scale=r,e):c(i,r)}function G(e,t=2,n=20,i=!1){if(b(e))return c(0);if(e.valueOf()===BigInt(1))return c(1);if(e.value<m&&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=c(n===0?"1":`0.${"0".repeat(n-1)}1`),a=c(t),B=c(t-1),h=c(2);let u=v(O(e,v(e,h,n)),h,n),l;do{l=u;const f=O(E(B,u),v(e,P(u,t-1),n));u=v(f,a,n)}while(S(_(p(l,u)),r)===1);const o=u.toString();if(i)return e.value=u.value,e.scale=n,e;const g=o.indexOf("."),y=g!==-1?o.slice(0,g+n+1):o;return c(y)}s.Big=w,s.absBig=_,s.addBig=O,s.alignScale=N,s.cloneBig=d,s.compareBig=S,s.createBig=c,s.divBig=v,s.fromString=D,s.isBigObject=M,s.isNumericValue=A,s.maxBig=V,s.minBig=q,s.modBig=F,s.mulBig=E,s.powBig=P,s.sqrtBig=G,s.subBig=p,Object.defineProperty(s,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
@@ -10,6 +10,8 @@ export { mulBig } from './operations/mul';
10
10
  export { powBig } from './operations/pow';
11
11
  export { sqrtBig } from './operations/sqrt';
12
12
  export { subBig } from './operations/sub';
13
+ export type { BigPipeline } from './pipeline';
14
+ export { BigPipe, pipeBig } from './pipeline';
13
15
  export type { BigValue, CompareResult, PossibleNumber, RoundingMode } from './types';
14
16
  export { alignScale } from './utils/align-scale';
15
17
  export { cloneBig } from './utils/clone';
@@ -17,3 +19,4 @@ export { createBig } from './utils/create';
17
19
  export { fromString } from './utils/from-string';
18
20
  export { isBigObject } from './utils/is-big-object';
19
21
  export { isNumericValue } from './utils/numeric';
22
+ export { pow10 } from './utils/pow10';
@@ -1,16 +1,5 @@
1
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
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,14 +1,13 @@
1
1
  import { Big } from '../big';
2
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 { RoundingMode } from '../types';
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;
@@ -4,16 +4,14 @@ import { Big } from '../big';
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;
@@ -4,16 +4,14 @@ import { Big } from '../big';
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 minimum 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
- * minBig(new Big(1), new Big(2)); // new Big instance with value 1 (first Big number)
15
- * minBig(new Big(2), new Big(1)); // new Big instance with value 1 (second Big number)
16
- * minBig(new Big(1), new Big(1)); // new Big instance with value 1 (first Big number)
17
- * minBig(new Big(1), new Big(1), true); // first Big number with value 1 and same instance
13
+ * minBig(new Big(1), new Big(2)); // returns the first Big number
14
+ * minBig(new Big(2), new Big(1)); // returns the second Big number
15
+ * minBig(new Big(1), new Big(1)); // returns the first Big number
18
16
  */
19
- export declare const minBig: (a: Big, b: Big, mutable?: boolean) => Big;
17
+ export declare function minBig(a: Big, b: Big): Big;
@@ -4,8 +4,7 @@ import { 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.
8
- * @returns A new Big instance representing the remainder of the division.
7
+ * @returns The mutated dividend instance representing the remainder of the division.
9
8
  * @throws {Error} Division by zero is not allowed.
10
9
  *
11
10
  * @category Operations
@@ -17,4 +16,4 @@ import { Big } from '../big';
17
16
  * console.log(modBig(big2, big1).toString()); // "3"
18
17
  * console.log(modBig(big1, big1).toString()); // "0"
19
18
  */
20
- export declare function modBig(dividend: Big, divisor: Big, mutable?: boolean): Big;
19
+ export declare function modBig(dividend: Big, divisor: Big): Big;
@@ -1,17 +1,5 @@
1
1
  import { Big } from '../big';
2
2
  /**
3
- * Multiplies two Big instances and returns the result as a new Big instance.
4
- *
5
- * @param {Big} a - The first Big instance.
6
- * @param {Big} b - The second Big instance.
7
- * @param {boolean} [mutable=false] - Whether to mutate the first Big instance. Defaults to false.
8
- * @returns {Big} The result of the multiplication.
9
- *
10
- * @category Operations
11
- *
12
- * @example
13
- * mulBig(new Big(1), new Big(2)); // new Big instance with value 2
14
- * mulBig(new Big(1.1), new Big(2.2)); // new Big instance with value 2.42
15
- * mulBig(new Big(1.1), new Big(2)); // new Big instance with value 2.2
3
+ * Multiplies two Big instances and mutates the first one.
16
4
  */
17
- export declare function mulBig(a: Big, b: Big, mutable?: boolean): Big;
5
+ export declare function mulBig(a: Big, b: Big): Big;
@@ -1,20 +1,5 @@
1
1
  import { Big } from '../big';
2
2
  /**
3
- * Raises a Big number to the power of another Big number.
4
- *
5
- * @param {Big} big - The base Big number.
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.
8
- * @returns {Big} - The result of a raised to the power of b.
9
- * @throws {Error} - If the exponent is not a non-negative integer.
10
- *
11
- * @category Operations
12
- *
13
- * @example
14
- * powBig(new Big(2), 0); // new Big instance with value 1
15
- * powBig(new Big(2), 1); // new Big instance with value 2
16
- * powBig(new Big(2), 3); // new Big instance with value 8
17
- * powBig(new Big(2), 3.3); // throws Error
18
- * powBig(new Big(2), -3); // throws Error
3
+ * Raises a Big number to the power and mutates the base instance.
19
4
  */
20
- export declare function powBig(big: Big, exp?: number, mutable?: boolean): Big;
5
+ export declare function powBig(big: Big, exp?: number): Big;
@@ -1,28 +1,7 @@
1
1
  import { Big } from '../big';
2
2
  /**
3
- * Calculates the nth root of a Big instance with custom precision and rounding mode.
4
- *
5
- * Note: it's the most weighted function in the library. It's reusing a lot of other functions, and it's not perfect in performance, recommended to use it only when you need it and with default precision and rounding mode. It uses the Newton's method to calculate the root.
6
- *
7
- * @param {Big} big - The Big instance.
8
- * @param {number} root - The root to calculate. Must be an integer. Defaults to 2.
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.
11
- * @returns {Big} The nth root of the Big instance with the specified precision and rounding mode.
12
- * @throws {Error} - If the root is not an integer.
3
+ * Calculates the nth root of a Big instance and mutates it with the result.
13
4
  *
14
5
  * @category Operations
15
- *
16
- * @example
17
- * const big1 = new Big(16);
18
- * console.log(sqrtBig(big1).toString()); // 4.00000000000000000000
19
- * console.log(sqrtBig(big1, 4, 0).toString()); // 2
20
- * console.log(sqrtBig(big1, 4, 1).toString()); // 2.0
21
- * console.log(sqrtBig(Big(0), 4).toString()); // 0
22
- * console.log(sqrtBig(Big(1), 4).toString()); // 1
23
- * console.log(sqrtBig(Big(-1), 4).toString()); // throws Error
24
- * console.log(sqrtBig(Big(16), 0.5).toString()); // throws Error
25
- * console.log(sqrtBig(Big(16), -2).toString()); // throws Error
26
- * console.log(sqrtBig(Big(16), 4, -1).toString()); // throws Error
27
6
  */
28
- export declare function sqrtBig(big: Big, root?: number, precision?: number, mutable?: boolean): Big;
7
+ export declare function sqrtBig(big: Big, root?: number, precision?: number): Big;
@@ -1,16 +1,5 @@
1
1
  import { Big } from '../big';
2
2
  /**
3
- * Subtracts two big values and returns the result as a new Big instance.
4
- *
5
- * @param {Big} a - The first Big instance.
6
- * @param {Big} b - The second Big instance.
7
- * @param {boolean} [mutable=false] - Whether to mutate the first Big instance. Defaults to false.
8
- * @returns {Big} The result of the subtraction.
9
- *
10
- * @category Operations
11
- *
12
- * @example
13
- * subBig(new Big(1), new Big(2)); // new Big instance with value -1
14
- * subBig(new Big(1.1), new Big(2.2)); // new Big instance with value -1.1
3
+ * Subtracts two Big instances and mutates the first one.
15
4
  */
16
- export declare function subBig(a: Big, b: Big, mutable?: boolean): Big;
5
+ export declare function subBig(a: Big, b: Big): Big;
@@ -0,0 +1,42 @@
1
+ import { Big } from './big';
2
+ import { BigValue, RoundingMode } from './types';
3
+ export type BigPipeline = {
4
+ /**
5
+ * The current Big value inside the pipeline.
6
+ */
7
+ readonly current: Big;
8
+ add: (value: BigValue) => BigPipeline;
9
+ sub: (value: BigValue) => BigPipeline;
10
+ mul: (value: BigValue) => BigPipeline;
11
+ div: (value: BigValue, precision?: number, roundingMode?: RoundingMode) => BigPipeline;
12
+ mod: (value: BigValue) => BigPipeline;
13
+ pow: (exp?: number) => BigPipeline;
14
+ sqrt: (root?: number, precision?: number) => BigPipeline;
15
+ /**
16
+ * Returns the current Big value. Alias to avoid accessing the getter directly.
17
+ */
18
+ value: () => Big;
19
+ /**
20
+ * String representation of the current value.
21
+ */
22
+ toString: (trimZeros?: boolean) => string;
23
+ };
24
+ export declare class BigPipe implements BigPipeline {
25
+ current: Big;
26
+ constructor(initial: BigValue);
27
+ add(value: BigValue): this;
28
+ sub(value: BigValue): this;
29
+ mul(value: BigValue): this;
30
+ div(value: BigValue, precision?: number, roundingMode?: RoundingMode): this;
31
+ mod(value: BigValue): this;
32
+ pow(exp?: number): this;
33
+ sqrt(root?: number, precision?: number): this;
34
+ value(): Big;
35
+ toString(trimZeros?: boolean): string;
36
+ }
37
+ /**
38
+ * Creates a pipeline for chaining Big operations.
39
+ * The pipeline owns and mutates its `Big` instance in-place.
40
+ * Passing an existing `Big` will mutate that instance.
41
+ */
42
+ export declare function pipeBig(initial: BigValue): BigPipeline;
@@ -1,15 +1,12 @@
1
1
  import { Big } from '../big';
2
2
  /**
3
3
  * Aligns the scale(decimal places) of two Big instances before performing arithmetic operations.
4
- * It takes two Big instances as input and returns a tuple (array) of two new Big instances with the same scale.
4
+ * It takes two Big instances as input and mutates the one with the smaller scale to match the other.
5
5
  *
6
6
  * When the scales of the two input instances are different, the function adjusts the value and scale of one of the instances so that their scales are equal, allowing the arithmetic operations to be performed correctly.
7
7
  *
8
- * Note: This function does not modify the input instances. It creates new instances with the aligned scales.
9
- *
10
8
  * @param {Big} a - The first Big instance.
11
9
  * @param {Big} b - The second Big instance.
12
- * @param {boolean} [mutable=false] - Whether to mutate the both Big instances. Defaults to false.
13
10
  * @returns {[Big, Big]} A tuple of two Big instances with the same scale.
14
11
  *
15
12
  * @category Utilities
@@ -21,4 +18,4 @@ import { Big } from '../big';
21
18
  * console.log(x.toString()); // "123.456000"
22
19
  * console.log(y.toString()); // "123.456789"
23
20
  */
24
- export declare function alignScale(a: Big, b: Big, mutable?: boolean): [Big, Big];
21
+ export declare function alignScale(a: Big, b: Big): [Big, Big];
@@ -9,17 +9,17 @@ export declare const DEFAULT_PRECISION = 20;
9
9
  *
10
10
  * @category Constants
11
11
  */
12
- export declare const ZERO_BIGINT: bigint;
12
+ export declare const ZERO_BIGINT = 0n;
13
13
  /**
14
14
  * One as a BigInt.
15
15
  *
16
16
  * @category Constants
17
17
  */
18
- export declare const ONE_BIGINT: bigint;
18
+ export declare const ONE_BIGINT = 1n;
19
19
  /**
20
20
  * Ten as a BigInt.
21
21
  * Often used as scale multiplier to adjust the scale of a Big instance.
22
22
  *
23
23
  * @category Constants
24
24
  */
25
- export declare const TEN_BIGINT: bigint;
25
+ export declare const TEN_BIGINT = 10n;
@@ -1,5 +1,5 @@
1
1
  import { Big } from '../big';
2
- import { BigObject, PossibleNumber } from '../types';
2
+ import { BigObject, BigValue, PossibleNumber } from '../types';
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.
@@ -35,3 +35,7 @@ export declare function createBig(value: PossibleNumber): Big;
35
35
  * const big3 = createBig(123.456, 2); // 1.23, because the scale is 2 and it removes the fractional part
36
36
  */
37
37
  export declare function createBig(value: PossibleNumber, scale: number | string | undefined): Big;
38
+ /**
39
+ * Overload accepting the full BigValue union. Useful for helpers that accept any supported input.
40
+ */
41
+ export declare function createBig(value: BigValue, scale?: number | string | undefined): Big;
@@ -5,7 +5,7 @@ import { BigObject } from '../types';
5
5
  *
6
6
  * @param {string | number} num - The number to convert
7
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.
8
+ * @param {boolean} [returnInstance=true] - Whether to return a Big instance or BigObject. Defaults to true.
9
9
  *
10
10
  * @return {Big | BigObject} - A Big instance or BigObject representing the number
11
11
  * @throws {Error} - If the number is too big or too small
@@ -1,4 +1,3 @@
1
- import { Big } from '../big';
2
1
  import { BigObject } from '../types';
3
2
  /**
4
3
  * 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.
@@ -17,4 +16,4 @@ import { BigObject } from '../types';
17
16
  * isBigObject(123456n); // false
18
17
  * isBigObject({ value: "abc", scale: "2"}); // false
19
18
  */
20
- export declare function isBigObject(value: Big | BigObject | unknown): value is BigObject;
19
+ export declare function isBigObject(value: unknown): value is BigObject;
@@ -0,0 +1 @@
1
+ export declare function pow10(exp: number): bigint;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "big.esm",
3
- "version": "1.3.2",
3
+ "version": "2.0.0",
4
4
  "description": "A small, fast JavaScript library for arbitrary-precision decimal arithmetic.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -14,7 +14,8 @@
14
14
  "docs": "bunx typedoc --out docs",
15
15
  "lint": "bunx biome check",
16
16
  "lint:fix": "bun run lint -- --write",
17
- "benchmark": "bun benchmark.js",
17
+ "benchmark": "bun benchmarks/benchmark.js",
18
+ "benchmark:weight": "bun benchmarks/size-compare.js",
18
19
  "semantic-release": "bunx semantic-release",
19
20
  "prepare": "bunx is-ci || bunx husky"
20
21
  },