@numio/bigmath 1.0.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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +129 -0
  3. package/build.ts +45 -0
  4. package/index.ts +6 -0
  5. package/npm/LICENSE +21 -0
  6. package/npm/README.md +129 -0
  7. package/npm/index.d.ts +5 -0
  8. package/npm/index.js +5 -0
  9. package/npm/package-lock.json +16 -0
  10. package/npm/package.json +57 -0
  11. package/npm/src/add/index.d.ts +2 -0
  12. package/npm/src/add/index.js +19 -0
  13. package/npm/src/add/types.d.ts +2 -0
  14. package/npm/src/add/utils.d.ts +3 -0
  15. package/npm/src/add/utils.js +48 -0
  16. package/npm/src/div/index.d.ts +2 -0
  17. package/npm/src/div/index.js +15 -0
  18. package/npm/src/div/types.d.ts +2 -0
  19. package/npm/src/div/utils.d.ts +2 -0
  20. package/npm/src/div/utils.js +90 -0
  21. package/npm/src/mul/index.d.ts +2 -0
  22. package/npm/src/mul/index.js +12 -0
  23. package/npm/src/mul/types.d.ts +2 -0
  24. package/npm/src/mul/utils.d.ts +3 -0
  25. package/npm/src/mul/utils.js +42 -0
  26. package/npm/src/shared/types.d.ts +4 -0
  27. package/npm/src/shared/utils.d.ts +5 -0
  28. package/npm/src/shared/utils.js +108 -0
  29. package/npm/src/sub/index.d.ts +2 -0
  30. package/npm/src/sub/index.js +19 -0
  31. package/npm/src/sub/types.d.ts +2 -0
  32. package/npm/src/sub/utils.d.ts +3 -0
  33. package/npm/src/sub/utils.js +76 -0
  34. package/npm/src/types.d.ts +6 -0
  35. package/package.json +57 -0
  36. package/src/add/index.ts +34 -0
  37. package/src/add/types.ts +7 -0
  38. package/src/add/utils.ts +54 -0
  39. package/src/div/index.ts +26 -0
  40. package/src/div/types.ts +8 -0
  41. package/src/div/utils.ts +110 -0
  42. package/src/mul/index.ts +23 -0
  43. package/src/mul/types.ts +7 -0
  44. package/src/mul/utils.ts +56 -0
  45. package/src/shared/types.ts +16 -0
  46. package/src/shared/utils.ts +130 -0
  47. package/src/sub/index.ts +35 -0
  48. package/src/sub/types.ts +6 -0
  49. package/src/sub/utils.ts +86 -0
  50. package/src/types.ts +6 -0
  51. package/tsconfig.json +9 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
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/README.md ADDED
@@ -0,0 +1,129 @@
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/build.ts ADDED
@@ -0,0 +1,45 @@
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 ADDED
@@ -0,0 +1,6 @@
1
+ import { add } from "./src/add/index.ts";
2
+ import { mul } from "./src/mul/index.ts";
3
+ import { sub } from "./src/sub/index.ts";
4
+ import { div } from "./src/div/index.ts";
5
+
6
+ export { add, mul, sub, div };
package/npm/LICENSE ADDED
@@ -0,0 +1,21 @@
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 ADDED
@@ -0,0 +1,129 @@
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/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { add } from "./src/add/index.d.ts";
2
+ import { mul } from "./src/mul/index.d.ts";
3
+ import { sub } from "./src/sub/index.d.ts";
4
+ import { div } from "./src/div/index.d.ts";
5
+ export { add, mul, sub, div };
package/npm/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import { add } from "./src/add/index.js";
2
+ import { mul } from "./src/mul/index.js";
3
+ import { sub } from "./src/sub/index.js";
4
+ import { div } from "./src/div/index.js";
5
+ export { add, mul, sub, div };
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "@numio/bigmath",
3
+ "version": "1.0.1",
4
+ "lockfileVersion": 3,
5
+ "requires": true,
6
+ "packages": {
7
+ "": {
8
+ "name": "@numio/bigmath",
9
+ "version": "1.0.1",
10
+ "license": "MIT",
11
+ "engines": {
12
+ "node": "*"
13
+ }
14
+ }
15
+ }
16
+ }
@@ -0,0 +1,57 @@
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
+ }
@@ -0,0 +1,2 @@
1
+ /** This function adds 2 numbers (as string). */
2
+ export declare function add(strs: string[]): string;
@@ -0,0 +1,19 @@
1
+ import { a2sSA, s2aSA } from "../shared/utils.js";
2
+ import { subtract } from "../sub/utils.js";
3
+ import { addition } from "./utils.js";
4
+ /** This function adds 2 numbers (as string). */
5
+ export function add(strs) {
6
+ var arrays = strs.map(function (str) { return s2aSA(str); });
7
+ var inputData = arrays.reduce(function (left, right) {
8
+ if (left.array.length === 0)
9
+ return right;
10
+ if (left.isNegative && !right.isNegative) {
11
+ return subtract([right.array, right.intLength], [left.array, left.intLength]);
12
+ }
13
+ if (!left.isNegative && right.isNegative) {
14
+ return subtract([left.array, left.intLength], [right.array, right.intLength]);
15
+ }
16
+ return addition([left.array, left.intLength], [right.array, right.intLength], left.isNegative && right.isNegative);
17
+ }, { array: [], intLength: 0, isFloat: false, isNegative: false });
18
+ return a2sSA(inputData.array, inputData.isFloat, inputData.isNegative);
19
+ }
@@ -0,0 +1,2 @@
1
+ import type { InputData } from "../types.d.ts";
2
+ export type Addition = (L: [number[], number], R: [number[], number], isNegative: boolean) => InputData;
@@ -0,0 +1,3 @@
1
+ import type { Addition } from "./types.d.ts";
2
+ /** This function adds 2 numbers (as array). */
3
+ export declare const addition: Addition;
@@ -0,0 +1,48 @@
1
+ /** This function adds 2 numbers (as array). */
2
+ export var addition = function (_a, _b, isNegative) {
3
+ var _c, _d;
4
+ var arrL = _a[0], intL = _a[1];
5
+ var arrR = _b[0], intR = _b[1];
6
+ var _e = intL >= intR
7
+ ? [arrL, arrR, intL, intR]
8
+ : [arrR, arrL, intR, intL], left = _e[0], right = _e[1], intLeft = _e[2], intRight = _e[3];
9
+ var fracLenL = left.length - intLeft;
10
+ var fracLenR = right.length - intRight;
11
+ var fracMaxLen = (fracLenL >= fracLenR ? fracLenL : fracLenR) - 1;
12
+ var pl = (intLeft >= intRight ? intLeft : intRight) + fracMaxLen;
13
+ var pr = (intLeft >= intRight ? intRight : intLeft) + fracMaxLen;
14
+ var carryOver = 48;
15
+ if (fracLenL === 0 && fracLenR > 0)
16
+ left.push(46);
17
+ while (pr >= 0) {
18
+ if (left[pl] === 46 || right[pr] === 46) {
19
+ pr -= 1;
20
+ pl -= 1;
21
+ }
22
+ var sum = (((_c = left[pl]) !== null && _c !== void 0 ? _c : 48) + ((_d = right[pr]) !== null && _d !== void 0 ? _d : 48) + carryOver) -
23
+ 3 * 48;
24
+ if (sum > 9) {
25
+ left[pl] = (sum % 10) + 48;
26
+ carryOver = ((sum / 10) | 0) + 48;
27
+ }
28
+ else {
29
+ left[pl] = sum + 48;
30
+ carryOver = 48;
31
+ }
32
+ pr -= 1;
33
+ pl -= 1;
34
+ }
35
+ while (pl >= 0 && carryOver) {
36
+ var sum = (left[pl] + carryOver) - 2 * 48;
37
+ left[pl] = (sum % 10) + 48;
38
+ carryOver = ((sum / 10) | 0) + 48;
39
+ pl -= 1;
40
+ }
41
+ carryOver > 48 && left.unshift(carryOver);
42
+ return {
43
+ array: left,
44
+ intLength: left.length - 1 - fracMaxLen,
45
+ isNegative: isNegative,
46
+ isFloat: fracLenL + fracLenR > 0,
47
+ };
48
+ };
@@ -0,0 +1,2 @@
1
+ /** This function should divide 2 numbers (as string). */
2
+ export declare const div: (strs: string[], limit?: number) => string;
@@ -0,0 +1,15 @@
1
+ import { a2sMD, s2aMD } from "../shared/utils.js";
2
+ import { division } from "./utils.js";
3
+ /** This function should divide 2 numbers (as string). */
4
+ export var div = function (strs, limit) {
5
+ if (limit === void 0) { limit = 20; }
6
+ if (strs[0] === "0")
7
+ return strs[0];
8
+ var arrays = strs.map(function (str) { return s2aMD(str); });
9
+ var inputData = arrays.reduce(function (left, right) {
10
+ if (left.array.length === 0)
11
+ return right;
12
+ return division([left.array, left.intLength], [right.array, right.intLength], left.isNegative !== right.isNegative, limit);
13
+ }, { array: [], intLength: 0, isNegative: false, isFloat: false });
14
+ return a2sMD(inputData.array, inputData.intLength != inputData.array.length, inputData.isNegative, inputData.intLength);
15
+ };
@@ -0,0 +1,2 @@
1
+ import type { InputData } from "../types.d.ts";
2
+ export type Division = (L: [number[], number], R: [number[], number], isNegative: boolean, initLimit: number) => InputData;
@@ -0,0 +1,2 @@
1
+ import type { Division } from "./types.d.ts";
2
+ export declare const division: Division;
@@ -0,0 +1,90 @@
1
+ export var division = function (_a, _b, isNegative, initLimit) {
2
+ var arrL = _a[0], intL = _a[1];
3
+ var arrR = _b[0], intR = _b[1];
4
+ var decDiff = (arrL.length - intL) - (arrR.length - intR);
5
+ var _c = [[arrR, 1], [arrL, -1]], L = _c[0], R = _c[1];
6
+ var _d = decDiff > 0 ? L : R, arr = _d[0], abs = _d[1];
7
+ for (var i = 0; i < decDiff * abs; i++) {
8
+ arr.push(48);
9
+ }
10
+ var digit = 48;
11
+ var limit = initLimit;
12
+ var isFloat = false;
13
+ var l = 0;
14
+ var r = arrR.length - 1;
15
+ var intLength = 0;
16
+ var result = [];
17
+ var lenArrayL = r - arrL.length;
18
+ for (var i = 0; i <= lenArrayL; i++) {
19
+ if (i === 0) {
20
+ isFloat = true;
21
+ intLength = 1;
22
+ }
23
+ result.push(48);
24
+ arrL.push(48);
25
+ limit -= 1;
26
+ }
27
+ while (r < arrL.length && limit >= 0) {
28
+ var comp = false;
29
+ if (arrL[l] === 48)
30
+ l += 1;
31
+ for (var i = 0; i < arrR.length; i++) {
32
+ if ((r - l + 1) < arrR.length)
33
+ comp = true;
34
+ if ((r - l + 1) !== arrR.length)
35
+ break;
36
+ if (arrL[l + i] !== arrR[i]) {
37
+ comp = arrL[l + i] < arrR[i];
38
+ break;
39
+ }
40
+ }
41
+ if (comp) {
42
+ if (r < arrL.length && (result.length > 0 || digit !== 48)) {
43
+ if (!isFloat)
44
+ intLength += 1;
45
+ result.push(digit);
46
+ }
47
+ if (r >= arrL.length - 1) {
48
+ if (initLimit === limit)
49
+ isFloat = true;
50
+ arrL.push(48);
51
+ limit -= 1;
52
+ }
53
+ r += 1;
54
+ digit = 48;
55
+ continue;
56
+ }
57
+ for (var i = arrR.length - 1; i >= 0; i--) {
58
+ var idx = r - (arrR.length - 1 - i);
59
+ if (arrL[idx] < arrR[i]) {
60
+ arrL[idx] = arrL[idx] - arrR[i] + 58;
61
+ arrL[idx - 1] -= 1;
62
+ }
63
+ else {
64
+ arrL[idx] = arrL[idx] - arrR[i] + 48;
65
+ }
66
+ }
67
+ digit += 1;
68
+ }
69
+ var multipliedResult = [];
70
+ var count = 0;
71
+ if (result[0] === 48) {
72
+ var isGrZero = false;
73
+ for (var i = 0; i < result.length; i++) {
74
+ if (!isGrZero)
75
+ result[i] > 48 ? isGrZero = true : count += 1;
76
+ isGrZero && multipliedResult.push(result[i]);
77
+ }
78
+ for (var i = 0; i < count; i++) {
79
+ multipliedResult.push(48);
80
+ intLength -= 1;
81
+ }
82
+ }
83
+ var isZero = result.length === count;
84
+ return {
85
+ array: result[0] === 48 ? multipliedResult : result,
86
+ isFloat: isZero ? false : isFloat,
87
+ isNegative: isZero ? false : isNegative,
88
+ intLength: isZero ? 1 : intLength,
89
+ };
90
+ };
@@ -0,0 +1,2 @@
1
+ /** This function multiplies 2 numbers (as string). */
2
+ export declare const mul: (strs: string[]) => string;
@@ -0,0 +1,12 @@
1
+ import { a2sMD, s2aMD } from "../shared/utils.js";
2
+ import { multiplication } from "./utils.js";
3
+ /** This function multiplies 2 numbers (as string). */
4
+ export var mul = function (strs) {
5
+ var arrays = strs.map(function (str) { return s2aMD(str); });
6
+ var inputData = arrays.reduce(function (left, right) {
7
+ if (left.array.length === 0)
8
+ return right;
9
+ return multiplication([left.array, left.intLength], [right.array, right.intLength], left.isNegative !== right.isNegative);
10
+ }, { array: [], intLength: 0, isNegative: false, isFloat: false });
11
+ return a2sMD(inputData.array, inputData.intLength != inputData.array.length, inputData.isNegative, inputData.intLength);
12
+ };
@@ -0,0 +1,2 @@
1
+ import type { InputData } from "../types.d.ts";
2
+ export type Multiplication = (arrL: [number[], number], arrR: [number[], number], isNegative: boolean) => InputData;