@numio/bigmath 1.0.1 → 1.0.3
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/README.md +62 -3
- package/{npm/index.d.ts → index.d.ts} +2 -1
- package/{npm/index.js → index.js} +2 -1
- package/package.json +1 -1
- package/{npm/src → src}/add/index.d.ts +1 -1
- package/{npm/src → src}/add/index.js +1 -1
- package/{npm/src → src}/div/index.d.ts +1 -1
- package/{npm/src → src}/div/index.js +1 -1
- package/src/mul/index.d.ts +2 -0
- package/{npm/src → src}/mul/index.js +1 -1
- package/src/round/constants.d.ts +7 -0
- package/src/round/constants.js +14 -0
- package/src/round/index.d.ts +3 -0
- package/src/round/index.js +45 -0
- package/src/round/types.d.ts +10 -0
- package/src/round/utils.d.ts +4 -0
- package/src/round/utils.js +60 -0
- package/src/sub/index.d.ts +2 -0
- package/{npm/src → src}/sub/index.js +1 -1
- package/build.ts +0 -45
- package/index.ts +0 -6
- package/npm/LICENSE +0 -21
- package/npm/README.md +0 -129
- package/npm/package-lock.json +0 -16
- package/npm/package.json +0 -57
- package/npm/src/mul/index.d.ts +0 -2
- package/npm/src/sub/index.d.ts +0 -2
- package/src/add/index.ts +0 -34
- package/src/add/types.ts +0 -7
- package/src/add/utils.ts +0 -54
- package/src/div/index.ts +0 -26
- package/src/div/types.ts +0 -8
- package/src/div/utils.ts +0 -110
- package/src/mul/index.ts +0 -23
- package/src/mul/types.ts +0 -7
- package/src/mul/utils.ts +0 -56
- package/src/shared/types.ts +0 -16
- package/src/shared/utils.ts +0 -130
- package/src/sub/index.ts +0 -35
- package/src/sub/types.ts +0 -6
- package/src/sub/utils.ts +0 -86
- package/src/types.ts +0 -6
- package/tsconfig.json +0 -9
- /package/{npm/src → src}/add/types.d.ts +0 -0
- /package/{npm/src → src}/add/utils.d.ts +0 -0
- /package/{npm/src → src}/add/utils.js +0 -0
- /package/{npm/src → src}/div/types.d.ts +0 -0
- /package/{npm/src → src}/div/utils.d.ts +0 -0
- /package/{npm/src → src}/div/utils.js +0 -0
- /package/{npm/src → src}/mul/types.d.ts +0 -0
- /package/{npm/src → src}/mul/utils.d.ts +0 -0
- /package/{npm/src → src}/mul/utils.js +0 -0
- /package/{npm/src → src}/shared/types.d.ts +0 -0
- /package/{npm/src → src}/shared/utils.d.ts +0 -0
- /package/{npm/src → src}/shared/utils.js +0 -0
- /package/{npm/src → src}/sub/types.d.ts +0 -0
- /package/{npm/src → src}/sub/utils.d.ts +0 -0
- /package/{npm/src → src}/sub/utils.js +0 -0
- /package/{npm/src → src}/types.d.ts +0 -0
package/README.md
CHANGED
@@ -29,9 +29,15 @@ This library is particularly useful in scenarios where precise calculations with
|
|
29
29
|
|
30
30
|
### Latest update
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
Added rounding
|
33
|
+
* round up
|
34
|
+
* round down
|
35
|
+
* half-up
|
36
|
+
* half-down
|
37
|
+
* half-even
|
38
|
+
* half-odd
|
39
|
+
* decimal places to be rounded
|
40
|
+
* significant figures decimals to be rounded
|
35
41
|
|
36
42
|
# Install:
|
37
43
|
|
@@ -107,6 +113,59 @@ const negative = div(["-2", "-3", "2"]); // 3
|
|
107
113
|
div("10", "3"); // 3.33333
|
108
114
|
```
|
109
115
|
|
116
|
+
### Round
|
117
|
+
```javascript
|
118
|
+
round("-1.12345"); // -1
|
119
|
+
round("1.5"); // 2
|
120
|
+
round("1.0"); // 1
|
121
|
+
round("0.00001"); // 0
|
122
|
+
round("9.9"); // 10
|
123
|
+
```
|
124
|
+
|
125
|
+
### Round at position
|
126
|
+
```javascript
|
127
|
+
round("1.12345", { decimals: 1 }); // 1.1
|
128
|
+
round("1.12345", { decimals: 2 }); // 1.12
|
129
|
+
round("1.12234", { decimals: 0 }); // 1
|
130
|
+
round("9.999", { decimals: 2 }); // 10
|
131
|
+
```
|
132
|
+
|
133
|
+
### Round modes
|
134
|
+
```javascript
|
135
|
+
round("1.11", { decimals: 1, roundMode: "up" }); // 1.2
|
136
|
+
round("1.19", { decimals: 1, roundMode: "up" }); // 1.2
|
137
|
+
|
138
|
+
round("1.11", { decimals: 1, roundMode: "down" }); // 1.1
|
139
|
+
round("1.19", { decimals: 1, roundMode: "down" }); // 1.1
|
140
|
+
|
141
|
+
round("1.15", { decimals: 1, roundMode: "half-up" }); // 1.2
|
142
|
+
round("1.15", { decimals: 1, roundMode: "half-down" }); // 1.1
|
143
|
+
|
144
|
+
round("1.15", { decimals: 1, roundMode: "half-even" }); // 1.2
|
145
|
+
round("1.25", { decimals: 1, roundMode: "half-even" }); // 1.2
|
146
|
+
round("1.35", { decimals: 1, roundMode: "half-even" }); // 1.4
|
147
|
+
round("1.45", { decimals: 1, roundMode: "half-even" }); // 1.4
|
148
|
+
round("1.55", { decimals: 1, roundMode: "half-even" }); // 1.6
|
149
|
+
|
150
|
+
round("1.15", { decimals: 1, roundMode: "half-odd" }); // 1.1
|
151
|
+
round("1.25", { decimals: 1, roundMode: "half-odd" }); // 1.3
|
152
|
+
round("1.35", { decimals: 1, roundMode: "half-odd" }); // 1.3
|
153
|
+
round("1.45", { decimals: 1, roundMode: "half-odd" }); // 1.5
|
154
|
+
round("1.55", { decimals: 1, roundMode: "half-odd" }); // 1.5
|
155
|
+
```
|
156
|
+
|
157
|
+
### Round with "significant figures" flag
|
158
|
+
```javascript
|
159
|
+
round("0.000119", { decimals: 2, sigFig: false }); // 0
|
160
|
+
round("0.000119", { decimals: 2, sigFig: true }); // 0.00012
|
161
|
+
|
162
|
+
round("0.0019", { decimals: 1, sigFig: true, roundMode: "down" }); // 0.001
|
163
|
+
round("0.0011", { decimals: 1, sigFig: true, roundMode: "up" }); // 0.002
|
164
|
+
|
165
|
+
round("1.000119", { decimals: 2, sigFig: false }); // 1
|
166
|
+
round("1.000119", { decimals: 2, sigFig: true }); // 1
|
167
|
+
```
|
168
|
+
|
110
169
|
Does not have a limitation on the number of digits. You can use any length you'd
|
111
170
|
like
|
112
171
|
|
@@ -2,4 +2,5 @@ import { add } from "./src/add/index.d.ts";
|
|
2
2
|
import { mul } from "./src/mul/index.d.ts";
|
3
3
|
import { sub } from "./src/sub/index.d.ts";
|
4
4
|
import { div } from "./src/div/index.d.ts";
|
5
|
-
|
5
|
+
import { round } from "./src/round/index.d.ts";
|
6
|
+
export { add, div, mul, round, sub };
|
@@ -2,4 +2,5 @@ import { add } from "./src/add/index.js";
|
|
2
2
|
import { mul } from "./src/mul/index.js";
|
3
3
|
import { sub } from "./src/sub/index.js";
|
4
4
|
import { div } from "./src/div/index.js";
|
5
|
-
|
5
|
+
import { round } from "./src/round/index.js";
|
6
|
+
export { add, div, mul, round, sub };
|
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": "1.0.
|
4
|
+
"version": "1.0.3",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
@@ -1,2 +1,2 @@
|
|
1
|
-
/** This function adds
|
1
|
+
/** This function adds numbers (as string). */
|
2
2
|
export declare function add(strs: string[]): string;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { a2sSA, s2aSA } from "../shared/utils.js";
|
2
2
|
import { subtract } from "../sub/utils.js";
|
3
3
|
import { addition } from "./utils.js";
|
4
|
-
/** This function adds
|
4
|
+
/** This function adds numbers (as string). */
|
5
5
|
export function add(strs) {
|
6
6
|
var arrays = strs.map(function (str) { return s2aSA(str); });
|
7
7
|
var inputData = arrays.reduce(function (left, right) {
|
@@ -1,2 +1,2 @@
|
|
1
|
-
/** This function should divide
|
1
|
+
/** This function should divide numbers (as string). */
|
2
2
|
export declare const div: (strs: string[], limit?: number) => string;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { a2sMD, s2aMD } from "../shared/utils.js";
|
2
2
|
import { division } from "./utils.js";
|
3
|
-
/** This function should divide
|
3
|
+
/** This function should divide numbers (as string). */
|
4
4
|
export var div = function (strs, limit) {
|
5
5
|
if (limit === void 0) { limit = 20; }
|
6
6
|
if (strs[0] === "0")
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { a2sMD, s2aMD } from "../shared/utils.js";
|
2
2
|
import { multiplication } from "./utils.js";
|
3
|
-
/** This function multiplies
|
3
|
+
/** This function multiplies numbers (as string). */
|
4
4
|
export var mul = function (strs) {
|
5
5
|
var arrays = strs.map(function (str) { return s2aMD(str); });
|
6
6
|
var inputData = arrays.reduce(function (left, right) {
|
@@ -0,0 +1,7 @@
|
|
1
|
+
export declare const UP = "up";
|
2
|
+
export declare const DOWN = "down";
|
3
|
+
export declare const HALF_UP = "half-up";
|
4
|
+
export declare const HALF_DOWN = "half-down";
|
5
|
+
export declare const HALF_EVEN = "half-even";
|
6
|
+
export declare const HALF_ODD = "half-odd";
|
7
|
+
export declare const ROUND_MODE: readonly ["up", "down", "half-up", "half-down", "half-even", "half-odd"];
|
@@ -0,0 +1,14 @@
|
|
1
|
+
export var UP = "up";
|
2
|
+
export var DOWN = "down";
|
3
|
+
export var HALF_UP = "half-up";
|
4
|
+
export var HALF_DOWN = "half-down";
|
5
|
+
export var HALF_EVEN = "half-even";
|
6
|
+
export var HALF_ODD = "half-odd";
|
7
|
+
export var ROUND_MODE = [
|
8
|
+
UP,
|
9
|
+
DOWN,
|
10
|
+
HALF_UP,
|
11
|
+
HALF_DOWN,
|
12
|
+
HALF_EVEN,
|
13
|
+
HALF_ODD,
|
14
|
+
];
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import { HALF_UP } from "./constants.js";
|
2
|
+
import { calcLast, handleCarryOver, handleTail } from "./utils.js";
|
3
|
+
/** This function round number. */
|
4
|
+
export var round = function (value, options) {
|
5
|
+
var _a;
|
6
|
+
var _b, _c, _d, _e;
|
7
|
+
var roundMode = (_b = options === null || options === void 0 ? void 0 : options.roundMode) !== null && _b !== void 0 ? _b : HALF_UP;
|
8
|
+
var array = [];
|
9
|
+
var sigFig = (_c = options === null || options === void 0 ? void 0 : options.sigFig) !== null && _c !== void 0 ? _c : false;
|
10
|
+
var decimals = (_d = options === null || options === void 0 ? void 0 : options.decimals) !== null && _d !== void 0 ? _d : 0;
|
11
|
+
var isFloat = false;
|
12
|
+
var isNill = true;
|
13
|
+
for (var i = 0; i < value.length; i++) {
|
14
|
+
var charCode = value.charCodeAt(i);
|
15
|
+
if (sigFig && charCode > 48)
|
16
|
+
sigFig = false;
|
17
|
+
if (isNill && charCode > 48)
|
18
|
+
isNill = false;
|
19
|
+
if (isFloat && !sigFig)
|
20
|
+
decimals -= 1;
|
21
|
+
if (charCode === 46)
|
22
|
+
isFloat = true;
|
23
|
+
if (isFloat && decimals === 0) {
|
24
|
+
var next = (_e = value.charCodeAt(i + 1)) !== null && _e !== void 0 ? _e : 0;
|
25
|
+
var prev = value.charCodeAt(i - 1);
|
26
|
+
var curr = charCode === 46 ? prev : charCode;
|
27
|
+
var _f = calcLast(curr, next, roundMode), last = _f[0], hasCarryOver = _f[1];
|
28
|
+
if (charCode === 46)
|
29
|
+
isFloat = false;
|
30
|
+
if (hasCarryOver) {
|
31
|
+
_a = handleCarryOver(array, isFloat), array = _a[0], isFloat = _a[1];
|
32
|
+
break;
|
33
|
+
}
|
34
|
+
charCode === 46
|
35
|
+
? array[array.length - 1] += last - prev
|
36
|
+
: array.push(last);
|
37
|
+
break;
|
38
|
+
}
|
39
|
+
array.push(charCode);
|
40
|
+
}
|
41
|
+
handleTail(array, isFloat);
|
42
|
+
return (isNill && array[array.length - 1] <= 48)
|
43
|
+
? "0"
|
44
|
+
: String.fromCharCode.apply(String, array);
|
45
|
+
};
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import type { ROUND_MODE } from "./constants.d.ts";
|
2
|
+
export type Round = (value: string, options?: {
|
3
|
+
decimals?: number;
|
4
|
+
roundMode?: RoundMode;
|
5
|
+
sigFig?: boolean;
|
6
|
+
}) => string;
|
7
|
+
export type RoundMode = typeof ROUND_MODE[number];
|
8
|
+
export type HandleTail = (array: number[], isFloat: boolean) => void;
|
9
|
+
export type HandleCarryOver = (array: number[], isFloat: boolean) => [number[], boolean];
|
10
|
+
export type CalcLast = (current: number, next: number, roundMode: RoundMode) => [number, boolean];
|
@@ -0,0 +1,60 @@
|
|
1
|
+
import { DOWN, HALF_DOWN, HALF_EVEN, HALF_ODD, HALF_UP, UP, } from "./constants.js";
|
2
|
+
export var handleTail = function (array, isFloat) {
|
3
|
+
var lastIdx = array.length - 1;
|
4
|
+
while (isFloat && array[lastIdx] <= 48) {
|
5
|
+
if (array[lastIdx] === 46 || array.length === 1)
|
6
|
+
isFloat = false;
|
7
|
+
array.length > 1 && array.pop();
|
8
|
+
lastIdx -= 1;
|
9
|
+
}
|
10
|
+
};
|
11
|
+
export var handleCarryOver = function (array, isFloat) {
|
12
|
+
var _a;
|
13
|
+
var hasCarryOver = true;
|
14
|
+
var idx = array.length - 1;
|
15
|
+
var mapFloat = new Map([
|
16
|
+
[57, function () {
|
17
|
+
array.pop();
|
18
|
+
idx -= 1;
|
19
|
+
}],
|
20
|
+
[46, function () {
|
21
|
+
array.pop();
|
22
|
+
idx -= 1;
|
23
|
+
isFloat = false;
|
24
|
+
}],
|
25
|
+
[0, function () {
|
26
|
+
array[idx] += 1;
|
27
|
+
hasCarryOver = false;
|
28
|
+
}],
|
29
|
+
]);
|
30
|
+
var mapInt = new Map([
|
31
|
+
[57, function () {
|
32
|
+
array[idx] = 48;
|
33
|
+
idx -= 1;
|
34
|
+
}],
|
35
|
+
[0, function () {
|
36
|
+
array[idx] += 1;
|
37
|
+
hasCarryOver = false;
|
38
|
+
}],
|
39
|
+
]);
|
40
|
+
while (hasCarryOver && idx >= 0) {
|
41
|
+
var map = isFloat ? mapFloat : mapInt;
|
42
|
+
var key = map.has(array[idx]) ? array[idx] : 0;
|
43
|
+
(_a = map.get(key)) === null || _a === void 0 ? void 0 : _a();
|
44
|
+
}
|
45
|
+
hasCarryOver && array.unshift(49);
|
46
|
+
return [array, isFloat];
|
47
|
+
};
|
48
|
+
export var calcLast = function (current, next, roundMode) {
|
49
|
+
var _a;
|
50
|
+
var isEven = current % 2 === 1;
|
51
|
+
var map = (_a = {},
|
52
|
+
_a[UP] = current + (next > 48 ? 1 : 0),
|
53
|
+
_a[DOWN] = current,
|
54
|
+
_a[HALF_UP] = current + (next >= 53 ? 1 : 0),
|
55
|
+
_a[HALF_DOWN] = current + (next > 53 ? 1 : 0),
|
56
|
+
_a[HALF_EVEN] = current + ((isEven && next >= 53) ? 1 : 0),
|
57
|
+
_a[HALF_ODD] = current + ((!isEven && next >= 53) ? 1 : 0),
|
58
|
+
_a);
|
59
|
+
return map[roundMode] > 57 ? [48, true] : [map[roundMode], false];
|
60
|
+
};
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { addition } from "../add/utils.js";
|
2
2
|
import { a2sSA, s2aSA } from "../shared/utils.js";
|
3
3
|
import { subtract } from "./utils.js";
|
4
|
-
/** This function subtracts
|
4
|
+
/** This function subtracts numbers (as string). */
|
5
5
|
export function sub(strs) {
|
6
6
|
var arrays = strs.map(function (str) { return s2aSA(str); });
|
7
7
|
var inputData = arrays.reduce(function (left, right) {
|
package/build.ts
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
const rootPath = "./npm";
|
2
|
-
const decoder = new TextDecoder("utf-8");
|
3
|
-
const encoder = new TextEncoder();
|
4
|
-
|
5
|
-
async function walk(path: string, cb: ({ path, fileInfo }: CB) => void) {
|
6
|
-
const fileInfo = await Deno.lstat(path);
|
7
|
-
|
8
|
-
cb({ fileInfo, path });
|
9
|
-
|
10
|
-
if (fileInfo.isDirectory) {
|
11
|
-
const entries = await Deno.readDir(path);
|
12
|
-
|
13
|
-
for await (const entry of entries) {
|
14
|
-
await walk(`${path}/${entry.name}`, cb);
|
15
|
-
}
|
16
|
-
}
|
17
|
-
}
|
18
|
-
|
19
|
-
await walk(rootPath, ({ path, fileInfo }) => {
|
20
|
-
if (fileInfo.isFile && path.endsWith(".js")) {
|
21
|
-
const content = decoder.decode(Deno.readFileSync(path));
|
22
|
-
const fixed = content.replaceAll(".ts", ".js");
|
23
|
-
|
24
|
-
Deno.writeFileSync(path, encoder.encode(fixed));
|
25
|
-
}
|
26
|
-
|
27
|
-
if (fileInfo.isFile && path.endsWith(".d.ts")) {
|
28
|
-
const content = decoder.decode(Deno.readFileSync(path));
|
29
|
-
const fixed = content.replaceAll(".ts", ".d.ts");
|
30
|
-
|
31
|
-
Deno.writeFileSync(path, encoder.encode(fixed));
|
32
|
-
}
|
33
|
-
|
34
|
-
if (fileInfo.isFile && path.endsWith("types.js")) {
|
35
|
-
Deno.remove(path);
|
36
|
-
}
|
37
|
-
});
|
38
|
-
|
39
|
-
Deno.copyFileSync("LICENSE", "npm/LICENSE");
|
40
|
-
Deno.copyFileSync("README.md", "npm/README.md");
|
41
|
-
Deno.copyFileSync("package.json", "npm/package.json");
|
42
|
-
Deno.copyFileSync("package-lock.json", "npm/package-lock.json");
|
43
|
-
|
44
|
-
|
45
|
-
type CB = { path: string; fileInfo: Deno.FileInfo };
|
package/index.ts
DELETED
package/npm/LICENSE
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2024 Oleksandr Brudnovskyi
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
13
|
-
copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
-
SOFTWARE.
|
package/npm/README.md
DELETED
@@ -1,129 +0,0 @@
|
|
1
|
-
# @numio/bigmath
|
2
|
-
|
3
|
-
@numio/bigmath is an arbitrary-precision arithmetic library. This library provides functions for performing arithmetic operations (addition, subtraction, multiplication, and division) on numbers of arbitrary length. It addresses the limitations of JavaScript's built-in number type, which suffers from precision loss when dealing with very large or very small numbers, or numbers with more than 15 significant digits.
|
4
|
-
|
5
|
-
|
6
|
-
## Key Features and Benefits
|
7
|
-
|
8
|
-
* **Arbitrary Precision:** Handles numbers of any length, avoiding the limitations of JavaScript's `Number` type. This allows for calculations with extremely large or small numbers without loss of precision.
|
9
|
-
* **No Precision Loss:** Eliminates precision errors that occur when using numeric literals with more than 15 significant digits. The library ensures accurate calculations even with very long numbers.
|
10
|
-
* **Four Basic Operations:** Provides functions for addition (`add`), subtraction (`sub`), multiplication (`mul`), and division (`div`).
|
11
|
-
* **Decimal Handling:** Correctly handles decimal numbers and performs calculations accurately, including scenarios involving negative numbers.
|
12
|
-
* **Division Precision Control:** The `div` function allows you to specify the number of digits after the decimal point for the result. The default precision is 20 digits.
|
13
|
-
* **Easy to Use:** The library provides simple and intuitive functions for performing arithmetic operations.
|
14
|
-
|
15
|
-
|
16
|
-
## How it Solves the Problem
|
17
|
-
|
18
|
-
JavaScript's `Number` type uses a 64-bit floating-point representation (IEEE 754), which can lead to precision issues when dealing with numbers that exceed its representable range or require more than 15 significant digits. This library likely uses a different representation internally (e.g., strings or arrays of digits) to store and manipulate numbers, effectively bypassing the limitations of the built-in `Number` type. This allows it to perform calculations on numbers of virtually unlimited size and maintain accuracy.
|
19
|
-
|
20
|
-
## Use Cases
|
21
|
-
|
22
|
-
This library is particularly useful in scenarios where precise calculations with large numbers are essential, such as:
|
23
|
-
|
24
|
-
* **Financial applications:** Dealing with large sums of money or precise interest calculations.
|
25
|
-
* **Scientific computing:** Working with very large or small numbers in scientific simulations.
|
26
|
-
* **Cryptography:** Implementing cryptographic algorithms that require high precision.
|
27
|
-
* **Any application** where exceeding JavaScript's number limits is a concern.
|
28
|
-
|
29
|
-
|
30
|
-
### Latest update
|
31
|
-
|
32
|
-
In version 1.0.1 arguments for calculation changed to array, which increases performance dramatically \
|
33
|
-
In version 0.3.0 added handling of negative numbers \
|
34
|
-
In version 0.2.0 added division (int & float numbers)
|
35
|
-
|
36
|
-
# Install:
|
37
|
-
|
38
|
-
### NPM
|
39
|
-
|
40
|
-
```bash
|
41
|
-
npm install @numio/bigmath
|
42
|
-
```
|
43
|
-
|
44
|
-
### YARN
|
45
|
-
|
46
|
-
```bash
|
47
|
-
yarn add @numio/bigmath
|
48
|
-
```
|
49
|
-
|
50
|
-
### BUN
|
51
|
-
|
52
|
-
```bash
|
53
|
-
bun add @numio/bigmath
|
54
|
-
```
|
55
|
-
|
56
|
-
### PNPM
|
57
|
-
|
58
|
-
```bash
|
59
|
-
pnpm add @numio/bigmath
|
60
|
-
```
|
61
|
-
|
62
|
-
### DENO
|
63
|
-
|
64
|
-
```bash
|
65
|
-
deno add jsr:@numio/bigmath
|
66
|
-
```
|
67
|
-
|
68
|
-
# Examples:
|
69
|
-
|
70
|
-
### Add numbers
|
71
|
-
|
72
|
-
```javascript
|
73
|
-
import { add } from "@numio/bigmath";
|
74
|
-
|
75
|
-
const int = add(["12345", "99"]); // 124444
|
76
|
-
const float = add(["0.1", "0.2", "0.3"]); // 0.6
|
77
|
-
const negative = add(["0.1", "-0.3", "0.1"]); // -0.1
|
78
|
-
```
|
79
|
-
|
80
|
-
### Subtract numbers
|
81
|
-
```javascript
|
82
|
-
import { sub } from "@numio/bigmath";
|
83
|
-
|
84
|
-
const int = sub(["150", "99"]); // 51
|
85
|
-
const float = sub(["1", "0.99"]); // 0.01
|
86
|
-
const negative = sub(["-0.1", "-0.3", "0.4"]); // -0.2
|
87
|
-
```
|
88
|
-
|
89
|
-
### Multiply numbers
|
90
|
-
```javascript
|
91
|
-
import { mul } from "@numio/bigmath";
|
92
|
-
|
93
|
-
const int = mul(["15", "11", "2"]); // 330
|
94
|
-
const float = mul(["0.01", "0.99"]); // 0.0099
|
95
|
-
const negative = mul(["-2", "3"]); // -6
|
96
|
-
```
|
97
|
-
|
98
|
-
### Divide numbers
|
99
|
-
```javascript
|
100
|
-
import { div } from "@numio/bigmath";
|
101
|
-
|
102
|
-
const int = div(["9999", "33"]); //
|
103
|
-
const float = div(["0.06", "0.2"]); // 0.3
|
104
|
-
const negative = div(["-2", "-3", "2"]); // 3
|
105
|
-
|
106
|
-
// set number of digit after the decimal. By default it's 20
|
107
|
-
div("10", "3"); // 3.33333
|
108
|
-
```
|
109
|
-
|
110
|
-
Does not have a limitation on the number of digits. You can use any length you'd
|
111
|
-
like
|
112
|
-
|
113
|
-
```javascript
|
114
|
-
// NO precision loss using numeric literals with more than 15 significant digits.
|
115
|
-
const int = sub(
|
116
|
-
"999999999999999999999999999999999999999999999999999999999999999",
|
117
|
-
"2",
|
118
|
-
); // "1000000000000000000000000000000000000000000000000000000000000001"
|
119
|
-
|
120
|
-
const float = mul(
|
121
|
-
"0.00000000000000000000000000000000000000000000000000000000000000000009",
|
122
|
-
"0.000000002",
|
123
|
-
); // 0.00000000000000000000000000000000000000000000000000000000000000000000000000018
|
124
|
-
```
|
125
|
-
|
126
|
-
Download from NPM - https://www.npmjs.com/package/@numio/bigmath \
|
127
|
-
Download from JSR - https://jsr.io/@numio/bigmath \
|
128
|
-
Home page - https://github.com/shpunter/numio-bigmath/blob/main/README.md \
|
129
|
-
License - https://github.com/shpunter/numio-bigmath/blob/main/LICENSE
|
package/npm/package-lock.json
DELETED
package/npm/package.json
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"name": "@numio/bigmath",
|
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": "1.0.1",
|
5
|
-
"keywords": [
|
6
|
-
"precision",
|
7
|
-
"arithmetic",
|
8
|
-
"big",
|
9
|
-
"number",
|
10
|
-
"decimal",
|
11
|
-
"float",
|
12
|
-
"biginteger",
|
13
|
-
"bigdecimal",
|
14
|
-
"bignumber",
|
15
|
-
"bigint",
|
16
|
-
"bignum",
|
17
|
-
"infinite",
|
18
|
-
"longnumbers",
|
19
|
-
"infinitenumbers",
|
20
|
-
"integer",
|
21
|
-
"float",
|
22
|
-
"math",
|
23
|
-
"add",
|
24
|
-
"multiply",
|
25
|
-
"subtract",
|
26
|
-
"plus",
|
27
|
-
"minus",
|
28
|
-
"divide",
|
29
|
-
"long division",
|
30
|
-
"bigmath",
|
31
|
-
"numio"
|
32
|
-
],
|
33
|
-
"repository": {
|
34
|
-
"type": "git",
|
35
|
-
"url": "git+https://github.com/shpunter/numio-bigmath.git"
|
36
|
-
},
|
37
|
-
"main": "./index.js",
|
38
|
-
"types": "./index.d.ts",
|
39
|
-
"exports": {
|
40
|
-
".": {
|
41
|
-
"import": "./index.js"
|
42
|
-
},
|
43
|
-
"./package.json": "./package.json"
|
44
|
-
},
|
45
|
-
"author": {
|
46
|
-
"name": "Oleksandr Brudnovskyi ",
|
47
|
-
"email": "oleksandr.brudnovskyi@gmail.com"
|
48
|
-
},
|
49
|
-
"engines": {
|
50
|
-
"node": "*"
|
51
|
-
},
|
52
|
-
"license": "MIT",
|
53
|
-
"scripts": {
|
54
|
-
"test": "node test"
|
55
|
-
},
|
56
|
-
"dependencies": {}
|
57
|
-
}
|
package/npm/src/mul/index.d.ts
DELETED
package/npm/src/sub/index.d.ts
DELETED
package/src/add/index.ts
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
import { a2sSA, s2aSA } from "../shared/utils.ts";
|
2
|
-
import { subtract } from "../sub/utils.ts";
|
3
|
-
import { addition } from "./utils.ts";
|
4
|
-
|
5
|
-
/** This function adds 2 numbers (as string). */
|
6
|
-
export function add(strs: string[]): string {
|
7
|
-
const arrays = strs.map((str) => s2aSA(str));
|
8
|
-
|
9
|
-
const inputData = arrays.reduce((left, right) => {
|
10
|
-
if (left.array.length === 0) return right;
|
11
|
-
|
12
|
-
if (left.isNegative && !right.isNegative) {
|
13
|
-
return subtract(
|
14
|
-
[right.array, right.intLength],
|
15
|
-
[left.array, left.intLength],
|
16
|
-
);
|
17
|
-
}
|
18
|
-
|
19
|
-
if (!left.isNegative && right.isNegative) {
|
20
|
-
return subtract(
|
21
|
-
[left.array, left.intLength],
|
22
|
-
[right.array, right.intLength],
|
23
|
-
);
|
24
|
-
}
|
25
|
-
|
26
|
-
return addition(
|
27
|
-
[left.array, left.intLength],
|
28
|
-
[right.array, right.intLength],
|
29
|
-
left.isNegative && right.isNegative,
|
30
|
-
);
|
31
|
-
}, { array: [], intLength: 0, isFloat: false, isNegative: false });
|
32
|
-
|
33
|
-
return a2sSA(inputData.array, inputData.isFloat, inputData.isNegative);
|
34
|
-
}
|