big.esm 1.2.2 → 1.3.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 +21 -5
- package/dist/big.esm.js +89 -87
- package/dist/big.esm.umd.cjs +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/operations/max.d.ts +19 -0
- package/dist/operations/min.d.ts +19 -0
- package/package.json +8 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.3.1](https://github.com/dschewchenko/big.esm/compare/v1.3.0...v1.3.1) (2023-06-25)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* docs update ([cac1876](https://github.com/dschewchenko/big.esm/commit/cac1876a8455ff044dc9bab66524e4bba2359cf1))
|
|
7
|
+
|
|
8
|
+
# [1.3.0](https://github.com/dschewchenko/big.esm/compare/v1.2.2...v1.3.0) (2023-06-24)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* add min max operations ([db274ef](https://github.com/dschewchenko/big.esm/commit/db274ef6c0531b8bd6a2e14839c976d51935db48))
|
|
14
|
+
|
|
1
15
|
## [1.2.2](https://github.com/dschewchenko/big.esm/compare/v1.2.1...v1.2.2) (2023-05-28)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -13,8 +13,8 @@ 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.
|
|
17
|
-
`sqrtBig` operation is heavy(more than half of bundle), because it reuses most of the operations from `big.esm`.
|
|
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
18
|
|
|
19
19
|
## Installation
|
|
20
20
|
|
|
@@ -24,6 +24,12 @@ Install `big.esm` using npm:
|
|
|
24
24
|
npm install big.esm
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
or yarn:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
yarn add big.esm
|
|
31
|
+
```
|
|
32
|
+
|
|
27
33
|
## Usage
|
|
28
34
|
|
|
29
35
|
```js
|
|
@@ -37,12 +43,12 @@ console.log(result.toString()); // 22222222121.1111111101
|
|
|
37
43
|
|
|
38
44
|
## Compatibility
|
|
39
45
|
|
|
40
|
-
`big.esm` is compatible with all modern browsers and Node.js 10+. It uses `BigInt` internally, so it is not compatible with older browsers and Node.js versions. On info from [caniuse.com](https://caniuse.com/#feat=bigint) `BigInt` is supported by
|
|
46
|
+
`big.esm` is compatible with all modern browsers and Node.js 10+. It uses `BigInt` internally, so it is not compatible with older browsers and Node.js versions. On info from [caniuse.com](https://caniuse.com/#feat=bigint) `BigInt` is supported by 96.47% of all browsers(as of 2023-06-25).
|
|
41
47
|
|
|
42
48
|
## Notes
|
|
43
49
|
|
|
44
50
|
- `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.
|
|
45
|
-
- `
|
|
51
|
+
- `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.
|
|
46
52
|
|
|
47
53
|
## API
|
|
48
54
|
|
|
@@ -76,6 +82,8 @@ Checks if the value is a valid numeric value for creating a `Big` instance.
|
|
|
76
82
|
|
|
77
83
|
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.
|
|
78
84
|
|
|
85
|
+
**Note:** In high-load applications, it is recommended to use the `mutable` option to reduce memory usage and improve performance.
|
|
86
|
+
|
|
79
87
|
#### `addBig(a: Big, b: Big, mutable = false): Big`
|
|
80
88
|
|
|
81
89
|
Adds two `Big` instances.
|
|
@@ -112,9 +120,17 @@ Returns the absolute value of `a`.
|
|
|
112
120
|
|
|
113
121
|
Compares two `Big` instances. Returns `-1` if `a < b`, `0` if `a == b` and `1` if `a > b`.
|
|
114
122
|
|
|
123
|
+
#### `minBig(a: Big, b: Big, mutable = false): Big`
|
|
124
|
+
|
|
125
|
+
Returns the minimum of two `Big` instances. If the numbers are equal, returns the first number.
|
|
126
|
+
|
|
127
|
+
#### `maxBig(a: Big, b: Big, mutable = false): Big`
|
|
128
|
+
|
|
129
|
+
Returns the maximum of two `Big` instances. If the numbers are equal, returns the first number.
|
|
130
|
+
|
|
115
131
|
## Benchmark
|
|
116
132
|
|
|
117
|
-
Benchmark results are available [here](https://
|
|
133
|
+
Benchmark results are available [here](https://github.com/dschewchenko/big.esm/blob/master/BENCHMARK.md).
|
|
118
134
|
|
|
119
135
|
## Roadmap
|
|
120
136
|
|
package/dist/big.esm.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
5
|
-
const m = BigInt(0),
|
|
6
|
-
function
|
|
7
|
-
return e instanceof
|
|
1
|
+
var x = Object.defineProperty;
|
|
2
|
+
var A = (e, t, n) => t in e ? x(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
|
+
var I = (e, t, n) => (A(e, typeof t != "symbol" ? t + "" : t, n), n);
|
|
4
|
+
const M = (e) => typeof e == "bigint" || typeof e == "number" || typeof e == "string" && !isNaN(Number(e)), _ = /0+$/, R = (e) => e.replace(_, "");
|
|
5
|
+
const m = BigInt(0), y = BigInt(1), p = BigInt(10);
|
|
6
|
+
function o(e, t) {
|
|
7
|
+
return e instanceof w ? e : new w(e, t);
|
|
8
8
|
}
|
|
9
|
-
function
|
|
9
|
+
function j(e, t = !1, n = !0) {
|
|
10
10
|
const r = e.toString();
|
|
11
11
|
if (!Number.isFinite(Number(r)))
|
|
12
12
|
throw new Error("Invalid number");
|
|
13
13
|
if (Number(r) === 0)
|
|
14
|
-
return n ?
|
|
15
|
-
let [s,
|
|
14
|
+
return n ? o(m) : { value: m, scale: 0 };
|
|
15
|
+
let [s, i] = r.split("e");
|
|
16
16
|
const g = s.startsWith("-") ? "-" : "";
|
|
17
17
|
s = g ? s.slice(1) : s;
|
|
18
|
-
let [
|
|
18
|
+
let [u, l = ""] = s.split(".");
|
|
19
19
|
l = t ? "" : l;
|
|
20
|
-
const
|
|
21
|
-
if (
|
|
22
|
-
const f = Math.min(
|
|
23
|
-
|
|
24
|
-
} else if (
|
|
25
|
-
const f = Math.min(-
|
|
26
|
-
l =
|
|
20
|
+
const a = Number.parseInt(i);
|
|
21
|
+
if (a > 0) {
|
|
22
|
+
const f = Math.min(a, l.length);
|
|
23
|
+
u += l.slice(0, f), l = l.slice(f), f < a && (u += "0".repeat(a - f));
|
|
24
|
+
} else if (a < 0) {
|
|
25
|
+
const f = Math.min(-a, u.length);
|
|
26
|
+
l = u.slice(-f) + l, u = u.slice(0, u.length - f), f < -a && (l = "0".repeat(-a - f) + l);
|
|
27
27
|
}
|
|
28
|
-
const h = BigInt(g +
|
|
29
|
-
return n ?
|
|
28
|
+
const h = BigInt(g + u + l), c = l.length;
|
|
29
|
+
return n ? o(h, c) : { value: h, scale: c };
|
|
30
30
|
}
|
|
31
|
-
function
|
|
31
|
+
function P(e) {
|
|
32
32
|
return typeof e != "object" || e === null ? !1 : "value" in e && "scale" in e && typeof e.value == "bigint" && typeof e.scale == "number";
|
|
33
33
|
}
|
|
34
|
-
class
|
|
34
|
+
class w {
|
|
35
35
|
/**
|
|
36
36
|
* Implements the overloaded constructors for the Big class.
|
|
37
37
|
* @param {BigValue} value - The value to create the Big instance from. Can be a number, string, BigInt, or another Big instance.
|
|
@@ -43,20 +43,20 @@ class p {
|
|
|
43
43
|
/**
|
|
44
44
|
* The value of the Big instance.
|
|
45
45
|
*/
|
|
46
|
-
|
|
46
|
+
I(this, "value");
|
|
47
47
|
/**
|
|
48
48
|
* The scale of the Big instance, representing the number of decimal places.
|
|
49
49
|
*/
|
|
50
|
-
|
|
51
|
-
if (
|
|
50
|
+
I(this, "scale");
|
|
51
|
+
if (P(t))
|
|
52
52
|
this.value = t.value, this.scale = t.scale;
|
|
53
|
-
else if (
|
|
53
|
+
else if (M(t)) {
|
|
54
54
|
let r = n !== void 0;
|
|
55
55
|
if (r && (typeof n != "number" && (n = Number(n)), r = !isNaN(n), n = r ? Math.floor(n) : 0), typeof t == "bigint")
|
|
56
56
|
this.value = t, this.scale = r ? n : 0;
|
|
57
57
|
else {
|
|
58
|
-
const { value: s, scale:
|
|
59
|
-
this.value = s, this.scale = r ? n :
|
|
58
|
+
const { value: s, scale: i } = j(t, r, !1);
|
|
59
|
+
this.value = s, this.scale = r ? n : i;
|
|
60
60
|
}
|
|
61
61
|
} else
|
|
62
62
|
throw new TypeError("Invalid type provided to Big constructor.");
|
|
@@ -81,8 +81,8 @@ class p {
|
|
|
81
81
|
r && (n = n.substring(1));
|
|
82
82
|
let s = "";
|
|
83
83
|
if (this.scale > m) {
|
|
84
|
-
const
|
|
85
|
-
s = n.slice(
|
|
84
|
+
const i = n.length > this.scale ? n.length - Number(this.scale) : 0;
|
|
85
|
+
s = n.slice(i).padStart(Number(this.scale), "0"), t && (s = R(s)), s.length > 0 && (s = `.${s}`), n = n.slice(0, i);
|
|
86
86
|
}
|
|
87
87
|
return `${r}${n || "0"}${s}`;
|
|
88
88
|
}
|
|
@@ -95,111 +95,113 @@ class p {
|
|
|
95
95
|
return this.toString();
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
function
|
|
98
|
+
function S(e, t, n = !1) {
|
|
99
99
|
const r = e.scale - t.scale;
|
|
100
100
|
if (r === 0)
|
|
101
101
|
return [e, t];
|
|
102
|
-
const s =
|
|
102
|
+
const s = p ** BigInt(Math.abs(r));
|
|
103
103
|
if (n)
|
|
104
104
|
return r > 0 ? (t.value *= s, t.scale = e.scale) : (e.value *= s, e.scale = t.scale), [e, t];
|
|
105
|
-
const
|
|
106
|
-
return [
|
|
107
|
-
}
|
|
108
|
-
function T(e, t, n = !1) {
|
|
109
|
-
const [r, s] = E(e, t), a = r.value + s.value;
|
|
110
|
-
return n ? (e.value = a, e.scale = r.scale, e) : u(a, r.scale);
|
|
105
|
+
const i = r > 0 ? e : o(e.value * s, t.scale), g = r < 0 ? t : o(t.value * s, e.scale);
|
|
106
|
+
return [i, g];
|
|
111
107
|
}
|
|
112
|
-
function
|
|
113
|
-
const [r, s] =
|
|
114
|
-
return n ? (e.value =
|
|
108
|
+
function O(e, t, n = !1) {
|
|
109
|
+
const [r, s] = S(e, t), i = r.value + s.value;
|
|
110
|
+
return n ? (e.value = i, e.scale = r.scale, e) : o(i, r.scale);
|
|
115
111
|
}
|
|
116
112
|
function N(e, t, n = !1) {
|
|
113
|
+
const [r, s] = S(e, t, n), i = r.value - s.value;
|
|
114
|
+
return n ? (e.value = i, e) : o(i, r.scale);
|
|
115
|
+
}
|
|
116
|
+
function E(e, t, n = !1) {
|
|
117
117
|
const r = e.value * t.value, s = e.scale + t.scale;
|
|
118
|
-
return n ? (e.value = r, e.scale = s, e) :
|
|
118
|
+
return n ? (e.value = r, e.scale = s, e) : o(r, s);
|
|
119
119
|
}
|
|
120
|
-
const
|
|
120
|
+
const d = (e) => e.value === m;
|
|
121
121
|
function B(e, t, n = 20, r = "half-up", s = !1) {
|
|
122
|
-
if (
|
|
122
|
+
if (d(t))
|
|
123
123
|
throw new Error("Division by zero is not allowed.");
|
|
124
|
-
const
|
|
124
|
+
const i = Math.max(e.scale, t.scale), g = Math.max(0, n), u = e.value * p ** BigInt(i + g - e.scale), l = t.value * p ** BigInt(i - t.scale), a = u / l, h = u % l;
|
|
125
125
|
let c;
|
|
126
126
|
switch (r) {
|
|
127
127
|
case "down":
|
|
128
|
-
c =
|
|
128
|
+
c = a;
|
|
129
129
|
break;
|
|
130
130
|
case "up":
|
|
131
|
-
c = h !== m ?
|
|
131
|
+
c = h !== m ? a + y : a + m;
|
|
132
132
|
break;
|
|
133
133
|
case "half-up":
|
|
134
134
|
default:
|
|
135
|
-
c = h * BigInt(2) >= l ?
|
|
135
|
+
c = h * BigInt(2) >= l ? a + y : a;
|
|
136
136
|
break;
|
|
137
137
|
}
|
|
138
|
-
return s ? (e.value = c, e.scale = g, e) :
|
|
138
|
+
return s ? (e.value = c, e.scale = g, e) : o(c, g);
|
|
139
139
|
}
|
|
140
|
-
function
|
|
141
|
-
if (
|
|
140
|
+
function V(e, t, n = !1) {
|
|
141
|
+
if (d(t))
|
|
142
142
|
throw new Error("Division by zero is not allowed.");
|
|
143
143
|
const r = B(e, t, 0, "down");
|
|
144
144
|
if (n)
|
|
145
|
-
return
|
|
146
|
-
const s =
|
|
147
|
-
return
|
|
145
|
+
return E(r, t, !0), N(e, r, !0);
|
|
146
|
+
const s = E(r, t);
|
|
147
|
+
return N(e, s);
|
|
148
148
|
}
|
|
149
|
-
function
|
|
149
|
+
function $(e, t = 2, n = !1) {
|
|
150
150
|
if (!Number.isInteger(t) || t < 0)
|
|
151
151
|
throw new Error("The exponent must be a non-negative integer.");
|
|
152
152
|
const r = e.value ** BigInt(t), s = e.scale * t;
|
|
153
|
-
return n ? (e.value = r, e.scale = s, e) :
|
|
153
|
+
return n ? (e.value = r, e.scale = s, e) : o(r, s);
|
|
154
154
|
}
|
|
155
|
-
const
|
|
156
|
-
const [n, r] =
|
|
155
|
+
const T = (e, t) => {
|
|
156
|
+
const [n, r] = S(e, t);
|
|
157
157
|
return n.value > r.value ? 1 : n.value < r.value ? -1 : 0;
|
|
158
158
|
};
|
|
159
|
-
function
|
|
159
|
+
function F(e, t = !1) {
|
|
160
160
|
const n = e.value < m ? -e.value : e.value;
|
|
161
|
-
return t ? (e.value = n, e) :
|
|
161
|
+
return t ? (e.value = n, e) : o(n, e.scale);
|
|
162
162
|
}
|
|
163
|
-
function
|
|
164
|
-
if (
|
|
165
|
-
return
|
|
163
|
+
function Z(e, t = 2, n = 20, r = !1) {
|
|
164
|
+
if (d(e))
|
|
165
|
+
return o(0);
|
|
166
166
|
if (e.valueOf() === BigInt(1))
|
|
167
|
-
return
|
|
167
|
+
return o(1);
|
|
168
168
|
if (e.value < m && t % 2 === 0)
|
|
169
169
|
throw new Error("The root must be an odd integer when the number is negative.");
|
|
170
170
|
if (!Number.isInteger(t) || t < 2)
|
|
171
171
|
throw new Error("The root must be an integer and greater than 1");
|
|
172
172
|
if (n < 0)
|
|
173
173
|
throw new Error("The precision must be a non-negative integer.");
|
|
174
|
-
const s =
|
|
175
|
-
let l = B(
|
|
174
|
+
const s = o(n === 0 ? "1" : "0." + "0".repeat(n - 1) + "1"), i = o(t), g = o(t - 1), u = o(2);
|
|
175
|
+
let l = B(O(e, B(e, u, n)), u, n), a;
|
|
176
176
|
do {
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
l = B(
|
|
180
|
-
} while (
|
|
177
|
+
a = l;
|
|
178
|
+
const D = O(E(g, l), B(e, $(l, t - 1), n));
|
|
179
|
+
l = B(D, i, n);
|
|
180
|
+
} while (T(F(N(a, l)), s) === 1);
|
|
181
181
|
const h = l.toString();
|
|
182
182
|
if (r)
|
|
183
183
|
return e.value = l.value, e.scale = n, e;
|
|
184
184
|
const c = h.indexOf("."), f = c !== -1 ? h.slice(0, c + n + 1) : h;
|
|
185
|
-
return
|
|
185
|
+
return o(f);
|
|
186
186
|
}
|
|
187
|
-
const
|
|
187
|
+
const v = (e) => new w(e.value, e.scale), k = (e, t, n = !1) => T(e, t) > 0 ? n ? t : v(t) : n ? e : v(e), q = (e, t, n = !1) => T(e, t) >= 0 ? n ? e : v(e) : n ? t : v(t);
|
|
188
188
|
export {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
189
|
+
w as Big,
|
|
190
|
+
F as absBig,
|
|
191
|
+
O as addBig,
|
|
192
|
+
S as alignScale,
|
|
193
|
+
v as cloneBig,
|
|
194
|
+
T as compareBig,
|
|
195
|
+
o as createBig,
|
|
196
196
|
B as divBig,
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
197
|
+
j as fromString,
|
|
198
|
+
P as isBigObject,
|
|
199
|
+
M as isNumericValue,
|
|
200
|
+
q as maxBig,
|
|
201
|
+
k as minBig,
|
|
202
|
+
V as modBig,
|
|
203
|
+
E as mulBig,
|
|
204
|
+
$ as powBig,
|
|
205
|
+
Z as sqrtBig,
|
|
206
|
+
N as subBig
|
|
205
207
|
};
|
package/dist/big.esm.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(r,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(r=typeof globalThis<"u"?globalThis:r||self,o((r.big=r.big||{},r.big.esm={})))})(this,function(r){"use strict";var Z=Object.defineProperty;var k=(r,o,d)=>o in r?Z(r,o,{enumerable:!0,configurable:!0,writable:!0,value:d}):r[o]=d;var D=(r,o,d)=>(k(r,typeof o!="symbol"?o+"":o,d),d);const o=e=>typeof e=="bigint"||typeof e=="number"||typeof e=="string"&&!isNaN(Number(e)),d=/0+$/,P=e=>e.replace(d,""),v=BigInt(0),M=BigInt(1),T=BigInt(10);function u(e,t){return e instanceof N?e:new N(e,t)}function A(e,t=!1,n=!0){const i=e.toString();if(!Number.isFinite(Number(i)))throw new Error("Invalid number");if(Number(i)===0)return n?u(v):{value:v,scale:0};let[s,a]=i.split("e");const h=s.startsWith("-")?"-":"";s=h?s.slice(1):s;let[f,l=""]=s.split(".");l=t?"":l;const c=Number.parseInt(a);if(c>0){const B=Math.min(c,l.length);f+=l.slice(0,B),l=l.slice(B),B<c&&(f+="0".repeat(c-B))}else if(c<0){const B=Math.min(-c,f.length);l=f.slice(-B)+l,f=f.slice(0,f.length-B),B<-c&&(l="0".repeat(-c-B)+l)}const m=BigInt(h+f+l),g=l.length;return n?u(m,g):{value:m,scale:g}}function _(e){return typeof e!="object"||e===null?!1:"value"in e&&"scale"in e&&typeof e.value=="bigint"&&typeof e.scale=="number"}class N{constructor(t,n){D(this,"value");D(this,"scale");if(_(t))this.value=t.value,this.scale=t.scale;else if(o(t)){let i=n!==void 0;if(i&&(typeof n!="number"&&(n=Number(n)),i=!isNaN(n),n=i?Math.floor(n):0),typeof t=="bigint")this.value=t,this.scale=i?n:0;else{const{value:s,scale:a}=A(t,i,!1);this.value=s,this.scale=i?n:a}}else throw new TypeError("Invalid type provided to Big constructor.")}valueOf(){return this.value}toString(t=!0){let n=this.value.toString();const i=n.startsWith("-")?"-":"";i&&(n=n.substring(1));let s="";if(this.scale>v){const a=n.length>this.scale?n.length-Number(this.scale):0;s=n.slice(a).padStart(Number(this.scale),"0"),t&&(s=P(s)),s.length>0&&(s=`.${s}`),n=n.slice(0,a)}return`${i}${n||"0"}${s}`}toJSON(){return this.toString()}}function S(e,t,n=!1){const i=e.scale-t.scale;if(i===0)return[e,t];const s=T**BigInt(Math.abs(i));if(n)return i>0?(t.value*=s,t.scale=e.scale):(e.value*=s,e.scale=t.scale),[e,t];const a=i>0?e:u(e.value*s,t.scale),h=i<0?t:u(t.value*s,e.scale);return[a,h]}function O(e,t,n=!1){const[i,s]=S(e,t),a=i.value+s.value;return n?(e.value=a,e.scale=i.scale,e):u(a,i.scale)}function E(e,t,n=!1){const[i,s]=S(e,t,n),a=i.value-s.value;return n?(e.value=a,e):u(a,i.scale)}function p(e,t,n=!1){const i=e.value*t.value,s=e.scale+t.scale;return n?(e.value=i,e.scale=s,e):u(i,s)}const j=e=>e.value===v;function I(e,t,n=20,i="half-up",s=!1){if(j(t))throw new Error("Division by zero is not allowed.");const a=Math.max(e.scale,t.scale),h=Math.max(0,n),f=e.value*T**BigInt(a+h-e.scale),l=t.value*T**BigInt(a-t.scale),c=f/l,m=f%l;let g;switch(i){case"down":g=c;break;case"up":g=m!==v?c+M:c+v;break;case"half-up":default:g=m*BigInt(2)>=l?c+M:c;break}return s?(e.value=g,e.scale=h,e):u(g,h)}function V(e,t,n=!1){if(j(t))throw new Error("Division by zero is not allowed.");const i=I(e,t,0,"down");if(n)return p(i,t,!0),E(e,i,!0);const s=p(i,t);return E(e,s)}function b(e,t=2,n=!1){if(!Number.isInteger(t)||t<0)throw new Error("The exponent must be a non-negative integer.");const i=e.value**BigInt(t),s=e.scale*t;return n?(e.value=i,e.scale=s,e):u(i,s)}const y=(e,t)=>{const[n,i]=S(e,t);return n.value>i.value?1:n.value<i.value?-1:0};function R(e,t=!1){const n=e.value<v?-e.value:e.value;return t?(e.value=n,e):u(n,e.scale)}function $(e,t=2,n=20,i=!1){if(j(e))return u(0);if(e.valueOf()===BigInt(1))return u(1);if(e.value<v&&t%2===0)throw new Error("The root must be an odd integer when the number is negative.");if(!Number.isInteger(t)||t<2)throw new Error("The root must be an integer and greater than 1");if(n<0)throw new Error("The precision must be a non-negative integer.");const s=u(n===0?"1":"0."+"0".repeat(n-1)+"1"),a=u(t),h=u(t-1),f=u(2);let l=I(O(e,I(e,f,n)),f,n),c;do{c=l;const G=O(p(h,l),I(e,b(l,t-1),n));l=I(G,a,n)}while(y(R(E(c,l)),s)===1);const m=l.toString();if(i)return e.value=l.value,e.scale=n,e;const g=m.indexOf("."),B=g!==-1?m.slice(0,g+n+1):m;return u(B)}const w=e=>new N(e.value,e.scale),q=(e,t,n=!1)=>y(e,t)>0?n?t:w(t):n?e:w(e),F=(e,t,n=!1)=>y(e,t)>=0?n?e:w(e):n?t:w(t);r.Big=N,r.absBig=R,r.addBig=O,r.alignScale=S,r.cloneBig=w,r.compareBig=y,r.createBig=u,r.divBig=I,r.fromString=A,r.isBigObject=_,r.isNumericValue=o,r.maxBig=F,r.minBig=q,r.modBig=V,r.mulBig=p,r.powBig=b,r.sqrtBig=$,r.subBig=E,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ export { powBig } from "./operations/pow";
|
|
|
9
9
|
export { sqrtBig } from "./operations/sqrt";
|
|
10
10
|
export { absBig } from "./operations/abs";
|
|
11
11
|
export { compareBig } from "./operations/compare";
|
|
12
|
+
export { minBig } from "./operations/min";
|
|
13
|
+
export { maxBig } from "./operations/max";
|
|
12
14
|
export { isBigObject } from "./utils/is-big-object";
|
|
13
15
|
export { isNumericValue } from "./utils/numeric";
|
|
14
16
|
export { alignScale } from "./utils/align-scale";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Big } from "../big";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the maximum of two Big numbers.
|
|
4
|
+
*
|
|
5
|
+
* @param {Big} a - The first Big number.
|
|
6
|
+
* @param {Big} b - The second Big number.
|
|
7
|
+
* @param {boolean} [mutable=false] - Whether to return same Big instance. Defaults to false.
|
|
8
|
+
*
|
|
9
|
+
* @returns {Big} - The maximum of the two Big numbers. If the two Big numbers are equal, the first Big number is returned.
|
|
10
|
+
*
|
|
11
|
+
* @category Operations
|
|
12
|
+
*
|
|
13
|
+
* @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
|
|
18
|
+
*/
|
|
19
|
+
export declare const maxBig: (a: Big, b: Big, mutable?: boolean) => Big;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Big } from "../big";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the minimum of two Big numbers.
|
|
4
|
+
*
|
|
5
|
+
* @param {Big} a - The first Big number.
|
|
6
|
+
* @param {Big} b - The second Big number.
|
|
7
|
+
* @param {boolean} [mutable=false] - Whether to return same Big instance. Defaults to false.
|
|
8
|
+
*
|
|
9
|
+
* @returns {Big} - The minimum of the two Big numbers. If the two Big numbers are equal, the first Big number is returned.
|
|
10
|
+
*
|
|
11
|
+
* @category Operations
|
|
12
|
+
*
|
|
13
|
+
* @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
|
|
18
|
+
*/
|
|
19
|
+
export declare const minBig: (a: Big, b: Big, mutable?: boolean) => Big;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "big.esm",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "A small, fast JavaScript library for arbitrary-precision decimal arithmetic.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"node": ">=16"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@commitlint/cli": "^17.6.
|
|
54
|
-
"@commitlint/config-conventional": "^17.6.
|
|
53
|
+
"@commitlint/cli": "^17.6.6",
|
|
54
|
+
"@commitlint/config-conventional": "^17.6.6",
|
|
55
55
|
"@semantic-release/changelog": "^6.0.3",
|
|
56
56
|
"@semantic-release/git": "^10.0.1",
|
|
57
57
|
"@types/jest": "^29.5.1",
|
|
@@ -68,12 +68,14 @@
|
|
|
68
68
|
"is-ci": "^3.0.1",
|
|
69
69
|
"prettier": "^2.8.8",
|
|
70
70
|
"rollup-plugin-visualizer": "^5.9.0",
|
|
71
|
-
"semantic-release": "^21.0.
|
|
71
|
+
"semantic-release": "^21.0.5",
|
|
72
72
|
"ts-jest": "^29.1.0",
|
|
73
|
-
"typedoc": "^0.24.
|
|
73
|
+
"typedoc": "^0.24.8",
|
|
74
74
|
"typedoc-plugin-mdn-links": "^3.0.3",
|
|
75
|
+
"typedoc-plugin-merge-modules": "^5.0.1",
|
|
76
|
+
"typedoc-plugin-pages": "^1.1.0",
|
|
75
77
|
"typescript": "^5.0.4",
|
|
76
|
-
"vite": "^4.3.
|
|
78
|
+
"vite": "^4.3.9",
|
|
77
79
|
"vite-plugin-dts": "^2.3.0",
|
|
78
80
|
"vitest": "^0.31.0"
|
|
79
81
|
}
|