big.esm 1.0.2 → 1.1.1
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 +40 -37
- 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 +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.1.1](https://github.com/dschewchenko/big.esm/compare/v1.1.0...v1.1.1) (2023-05-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* readme update and ci update ([d43d486](https://github.com/dschewchenko/big.esm/commit/d43d4869a468d7092674778c97ad888cd4600af8))
|
|
7
|
+
|
|
8
|
+
# [1.1.0](https://github.com/dschewchenko/big.esm/compare/v1.0.2...v1.1.0) (2023-05-14)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* to string must return value without zeros by default ([1938f08](https://github.com/dschewchenko/big.esm/commit/1938f0885a478b768f90c797bae53b6320e8b683))
|
|
14
|
+
|
|
1
15
|
## [1.0.2](https://github.com/dschewchenko/big.esm/compare/v1.0.1...v1.0.2) (2023-05-13)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# big.esm
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/big.esm)
|
|
4
|
+
[](https://www.npmjs.com/package/big.esm)
|
|
5
|
+
|
|
3
6
|
Library for working with large numbers and fractions using `BigInt`.
|
|
4
7
|
It provides advanced functionality for performing arithmetic operations with precision and decimal handling.
|
|
5
8
|
[Documentation](https://dschewchenko.github.io/big.esm/).
|
|
@@ -14,10 +17,11 @@ Fully written in TypeScript, so it provides type definitions out of the box.
|
|
|
14
17
|
|
|
15
18
|
Tree-shaking is supported.
|
|
16
19
|
|
|
17
|
-
Full bundle size(
|
|
18
|
-
`
|
|
20
|
+
Full bundle size(ESM) — 3.12 kB minified and 1.42 kB gzipped.
|
|
21
|
+
`powBig` operation is half of this size, 1.5kB/0.7kB(minified/gzip),
|
|
22
|
+
because it's big and reuses most of the operations from `big.esm`.
|
|
19
23
|
|
|
20
|
-
Full bundle size(UMD) — 3.
|
|
24
|
+
Full bundle size(UMD) — 3.43 kB minified and 1.57 kB gzipped.
|
|
21
25
|
|
|
22
26
|
## Installation
|
|
23
27
|
|
|
@@ -59,10 +63,9 @@ Creates a new `Big` instance from a string, number or `BigInt`. Optionally, you
|
|
|
59
63
|
|
|
60
64
|
### Utilities
|
|
61
65
|
|
|
62
|
-
#### `createBig(value: string | number | bigint): Big`
|
|
66
|
+
#### `createBig(value: string | number | bigint, scale?: number | string): Big`
|
|
63
67
|
|
|
64
|
-
Alias for `new Big(value)`.
|
|
65
|
-
But it does not support the scale parameter and if the value is instance of `Big` it will be returned without changes.
|
|
68
|
+
Alias for `new Big(value, scale)`.
|
|
66
69
|
|
|
67
70
|
#### `cloneBig(a: Big): Big`
|
|
68
71
|
|
|
@@ -90,21 +93,21 @@ Subtracts two `Big` instances.
|
|
|
90
93
|
|
|
91
94
|
Multiplies two `Big` instances.
|
|
92
95
|
|
|
93
|
-
#### `divBig(a: Big, b: Big): Big`
|
|
96
|
+
#### `divBig(a: Big, b: Big, precision = 20, roundingMode = "half-up"): Big`
|
|
94
97
|
|
|
95
|
-
Divides two `Big` instances.
|
|
98
|
+
Divides two `Big` instances. The default precision is 20. The default rounding mode is "half-up".
|
|
96
99
|
|
|
97
100
|
#### `modBig(a: Big, b: Big): Big`
|
|
98
101
|
|
|
99
102
|
Calculates the remainder of dividing two `Big` instances.
|
|
100
103
|
|
|
101
|
-
#### `powBig(a: Big,
|
|
104
|
+
#### `powBig(a: Big, exp: number): Big`
|
|
102
105
|
|
|
103
106
|
Raises `a` to the power of `b`.
|
|
104
107
|
|
|
105
|
-
#### `sqrtBig(a: Big): Big`
|
|
108
|
+
#### `sqrtBig(a: Big, root = 2, precision = 20): Big`
|
|
106
109
|
|
|
107
|
-
Calculates the
|
|
110
|
+
Calculates the root of `a`. The default root is 2, which means the square root. The default precision is 20.
|
|
108
111
|
|
|
109
112
|
#### `absBig(a: Big): Big`
|
|
110
113
|
|
|
@@ -118,32 +121,32 @@ Compares two `Big` instances. Returns `-1` if `a < b`, `0` if `a == b` and `1` i
|
|
|
118
121
|
|
|
119
122
|
Compare `big.esm` with `big.js`. 100_000 iterations for each operation.
|
|
120
123
|
|
|
121
|
-
| Operation
|
|
122
|
-
|
|
123
|
-
| init - smallInt
|
|
124
|
-
| add - smallInt
|
|
125
|
-
| subtract - smallInt
|
|
126
|
-
| multiply - smallInt
|
|
127
|
-
| divide - smallInt
|
|
128
|
-
| power of 4 - smallInt
|
|
129
|
-
| init - smallNumber
|
|
130
|
-
| add - smallNumber
|
|
131
|
-
| subtract - smallNumber
|
|
132
|
-
| multiply - smallNumber
|
|
133
|
-
| divide - smallNumber
|
|
134
|
-
| power of 4 - smallNumber |
|
|
135
|
-
| init - bigInt
|
|
136
|
-
| add - bigInt
|
|
137
|
-
| subtract - bigInt
|
|
138
|
-
| multiply - bigInt
|
|
139
|
-
| divide - bigInt
|
|
140
|
-
| power of 4 - bigInt
|
|
141
|
-
| init - bigNumber
|
|
142
|
-
| add - bigNumber
|
|
143
|
-
| subtract - bigNumber
|
|
144
|
-
| multiply - bigNumber
|
|
145
|
-
| divide - bigNumber
|
|
146
|
-
| power of 4 - bigNumber
|
|
124
|
+
| Operation | big.esm | big.js | Difference |
|
|
125
|
+
|--------------------------|-----------|------------|-----------------|
|
|
126
|
+
| init - smallInt | 39.856ms | 22.169ms | -79.78% slower |
|
|
127
|
+
| add - smallInt | 24.227ms | 15.125ms | -60.17% slower |
|
|
128
|
+
| subtract - smallInt | 22.181ms | 15.094ms | -46.95% slower |
|
|
129
|
+
| multiply - smallInt | 22.276ms | 18.538ms | -20.16% slower |
|
|
130
|
+
| divide - smallInt | 66.923ms | 179.699ms | 62.76% faster |
|
|
131
|
+
| power of 4 - smallInt | 113.714ms | 39.359ms | -188.92% slower |
|
|
132
|
+
| init - smallNumber | 44.606ms | 37.136ms | -20.12% slower |
|
|
133
|
+
| add - smallNumber | 21.719ms | 21.857ms | 0.63% faster |
|
|
134
|
+
| subtract - smallNumber | 21.090ms | 24.064ms | 12.36% faster |
|
|
135
|
+
| multiply - smallNumber | 23.246ms | 35.010ms | 33.60% faster |
|
|
136
|
+
| divide - smallNumber | 60.102ms | 189.603ms | 68.30% faster |
|
|
137
|
+
| power of 4 - smallNumber | 136.611ms | 68.859ms | -98.39% slower |
|
|
138
|
+
| init - bigInt | 50.278ms | 36.880ms | -36.33% slower |
|
|
139
|
+
| add - bigInt | 27.485ms | 25.402ms | -8.20% slower |
|
|
140
|
+
| subtract - bigInt | 27.402ms | 24.622ms | -11.29% slower |
|
|
141
|
+
| multiply - bigInt | 33.650ms | 121.126ms | 72.22% faster |
|
|
142
|
+
| divide - bigInt | 79.523ms | 602.879ms | 86.81% faster |
|
|
143
|
+
| power of 4 - bigInt | 248.514ms | 499.246ms | 50.22% faster |
|
|
144
|
+
| init - bigNumber | 55.661ms | 77.789ms | 28.45% faster |
|
|
145
|
+
| add - bigNumber | 29.755ms | 47.843ms | 37.81% faster |
|
|
146
|
+
| subtract - bigNumber | 29.107ms | 47.313ms | 38.48% faster |
|
|
147
|
+
| multiply - bigNumber | 39.976ms | 266.814ms | 85.02% faster |
|
|
148
|
+
| divide - bigNumber | 78.278ms | 931.610ms | 91.60% faster |
|
|
149
|
+
| power of 4 - bigNumber | 376.169ms | 1132.657ms | 66.79% faster |
|
|
147
150
|
|
|
148
151
|
#### Test data:
|
|
149
152
|
|
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;
|