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