@numio/bigmath 2.1.2 → 2.2.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 +11 -2
- package/index.d.ts +6 -1
- package/index.js +6 -1
- package/package.json +1 -1
- package/src/isValid/isBinary.d.ts +2 -0
- package/src/isValid/isBinary.js +3 -0
- package/src/isValid/isDecimal.d.ts +2 -0
- package/src/isValid/isDecimal.js +3 -0
- package/src/isValid/isHex.d.ts +2 -0
- package/src/isValid/isHex.js +3 -0
- package/src/isValid/isNumber.d.ts +2 -0
- package/src/isValid/isNumber.js +7 -0
- package/src/isValid/isOctal.d.ts +2 -0
- package/src/isValid/isOctal.js +3 -0
- package/src/isValid/types.d.ts +1 -0
- package/src/shared/utils.js +24 -18
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
### 2.2.0
|
2
|
+
Added `isHex(str: string): boolean` - Checks if a string is a valid hexadecimal number (prefixed with `0x` or `-0x`).
|
3
|
+
Added `isBinary(str: string): boolean` - Checks if a string is a valid binary number (prefixed with `0b` or `-0b`).
|
4
|
+
Added `isDecimal(str: string): boolean` - Checks if a string is a valid decimal number.
|
5
|
+
Added `isOctal(str: string): boolean` - Checks if a string is a valid octal number (prefixed with `0o` or `-0o`).
|
6
|
+
Added `isNumber(str: string): boolean` - Checks if a string is a valid number in any of the formats supported by the library (decimal, hexadecimal, binary, octal).
|
7
|
+
|
8
|
+
### 2.1.3
|
9
|
+
Optimize performance for small numbers
|
10
|
+
|
1
11
|
### 2.1.2
|
2
12
|
Renamed main.ts files
|
3
13
|
Changed import path in test files
|
package/README.md
CHANGED
@@ -12,6 +12,8 @@
|
|
12
12
|
* **Eliminate Precision Loss:** Achieve accurate results even with numeric literals exceeding 15 significant digits, ensuring the integrity of your calculations.
|
13
13
|
* **Precise Decimal Operations:** Execute addition, subtraction, multiplication, and division on decimal numbers with guaranteed accuracy, including scenarios with negative values.
|
14
14
|
* ****NEW! Multi-Base Number Support:** Seamlessly perform arithmetic operations involving **hexadecimal (HEX), octal, binary, and decimal numbers**, offering unparalleled flexibility in handling various number formats.
|
15
|
+
* ****NEW! Number Format Validation:** Easily validate if a string represents a valid **hexadecimal (`isHex`)**, **binary (`isBinary`)**, **decimal (`isDecimal`)**, **octal (`isOctal`)**, or any valid **number (`isNumber`)** format supported by the library.
|
16
|
+
|
15
17
|
|
16
18
|
|
17
19
|
**Unlock Advanced Numerical Operations:**
|
@@ -54,8 +56,13 @@ With `@numio/bigmath`, you can confidently perform complex arithmetic operations
|
|
54
56
|
|
55
57
|
### Latest update
|
56
58
|
|
57
|
-
|
58
|
-
|
59
|
+
**New Functions added:**
|
60
|
+
|
61
|
+
* `isHex` - Checks if a string is a valid hexadecimal number (prefixed with `0x` or `-0x`).
|
62
|
+
* `isBinary` - Checks if a string is a valid binary number (prefixed with `0b` or `-0b`).
|
63
|
+
* `isDecimal` - Checks if a string is a valid decimal number.
|
64
|
+
* `isOctal` - Checks if a string is a valid octal number (prefixed with `0o` or `-0o`).
|
65
|
+
* `isNumber` - Checks if a string is a valid number in any of the formats supported by the library (decimal, hexadecimal, binary, octal).
|
59
66
|
|
60
67
|
# Install:
|
61
68
|
|
@@ -392,4 +399,6 @@ Download from JSR - https://jsr.io/@numio/bigmath
|
|
392
399
|
|
393
400
|
Home page - https://github.com/shpunter/numio-bigmath/blob/main/README.md
|
394
401
|
|
402
|
+
Change log - https://github.com/shpunter/numio-bigmath/blob/main/CHANGELOG.md
|
403
|
+
|
395
404
|
License - https://github.com/shpunter/numio-bigmath/blob/main/LICENSE
|
package/index.d.ts
CHANGED
@@ -18,4 +18,9 @@ import { sqrt } from "./src/sqrt/sqrt.d.ts";
|
|
18
18
|
import { cbrt } from "./src/cbrt/cbrt.d.ts";
|
19
19
|
import { abs } from "./src/abs/abs.d.ts";
|
20
20
|
import { toBase } from "./src/toBase/toBase.d.ts";
|
21
|
-
|
21
|
+
import { isHex } from "./src/isValid/isHex.d.ts";
|
22
|
+
import { isBinary } from "./src/isValid/isBinary.d.ts";
|
23
|
+
import { isDecimal } from "./src/isValid/isDecimal.d.ts";
|
24
|
+
import { isOctal } from "./src/isValid/isOctal.d.ts";
|
25
|
+
import { isNumber } from "./src/isValid/isNumber.d.ts";
|
26
|
+
export { abs, add, cbrt, div, IQR, isEqual, isLeftGreater, isLeftGreaterOrEqual, MAD, max, mean, min, mul, Pipe, quartile, round, sort, sqrt, sub, toBase, isHex, isBinary, isDecimal, isOctal, isNumber };
|
package/index.js
CHANGED
@@ -18,4 +18,9 @@ import { sqrt } from "./src/sqrt/sqrt.js";
|
|
18
18
|
import { cbrt } from "./src/cbrt/cbrt.js";
|
19
19
|
import { abs } from "./src/abs/abs.js";
|
20
20
|
import { toBase } from "./src/toBase/toBase.js";
|
21
|
-
|
21
|
+
import { isHex } from "./src/isValid/isHex.js";
|
22
|
+
import { isBinary } from "./src/isValid/isBinary.js";
|
23
|
+
import { isDecimal } from "./src/isValid/isDecimal.js";
|
24
|
+
import { isOctal } from "./src/isValid/isOctal.js";
|
25
|
+
import { isNumber } from "./src/isValid/isNumber.js";
|
26
|
+
export { abs, add, cbrt, div, IQR, isEqual, isLeftGreater, isLeftGreaterOrEqual, MAD, max, mean, min, mul, Pipe, quartile, round, sort, sqrt, sub, toBase, isHex, isBinary, isDecimal, isOctal, isNumber };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@numio/bigmath",
|
3
3
|
"description": "@numio/bigmath is an arbitrary-precision arithmetic library. It can be used for basic operations with decimal numbers (integers and float)",
|
4
|
-
"version": "2.
|
4
|
+
"version": "2.2.0",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { isBinary } from "./isBinary.js";
|
2
|
+
import { isDecimal } from "./isDecimal.js";
|
3
|
+
import { isHex } from "./isHex.js";
|
4
|
+
import { isOctal } from "./isOctal.js";
|
5
|
+
export const isNumber = (str) => {
|
6
|
+
return isBinary(str) || isHex(str) || isOctal(str) || isDecimal(str);
|
7
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export type IsValid = (str: string) => boolean;
|
package/src/shared/utils.js
CHANGED
@@ -20,40 +20,46 @@ export const s2bi = (str) => {
|
|
20
20
|
const fpi = str.indexOf(".");
|
21
21
|
if (fpi === -1)
|
22
22
|
return [BigInt(str), 0];
|
23
|
+
if (str.length < 15 && str[0] !== "0") {
|
24
|
+
return [
|
25
|
+
BigInt(+(str.slice(0, fpi) + str.slice(fpi + 1))),
|
26
|
+
str.length - 1 - fpi,
|
27
|
+
];
|
28
|
+
}
|
23
29
|
return [
|
24
30
|
BigInt(str.slice(0, fpi) + str.slice(fpi + 1)),
|
25
|
-
|
31
|
+
str.length - 1 - fpi,
|
26
32
|
];
|
27
33
|
};
|
28
34
|
export const calcInner = (array, op, def) => {
|
29
|
-
let
|
30
|
-
let
|
35
|
+
let totalBi = def ? def[0] : array[0][0];
|
36
|
+
let totalFpe = def ? def[1] : array[0][1];
|
31
37
|
const opm = op(1n, 1n);
|
32
38
|
for (let i = def ? 0 : 1; i < array.length; i++) {
|
33
|
-
const [
|
34
|
-
if (
|
35
|
-
|
39
|
+
const [bi, fpe] = array[i];
|
40
|
+
if (fpe === 0 && totalFpe === 0) {
|
41
|
+
totalBi = op(totalBi, bi);
|
36
42
|
continue;
|
37
43
|
}
|
38
44
|
if (opm === 1n) {
|
39
|
-
|
40
|
-
|
45
|
+
totalBi = op(totalBi, bi);
|
46
|
+
totalFpe += fpe;
|
41
47
|
}
|
42
48
|
else {
|
43
|
-
if (
|
44
|
-
const fpDiff =
|
45
|
-
|
49
|
+
if (totalFpe < fpe) {
|
50
|
+
const fpDiff = fpe - totalFpe;
|
51
|
+
totalBi = op(totalBi * (10n ** BigInt(fpDiff)), bi);
|
46
52
|
}
|
47
|
-
if (
|
48
|
-
|
53
|
+
if (totalFpe > fpe) {
|
54
|
+
totalBi = op(totalBi, bi * (10n ** BigInt(totalFpe - fpe)));
|
49
55
|
}
|
50
|
-
if (
|
51
|
-
|
52
|
-
if (
|
53
|
-
|
56
|
+
if (totalFpe === fpe)
|
57
|
+
totalBi = op(totalBi, bi);
|
58
|
+
if (totalFpe < fpe)
|
59
|
+
totalFpe = fpe;
|
54
60
|
}
|
55
61
|
}
|
56
|
-
return [
|
62
|
+
return [totalBi, totalFpe];
|
57
63
|
};
|
58
64
|
export const fillHead = (len, fpe, isNeg, hasBefore) => {
|
59
65
|
let head = (isNeg ? "-" : "") + (hasBefore ? "" : "0.");
|