@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
@@ -0,0 +1,16 @@
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;
@@ -0,0 +1,130 @@
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
+ };
@@ -0,0 +1,35 @@
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
+ }
@@ -0,0 +1,6 @@
1
+ import type { InputData } from "../types.ts";
2
+
3
+ export type Subtract = (
4
+ L: [number[], number],
5
+ R: [number[], number],
6
+ ) => InputData;
@@ -0,0 +1,86 @@
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 ADDED
@@ -0,0 +1,6 @@
1
+ export type InputData = {
2
+ array: number[];
3
+ intLength: number;
4
+ isNegative: boolean;
5
+ isFloat: boolean
6
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
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
+ }