@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.
Files changed (59) hide show
  1. package/README.md +62 -3
  2. package/{npm/index.d.ts → index.d.ts} +2 -1
  3. package/{npm/index.js → index.js} +2 -1
  4. package/package.json +1 -1
  5. package/{npm/src → src}/add/index.d.ts +1 -1
  6. package/{npm/src → src}/add/index.js +1 -1
  7. package/{npm/src → src}/div/index.d.ts +1 -1
  8. package/{npm/src → src}/div/index.js +1 -1
  9. package/src/mul/index.d.ts +2 -0
  10. package/{npm/src → src}/mul/index.js +1 -1
  11. package/src/round/constants.d.ts +7 -0
  12. package/src/round/constants.js +14 -0
  13. package/src/round/index.d.ts +3 -0
  14. package/src/round/index.js +45 -0
  15. package/src/round/types.d.ts +10 -0
  16. package/src/round/utils.d.ts +4 -0
  17. package/src/round/utils.js +60 -0
  18. package/src/sub/index.d.ts +2 -0
  19. package/{npm/src → src}/sub/index.js +1 -1
  20. package/build.ts +0 -45
  21. package/index.ts +0 -6
  22. package/npm/LICENSE +0 -21
  23. package/npm/README.md +0 -129
  24. package/npm/package-lock.json +0 -16
  25. package/npm/package.json +0 -57
  26. package/npm/src/mul/index.d.ts +0 -2
  27. package/npm/src/sub/index.d.ts +0 -2
  28. package/src/add/index.ts +0 -34
  29. package/src/add/types.ts +0 -7
  30. package/src/add/utils.ts +0 -54
  31. package/src/div/index.ts +0 -26
  32. package/src/div/types.ts +0 -8
  33. package/src/div/utils.ts +0 -110
  34. package/src/mul/index.ts +0 -23
  35. package/src/mul/types.ts +0 -7
  36. package/src/mul/utils.ts +0 -56
  37. package/src/shared/types.ts +0 -16
  38. package/src/shared/utils.ts +0 -130
  39. package/src/sub/index.ts +0 -35
  40. package/src/sub/types.ts +0 -6
  41. package/src/sub/utils.ts +0 -86
  42. package/src/types.ts +0 -6
  43. package/tsconfig.json +0 -9
  44. /package/{npm/src → src}/add/types.d.ts +0 -0
  45. /package/{npm/src → src}/add/utils.d.ts +0 -0
  46. /package/{npm/src → src}/add/utils.js +0 -0
  47. /package/{npm/src → src}/div/types.d.ts +0 -0
  48. /package/{npm/src → src}/div/utils.d.ts +0 -0
  49. /package/{npm/src → src}/div/utils.js +0 -0
  50. /package/{npm/src → src}/mul/types.d.ts +0 -0
  51. /package/{npm/src → src}/mul/utils.d.ts +0 -0
  52. /package/{npm/src → src}/mul/utils.js +0 -0
  53. /package/{npm/src → src}/shared/types.d.ts +0 -0
  54. /package/{npm/src → src}/shared/utils.d.ts +0 -0
  55. /package/{npm/src → src}/shared/utils.js +0 -0
  56. /package/{npm/src → src}/sub/types.d.ts +0 -0
  57. /package/{npm/src → src}/sub/utils.d.ts +0 -0
  58. /package/{npm/src → src}/sub/utils.js +0 -0
  59. /package/{npm/src → src}/types.d.ts +0 -0
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
@@ -1,8 +0,0 @@
1
- import type { InputData } from "../types.ts";
2
-
3
- export type Division = (
4
- L: [number[], number],
5
- R: [number[], number],
6
- isNegative: boolean,
7
- initLimit: number
8
- ) => InputData;
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
@@ -1,7 +0,0 @@
1
- import type { InputData } from "../types.ts";
2
-
3
- export type Multiplication = (
4
- arrL: [number[], number],
5
- arrR: [number[], number],
6
- isNegative: boolean
7
- ) => InputData;
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
- };
@@ -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;
@@ -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
@@ -1,6 +0,0 @@
1
- import type { InputData } from "../types.ts";
2
-
3
- export type Subtract = (
4
- L: [number[], number],
5
- R: [number[], number],
6
- ) => InputData;
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
@@ -1,6 +0,0 @@
1
- export type InputData = {
2
- array: number[];
3
- intLength: number;
4
- isNegative: boolean;
5
- isFloat: boolean
6
- }
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES5",
4
- "module": "ES2022",
5
- "outDir": "npm",
6
- "declaration": true,
7
- },
8
- "exclude": ["./build.ts", "**/*.test.ts", "npm", "tsconfig.json"]
9
- }
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