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 +10 -0
- package/README.md +75 -41
- package/dist/big.esm.js +227 -129
- package/dist/big.esm.umd.cjs +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/operations/abs.d.ts +2 -13
- package/dist/operations/add.d.ts +2 -14
- package/dist/operations/div.d.ts +3 -4
- package/dist/operations/max.d.ts +5 -7
- package/dist/operations/min.d.ts +5 -7
- package/dist/operations/mod.d.ts +2 -3
- package/dist/operations/mul.d.ts +2 -14
- package/dist/operations/pow.d.ts +2 -17
- package/dist/operations/sqrt.d.ts +2 -23
- package/dist/operations/sub.d.ts +2 -13
- package/dist/pipeline.d.ts +42 -0
- package/dist/utils/align-scale.d.ts +2 -5
- package/dist/utils/constants.d.ts +3 -3
- package/dist/utils/create.d.ts +5 -1
- package/dist/utils/from-string.d.ts +1 -1
- package/dist/utils/is-big-object.d.ts +1 -2
- package/dist/utils/pow10.d.ts +1 -0
- package/package.json +3 -2
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 {
|
|
39
|
+
import { addBig, cloneBig, createBig } from "big.esm";
|
|
43
40
|
|
|
44
|
-
const a = createBig("12345678910.12345678910");
|
|
41
|
+
const a = createBig("12345678910.12345678910");
|
|
45
42
|
const b = createBig("9876543210.9876543210");
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
#### `
|
|
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
|
-
|
|
99
|
+
Creates a `Big` instance from a `string`, `number`, `bigint`, `Big`, or `BigObject`. Preferred constructor helper.
|
|
70
100
|
|
|
71
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
121
|
+
#### `addBig(a: Big, b: Big): Big`
|
|
94
122
|
|
|
95
|
-
Adds
|
|
123
|
+
Adds `b` to `a` (mutates `a`).
|
|
96
124
|
|
|
97
|
-
#### `subBig(a: Big, b: Big
|
|
125
|
+
#### `subBig(a: Big, b: Big): Big`
|
|
98
126
|
|
|
99
|
-
Subtracts
|
|
127
|
+
Subtracts `b` from `a` (mutates `a`).
|
|
100
128
|
|
|
101
|
-
#### `mulBig(a: Big, b: Big
|
|
129
|
+
#### `mulBig(a: Big, b: Big): Big`
|
|
102
130
|
|
|
103
|
-
Multiplies
|
|
131
|
+
Multiplies `a` by `b` (mutates `a`).
|
|
104
132
|
|
|
105
|
-
#### `divBig(a: Big, b: Big, precision = 20, roundingMode = "half-up"
|
|
133
|
+
#### `divBig(a: Big, b: Big, precision = 20, roundingMode = "half-up"): Big`
|
|
106
134
|
|
|
107
|
-
Divides
|
|
135
|
+
Divides `a` by `b` (mutates `a`).
|
|
108
136
|
|
|
109
|
-
#### `modBig(a: Big, b: Big
|
|
137
|
+
#### `modBig(a: Big, b: Big): Big`
|
|
110
138
|
|
|
111
|
-
Calculates
|
|
139
|
+
Calculates `a % b` (mutates `a`).
|
|
112
140
|
|
|
113
|
-
#### `powBig(a: Big, exp: number
|
|
141
|
+
#### `powBig(a: Big, exp: number): Big`
|
|
114
142
|
|
|
115
|
-
Raises `a` to
|
|
143
|
+
Raises `a` to `exp` (mutates `a`).
|
|
116
144
|
|
|
117
|
-
#### `sqrtBig(a: Big, root = 2, precision = 20
|
|
145
|
+
#### `sqrtBig(a: Big, root = 2, precision = 20): Big`
|
|
118
146
|
|
|
119
|
-
Calculates the root of `a
|
|
147
|
+
Calculates the root of `a` (mutates `a`).
|
|
120
148
|
|
|
121
|
-
#### `absBig(a: Big
|
|
149
|
+
#### `absBig(a: Big): Big`
|
|
122
150
|
|
|
123
|
-
|
|
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
|
|
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
|
-
|
|
165
|
+
### Pipeline
|
|
132
166
|
|
|
133
|
-
#### `
|
|
167
|
+
#### `pipeBig(initial: BigValue): BigPipeline`
|
|
134
168
|
|
|
135
|
-
|
|
169
|
+
Creates a chainable pipeline that mutates its internal `Big` in-place.
|
|
136
170
|
|
|
137
|
-
|
|
171
|
+
#### `new BigPipe(initial: BigValue): BigPipe`
|
|
138
172
|
|
|
139
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
29
|
-
|
|
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 (
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
|
94
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
|
|
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
|
-
|
|
144
|
+
a = o !== h ? c + m : c;
|
|
125
145
|
break;
|
|
126
146
|
default:
|
|
127
|
-
|
|
147
|
+
a = o * 2n >= i ? c + m : c;
|
|
128
148
|
break;
|
|
129
149
|
}
|
|
130
|
-
return
|
|
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
|
|
133
|
-
function
|
|
134
|
-
|
|
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
|
|
138
|
-
const
|
|
139
|
-
|
|
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
|
|
142
|
-
if (
|
|
143
|
-
throw new Error("
|
|
144
|
-
const
|
|
145
|
-
|
|
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
|
|
175
|
+
function d(e, t = 2) {
|
|
151
176
|
if (!Number.isInteger(t) || t < 0)
|
|
152
|
-
throw new Error("
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
if (e
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
if (
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
};
|
package/dist/big.esm.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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';
|
package/dist/operations/abs.d.ts
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
1
|
import { Big } from '../big';
|
|
2
2
|
/**
|
|
3
|
-
* Returns the absolute value
|
|
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
|
|
5
|
+
export declare function absBig(big: Big): Big;
|
package/dist/operations/add.d.ts
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
1
|
import { Big } from '../big';
|
|
2
2
|
/**
|
|
3
|
-
* Adds two
|
|
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
|
|
5
|
+
export declare function addBig(a: Big, b: Big): Big;
|
package/dist/operations/div.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { Big } from '../big';
|
|
2
2
|
import { RoundingMode } from '../types';
|
|
3
3
|
/**
|
|
4
|
-
* Divides two Big instances and
|
|
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
|
-
* @
|
|
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
|
|
24
|
+
export declare function divBig(dividend: Big, divisor: Big, precision?: number, roundingMode?: RoundingMode): Big;
|
package/dist/operations/max.d.ts
CHANGED
|
@@ -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} -
|
|
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)); //
|
|
15
|
-
* maxBig(new Big(2), new Big(1)); //
|
|
16
|
-
* maxBig(new Big(1), new Big(1)); //
|
|
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
|
|
17
|
+
export declare function maxBig(a: Big, b: Big): Big;
|
package/dist/operations/min.d.ts
CHANGED
|
@@ -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} -
|
|
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)); //
|
|
15
|
-
* minBig(new Big(2), new Big(1)); //
|
|
16
|
-
* minBig(new Big(1), new Big(1)); //
|
|
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
|
|
17
|
+
export declare function minBig(a: Big, b: Big): Big;
|
package/dist/operations/mod.d.ts
CHANGED
|
@@ -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
|
-
* @
|
|
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
|
|
19
|
+
export declare function modBig(dividend: Big, divisor: Big): Big;
|
package/dist/operations/mul.d.ts
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
1
|
import { Big } from '../big';
|
|
2
2
|
/**
|
|
3
|
-
* Multiplies two Big instances and
|
|
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
|
|
5
|
+
export declare function mulBig(a: Big, b: Big): Big;
|
package/dist/operations/pow.d.ts
CHANGED
|
@@ -1,20 +1,5 @@
|
|
|
1
1
|
import { Big } from '../big';
|
|
2
2
|
/**
|
|
3
|
-
* Raises a Big number to the power
|
|
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
|
|
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
|
|
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
|
|
7
|
+
export declare function sqrtBig(big: Big, root?: number, precision?: number): Big;
|
package/dist/operations/sub.d.ts
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
1
|
import { Big } from '../big';
|
|
2
2
|
/**
|
|
3
|
-
* Subtracts two
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
25
|
+
export declare const TEN_BIGINT = 10n;
|
package/dist/utils/create.d.ts
CHANGED
|
@@ -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=
|
|
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:
|
|
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": "
|
|
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
|
},
|