@numio/bigmath 1.0.1 → 1.0.2
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 +1 -1
- package/package.json +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/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/index.d.ts → index.d.ts} +0 -0
- /package/{npm/index.js → index.js} +0 -0
- /package/{npm/src → src}/add/index.d.ts +0 -0
- /package/{npm/src → src}/add/index.js +0 -0
- /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/index.d.ts +0 -0
- /package/{npm/src → src}/div/index.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/index.d.ts +0 -0
- /package/{npm/src → src}/mul/index.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/index.d.ts +0 -0
- /package/{npm/src → src}/sub/index.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,7 +29,7 @@ This library is particularly useful in scenarios where precise calculations with
|
|
29
29
|
|
30
30
|
### Latest update
|
31
31
|
|
32
|
-
In version 1.0.
|
32
|
+
In version 1.0.0 arguments for calculation changed to array, which increases performance dramatically \
|
33
33
|
In version 0.3.0 added handling of negative numbers \
|
34
34
|
In version 0.2.0 added division (int & float numbers)
|
35
35
|
|
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.2",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
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/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
|
-
}
|
package/src/add/types.ts
DELETED
package/src/add/utils.ts
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
import type { Addition } from "./types.ts";
|
2
|
-
|
3
|
-
/** This function adds 2 numbers (as array). */
|
4
|
-
export const addition: Addition = ([arrL, intL], [arrR, intR], isNegative) => {
|
5
|
-
const [left, right, intLeft, intRight] = intL >= intR
|
6
|
-
? [arrL, arrR, intL, intR]
|
7
|
-
: [arrR, arrL, intR, intL];
|
8
|
-
const fracLenL = left.length - intLeft;
|
9
|
-
const fracLenR = right.length - intRight;
|
10
|
-
const fracMaxLen = (fracLenL >= fracLenR ? fracLenL : fracLenR) - 1;
|
11
|
-
let pl = (intLeft >= intRight ? intLeft : intRight) + fracMaxLen;
|
12
|
-
let pr = (intLeft >= intRight ? intRight : intLeft) + fracMaxLen;
|
13
|
-
let carryOver = 48;
|
14
|
-
|
15
|
-
if (fracLenL === 0 && fracLenR > 0) left.push(46);
|
16
|
-
|
17
|
-
while (pr >= 0) {
|
18
|
-
if (left[pl] === 46 || right[pr] === 46) {
|
19
|
-
pr -= 1;
|
20
|
-
pl -= 1;
|
21
|
-
}
|
22
|
-
|
23
|
-
const sum = ((left[pl] ?? 48) + (right[pr] ?? 48) + carryOver) -
|
24
|
-
3 * 48;
|
25
|
-
|
26
|
-
if (sum > 9) {
|
27
|
-
left[pl] = (sum % 10) + 48;
|
28
|
-
carryOver = ((sum / 10) | 0) + 48;
|
29
|
-
} else {
|
30
|
-
left[pl] = sum + 48;
|
31
|
-
carryOver = 48;
|
32
|
-
}
|
33
|
-
|
34
|
-
pr -= 1;
|
35
|
-
pl -= 1;
|
36
|
-
}
|
37
|
-
|
38
|
-
while (pl >= 0 && carryOver) {
|
39
|
-
const sum = (left[pl] + carryOver) - 2 * 48;
|
40
|
-
|
41
|
-
left[pl] = (sum % 10) + 48;
|
42
|
-
carryOver = ((sum / 10) | 0) + 48;
|
43
|
-
pl -= 1;
|
44
|
-
}
|
45
|
-
|
46
|
-
carryOver > 48 && left.unshift(carryOver);
|
47
|
-
|
48
|
-
return {
|
49
|
-
array: left,
|
50
|
-
intLength: left.length -1 - fracMaxLen,
|
51
|
-
isNegative,
|
52
|
-
isFloat: fracLenL + fracLenR > 0,
|
53
|
-
};
|
54
|
-
};
|
package/src/div/index.ts
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
import { a2sMD, s2aMD } from "../shared/utils.ts";
|
2
|
-
import { division } from "./utils.ts";
|
3
|
-
|
4
|
-
/** This function should divide 2 numbers (as string). */
|
5
|
-
export const div = (strs: string[], limit = 20): string => {
|
6
|
-
if (strs[0] === "0") return strs[0];
|
7
|
-
|
8
|
-
const arrays = strs.map((str) => s2aMD(str));
|
9
|
-
const inputData = arrays.reduce((left, right) => {
|
10
|
-
if (left.array.length === 0) return right;
|
11
|
-
|
12
|
-
return division(
|
13
|
-
[left.array, left.intLength],
|
14
|
-
[right.array, right.intLength],
|
15
|
-
left.isNegative !== right.isNegative,
|
16
|
-
limit
|
17
|
-
);
|
18
|
-
}, { array: [], intLength: 0, isNegative: false, isFloat: false });
|
19
|
-
|
20
|
-
return a2sMD(
|
21
|
-
inputData.array,
|
22
|
-
inputData.intLength != inputData.array.length,
|
23
|
-
inputData.isNegative,
|
24
|
-
inputData.intLength,
|
25
|
-
);
|
26
|
-
};
|
package/src/div/types.ts
DELETED
package/src/div/utils.ts
DELETED
@@ -1,110 +0,0 @@
|
|
1
|
-
import type { Division } from "./types.ts";
|
2
|
-
|
3
|
-
export const division: Division = (
|
4
|
-
[arrL, intL],
|
5
|
-
[arrR, intR],
|
6
|
-
isNegative,
|
7
|
-
initLimit,
|
8
|
-
) => {
|
9
|
-
const decDiff = (arrL.length - intL) - (arrR.length - intR);
|
10
|
-
const [L, R] = [[arrR, 1], [arrL, -1]] as const;
|
11
|
-
const [arr, abs] = decDiff > 0 ? L : R;
|
12
|
-
|
13
|
-
for (let i = 0; i < decDiff * abs; i++) {
|
14
|
-
arr.push(48);
|
15
|
-
}
|
16
|
-
|
17
|
-
let digit = 48;
|
18
|
-
let limit = initLimit;
|
19
|
-
let isFloat = false;
|
20
|
-
let l = 0;
|
21
|
-
let r = arrR.length - 1;
|
22
|
-
let intLength = 0;
|
23
|
-
const result: number[] = [];
|
24
|
-
const lenArrayL = r - arrL.length;
|
25
|
-
|
26
|
-
for (let i = 0; i <= lenArrayL; i++) {
|
27
|
-
if (i === 0) {
|
28
|
-
isFloat = true;
|
29
|
-
intLength = 1;
|
30
|
-
}
|
31
|
-
|
32
|
-
result.push(48);
|
33
|
-
arrL.push(48);
|
34
|
-
limit -= 1;
|
35
|
-
}
|
36
|
-
|
37
|
-
while (r < arrL.length && limit >= 0) {
|
38
|
-
let comp = false;
|
39
|
-
|
40
|
-
if (arrL[l] === 48) l += 1;
|
41
|
-
|
42
|
-
for (let i = 0; i < arrR.length; i++) {
|
43
|
-
if ((r - l + 1) < arrR.length) comp = true;
|
44
|
-
if ((r - l + 1) !== arrR.length) break;
|
45
|
-
|
46
|
-
if (arrL[l + i] !== arrR[i]) {
|
47
|
-
comp = arrL[l + i] < arrR[i];
|
48
|
-
break;
|
49
|
-
}
|
50
|
-
}
|
51
|
-
|
52
|
-
if (comp) {
|
53
|
-
if (r < arrL.length && (result.length > 0 || digit !== 48)) {
|
54
|
-
if (!isFloat) intLength += 1;
|
55
|
-
result.push(digit);
|
56
|
-
}
|
57
|
-
|
58
|
-
if (r >= arrL.length - 1) {
|
59
|
-
if (initLimit === limit) isFloat = true;
|
60
|
-
arrL.push(48);
|
61
|
-
limit -= 1;
|
62
|
-
}
|
63
|
-
|
64
|
-
r += 1;
|
65
|
-
digit = 48;
|
66
|
-
|
67
|
-
continue;
|
68
|
-
}
|
69
|
-
|
70
|
-
for (let i = arrR.length - 1; i >= 0; i--) {
|
71
|
-
const idx = r - (arrR.length - 1 - i);
|
72
|
-
|
73
|
-
if (arrL[idx] < arrR[i]) {
|
74
|
-
arrL[idx] = arrL[idx] - arrR[i] + 58;
|
75
|
-
arrL[idx - 1] -= 1;
|
76
|
-
} else {
|
77
|
-
arrL[idx] = arrL[idx] - arrR[i] + 48;
|
78
|
-
}
|
79
|
-
}
|
80
|
-
|
81
|
-
digit += 1;
|
82
|
-
}
|
83
|
-
|
84
|
-
const multipliedResult = [];
|
85
|
-
let count = 0;
|
86
|
-
|
87
|
-
if (result[0] === 48) {
|
88
|
-
let isGrZero = false;
|
89
|
-
|
90
|
-
for (let i = 0; i < result.length; i++) {
|
91
|
-
if (!isGrZero) result[i] > 48 ? isGrZero = true : count += 1;
|
92
|
-
|
93
|
-
isGrZero && multipliedResult.push(result[i]);
|
94
|
-
}
|
95
|
-
|
96
|
-
for (let i = 0; i < count; i++) {
|
97
|
-
multipliedResult.push(48);
|
98
|
-
intLength -= 1;
|
99
|
-
}
|
100
|
-
}
|
101
|
-
|
102
|
-
const isZero = result.length === count;
|
103
|
-
|
104
|
-
return {
|
105
|
-
array: result[0] === 48 ? multipliedResult : result,
|
106
|
-
isFloat: isZero ? false : isFloat,
|
107
|
-
isNegative: isZero ? false : isNegative,
|
108
|
-
intLength: isZero ? 1 : intLength,
|
109
|
-
};
|
110
|
-
};
|
package/src/mul/index.ts
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
import { a2sMD, s2aMD } from "../shared/utils.ts";
|
2
|
-
import { multiplication } from "./utils.ts";
|
3
|
-
|
4
|
-
/** This function multiplies 2 numbers (as string). */
|
5
|
-
export const mul = (strs: string[]): string => {
|
6
|
-
const arrays = strs.map((str) => s2aMD(str));
|
7
|
-
const inputData = arrays.reduce((left, right) => {
|
8
|
-
if (left.array.length === 0) return right;
|
9
|
-
|
10
|
-
return multiplication(
|
11
|
-
[left.array, left.intLength],
|
12
|
-
[right.array, right.intLength],
|
13
|
-
left.isNegative !== right.isNegative,
|
14
|
-
);
|
15
|
-
}, { array: [], intLength: 0, isNegative: false, isFloat: false });
|
16
|
-
|
17
|
-
return a2sMD(
|
18
|
-
inputData.array,
|
19
|
-
inputData.intLength != inputData.array.length,
|
20
|
-
inputData.isNegative,
|
21
|
-
inputData.intLength,
|
22
|
-
);
|
23
|
-
};
|
package/src/mul/types.ts
DELETED
package/src/mul/utils.ts
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
import type { Multiplication } from "./types.ts";
|
2
|
-
|
3
|
-
/** This function multiplies 2 numbers (as array). */
|
4
|
-
export const multiplication: Multiplication = (
|
5
|
-
[arrL, intL],
|
6
|
-
[arrR, intR],
|
7
|
-
isNegative,
|
8
|
-
) => {
|
9
|
-
if (
|
10
|
-
arrL.length === 0 || arrR.length === 0 ||
|
11
|
-
(arrL.length === 1 && arrL[0] === 48) ||
|
12
|
-
(arrR.length === 1 && arrR[0] === 48)
|
13
|
-
) {
|
14
|
-
return { array: [48], intLength: 1, isFloat: false, isNegative: false };
|
15
|
-
}
|
16
|
-
|
17
|
-
const dec = (arrL.length - intL) + (arrR.length - intR);
|
18
|
-
|
19
|
-
const [left, right] = arrL.length >= arrR.length
|
20
|
-
? [arrL, arrR]
|
21
|
-
: [arrR, arrL];
|
22
|
-
|
23
|
-
const sums: number[] = Array(right.length + left.length - 1);
|
24
|
-
|
25
|
-
for (let i = right.length - 1; i >= 0; i--) {
|
26
|
-
for (let j = left.length - 1; j >= 0; j--) {
|
27
|
-
const idx = j + i;
|
28
|
-
|
29
|
-
sums[idx] = (sums[idx] ?? 0) +
|
30
|
-
(left[j] - 48) * (right[i] - 48);
|
31
|
-
}
|
32
|
-
}
|
33
|
-
|
34
|
-
const result = Array(sums.length);
|
35
|
-
let idx = sums.length - 1;
|
36
|
-
let carryOver = 0;
|
37
|
-
let currNum = 0;
|
38
|
-
let nextNum = 0;
|
39
|
-
|
40
|
-
while (idx >= 0) {
|
41
|
-
currNum = sums[idx] ?? 0;
|
42
|
-
nextNum = (currNum + carryOver) % 10;
|
43
|
-
carryOver = (carryOver + currNum) / 10 | 0;
|
44
|
-
result[idx] = nextNum + 48;
|
45
|
-
idx -= 1;
|
46
|
-
}
|
47
|
-
|
48
|
-
carryOver > 0 && result.unshift(carryOver + 48);
|
49
|
-
|
50
|
-
return {
|
51
|
-
array: result,
|
52
|
-
intLength: result.length - dec,
|
53
|
-
isFloat: dec > 0,
|
54
|
-
isNegative,
|
55
|
-
};
|
56
|
-
};
|
package/src/shared/types.ts
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
import type { InputData } from "../types.ts";
|
2
|
-
|
3
|
-
export type A2S = (
|
4
|
-
array: number[],
|
5
|
-
isFloat: boolean,
|
6
|
-
isNegative?: boolean,
|
7
|
-
intLength?: number
|
8
|
-
) => string;
|
9
|
-
|
10
|
-
export type S2ASA = (
|
11
|
-
string: string,
|
12
|
-
) => InputData;
|
13
|
-
|
14
|
-
export type S2AMD = (
|
15
|
-
strings: string,
|
16
|
-
) => InputData;
|
package/src/shared/utils.ts
DELETED
@@ -1,130 +0,0 @@
|
|
1
|
-
import type { A2S, S2AMD, S2ASA } from "./types.ts";
|
2
|
-
|
3
|
-
// array to string
|
4
|
-
export const a2sMD: A2S = (
|
5
|
-
array,
|
6
|
-
isFloat,
|
7
|
-
isNegative = false,
|
8
|
-
intLength = 0,
|
9
|
-
) => {
|
10
|
-
const result: number[] = [];
|
11
|
-
let lastValuableIdx = array.length;
|
12
|
-
|
13
|
-
if (isFloat) {
|
14
|
-
let idx = array.length - 1;
|
15
|
-
|
16
|
-
while (array[idx] === 48 && idx >= intLength) {
|
17
|
-
lastValuableIdx = idx;
|
18
|
-
idx -= 1;
|
19
|
-
}
|
20
|
-
}
|
21
|
-
|
22
|
-
if (intLength !== array.length) {
|
23
|
-
if (intLength <= 0) {
|
24
|
-
result.push(48, 46);
|
25
|
-
|
26
|
-
for (let i = intLength; i < 0; i++) {
|
27
|
-
result.push(48);
|
28
|
-
}
|
29
|
-
|
30
|
-
for (let i = 0; i < lastValuableIdx; i++) {
|
31
|
-
result.push(array[i]);
|
32
|
-
}
|
33
|
-
} else {
|
34
|
-
for (let i = 0; i < lastValuableIdx; i++) {
|
35
|
-
if (i === intLength) result.push(46);
|
36
|
-
|
37
|
-
result.push(array[i]);
|
38
|
-
}
|
39
|
-
}
|
40
|
-
|
41
|
-
return (isNegative ? "-" : "") + String.fromCharCode(...result);
|
42
|
-
}
|
43
|
-
|
44
|
-
return (isNegative ? "-" : "") + String.fromCharCode(...array).trim();
|
45
|
-
};
|
46
|
-
|
47
|
-
export const a2sSA: A2S = (array, isFloat, isNegative = false) => {
|
48
|
-
let isToCheckTail = isFloat;
|
49
|
-
|
50
|
-
for (let i = array.length - 1; i >= 0; i--) {
|
51
|
-
if (isToCheckTail && array[i] === 46) {
|
52
|
-
array[i] = 32;
|
53
|
-
isToCheckTail = false;
|
54
|
-
break;
|
55
|
-
}
|
56
|
-
|
57
|
-
if (isToCheckTail && array[i] === 48) array[i] = 32;
|
58
|
-
else break;
|
59
|
-
}
|
60
|
-
|
61
|
-
for (let i = 0; i < array.length; i++) {
|
62
|
-
if (array[i + 1] === 46 || array.length <= 1) break;
|
63
|
-
|
64
|
-
if (array[i] === 48) array[i] = 32;
|
65
|
-
else break;
|
66
|
-
}
|
67
|
-
|
68
|
-
return (isNegative ? "-" : "") + String.fromCharCode(...array).trim();
|
69
|
-
};
|
70
|
-
|
71
|
-
// string to array (sub, add)
|
72
|
-
export const s2aSA: S2ASA = (string) => {
|
73
|
-
const isNegative = string.charCodeAt(0) === 45;
|
74
|
-
const shift = isNegative ? 1 : 0;
|
75
|
-
const array = Array<number>(string.length - shift);
|
76
|
-
let intLength = string.length - shift;
|
77
|
-
let isFloat = false;
|
78
|
-
|
79
|
-
for (let idx = 0 + shift; idx < string.length; idx++) {
|
80
|
-
const charCode = string.charCodeAt(idx);
|
81
|
-
|
82
|
-
if (charCode === 46) {
|
83
|
-
intLength = idx - shift;
|
84
|
-
isFloat || (isFloat = true);
|
85
|
-
}
|
86
|
-
|
87
|
-
array[idx - shift] = charCode;
|
88
|
-
}
|
89
|
-
|
90
|
-
return { array, intLength, isNegative, isFloat };
|
91
|
-
};
|
92
|
-
|
93
|
-
// string to array (mul, div)
|
94
|
-
export const s2aMD: S2AMD = (string) => {
|
95
|
-
const array = Array<number>(0);
|
96
|
-
const isNegative = string.charCodeAt(0) === 45;
|
97
|
-
const shift = isNegative ? 1 : 0;
|
98
|
-
let dec = 0;
|
99
|
-
|
100
|
-
if (
|
101
|
-
(string.length === 1 && string.charCodeAt(0) === 48) ||
|
102
|
-
string.length === 2 && string.charCodeAt(0) === 45 &&
|
103
|
-
string.charCodeAt(1) === 48
|
104
|
-
) {
|
105
|
-
return {
|
106
|
-
array: [48],
|
107
|
-
intLength: 1,
|
108
|
-
isNegative: false,
|
109
|
-
isFloat: false,
|
110
|
-
};
|
111
|
-
}
|
112
|
-
for (let idx = 0 + shift; idx < string.length; idx++) {
|
113
|
-
const charCode = string.charCodeAt(idx);
|
114
|
-
|
115
|
-
if (array.length === 0 && charCode === 48) continue;
|
116
|
-
if (charCode === 46) {
|
117
|
-
dec = string.length - 1 - idx;
|
118
|
-
continue;
|
119
|
-
}
|
120
|
-
|
121
|
-
array.push(charCode);
|
122
|
-
}
|
123
|
-
|
124
|
-
return {
|
125
|
-
array,
|
126
|
-
intLength: array.length - dec,
|
127
|
-
isNegative,
|
128
|
-
isFloat: dec > 0,
|
129
|
-
};
|
130
|
-
};
|
package/src/sub/index.ts
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
import { addition } from "../add/utils.ts";
|
2
|
-
import { a2sSA, s2aSA } from "../shared/utils.ts";
|
3
|
-
import { subtract } from "./utils.ts";
|
4
|
-
|
5
|
-
/** This function subtracts 2 numbers (as string). */
|
6
|
-
|
7
|
-
export function sub(strs: string[]): string {
|
8
|
-
const arrays = strs.map((str) => s2aSA(str));
|
9
|
-
|
10
|
-
const inputData = arrays.reduce((left, right) => {
|
11
|
-
if (left.array.length === 0) return right;
|
12
|
-
|
13
|
-
if (left.isNegative && right.isNegative) {
|
14
|
-
return subtract(
|
15
|
-
[right.array, right.intLength],
|
16
|
-
[left.array, left.intLength],
|
17
|
-
);
|
18
|
-
}
|
19
|
-
|
20
|
-
if (!left.isNegative && !right.isNegative) {
|
21
|
-
return subtract(
|
22
|
-
[left.array, left.intLength],
|
23
|
-
[right.array, right.intLength],
|
24
|
-
);
|
25
|
-
}
|
26
|
-
|
27
|
-
return addition(
|
28
|
-
[left.array, left.intLength],
|
29
|
-
[right.array, right.intLength],
|
30
|
-
left.isNegative,
|
31
|
-
);
|
32
|
-
}, { array: [], intLength: 0, isNegative: false, isFloat: false });
|
33
|
-
|
34
|
-
return a2sSA(inputData.array, inputData.isFloat, inputData.isNegative);
|
35
|
-
}
|
package/src/sub/types.ts
DELETED
package/src/sub/utils.ts
DELETED
@@ -1,86 +0,0 @@
|
|
1
|
-
import type { Subtract } from "./types.ts";
|
2
|
-
|
3
|
-
/** This function subtracts 2 numbers (as array). */
|
4
|
-
export const subtract: Subtract = ([arrL, intL], [arrR, intR]) => {
|
5
|
-
const lenDiff = (intL - intR) * (intL > intR ? 1 : -1);
|
6
|
-
let [left, right, intLeft, intRight] = intL >= intR
|
7
|
-
? [arrL, arrR, intL, intR]
|
8
|
-
: [arrR, arrL, intR, intL];
|
9
|
-
const fracLenL = left.length - intLeft;
|
10
|
-
const fracLenR = right.length - intRight;
|
11
|
-
let pl = lenDiff;
|
12
|
-
let pr = 0;
|
13
|
-
let isLeftBigger = lenDiff > 0;
|
14
|
-
let carryOver = false;
|
15
|
-
let isNegative = intLeft !== intL;
|
16
|
-
|
17
|
-
if (intLeft === left.length && intRight !== right.length) left.push(46);
|
18
|
-
if (intRight === right.length && intLeft !== left.length) right.push(46);
|
19
|
-
|
20
|
-
while (pr < right.length) {
|
21
|
-
if (left[pl] === 46 || right[pr] === 46) {
|
22
|
-
pr += 1;
|
23
|
-
pl += 1;
|
24
|
-
}
|
25
|
-
|
26
|
-
let sub = ((left[pl] ?? 48) - (right[pr] ?? 48)) + 48;
|
27
|
-
|
28
|
-
if (!isLeftBigger && left[pl] > right[pr]) {
|
29
|
-
isLeftBigger = true;
|
30
|
-
}
|
31
|
-
|
32
|
-
if (!isLeftBigger && sub < 48 && lenDiff === 0) {
|
33
|
-
[left, right] = [right, left];
|
34
|
-
[intL, intR] = [intR, intL];
|
35
|
-
isLeftBigger = true;
|
36
|
-
isNegative = true;
|
37
|
-
continue;
|
38
|
-
}
|
39
|
-
|
40
|
-
if (sub < 48) {
|
41
|
-
sub += 10;
|
42
|
-
carryOver = true;
|
43
|
-
}
|
44
|
-
|
45
|
-
let plReverse = pl - 1;
|
46
|
-
let prReverse = pr - 1;
|
47
|
-
|
48
|
-
while (carryOver) {
|
49
|
-
if (left[plReverse] === 46 || right[prReverse] === 46) {
|
50
|
-
plReverse -= 1;
|
51
|
-
prReverse -= 1;
|
52
|
-
}
|
53
|
-
|
54
|
-
if (left[plReverse] !== 48) {
|
55
|
-
plReverse >= 0 && (left[plReverse] -= 1);
|
56
|
-
prReverse >= 0 && (right[prReverse] -= 1);
|
57
|
-
|
58
|
-
carryOver = false;
|
59
|
-
} else {
|
60
|
-
plReverse >= 0 && (left[plReverse] = 57);
|
61
|
-
prReverse >= 0 && (right[prReverse] = 57);
|
62
|
-
}
|
63
|
-
|
64
|
-
plReverse -= 1;
|
65
|
-
prReverse -= 1;
|
66
|
-
}
|
67
|
-
|
68
|
-
left[pl] = sub;
|
69
|
-
right[pr] = sub;
|
70
|
-
|
71
|
-
pl += 1;
|
72
|
-
pr += 1;
|
73
|
-
}
|
74
|
-
|
75
|
-
while (left[0] === 48 && left[1] !== 46 && left.length > 1) {
|
76
|
-
left.shift();
|
77
|
-
intLeft -= 1;
|
78
|
-
}
|
79
|
-
|
80
|
-
return {
|
81
|
-
array: left,
|
82
|
-
intLength: intLeft,
|
83
|
-
isNegative,
|
84
|
-
isFloat: fracLenL + fracLenR > 0,
|
85
|
-
};
|
86
|
-
};
|
package/src/types.ts
DELETED
package/tsconfig.json
DELETED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|