-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringMath.ts
More file actions
184 lines (172 loc) · 6.23 KB
/
stringMath.ts
File metadata and controls
184 lines (172 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
export function add(a: string, b: string): string {
if (!isNegative(a) && isNegative(b))
return sub(a, flipSign(b));
if (isNegative(a) && !isNegative(b))
return sub(b, flipSign(a));
if (isNegative(a) && isNegative(b))
return "-" + add(flipSign(a), flipSign(b));
let u = 0n;
let i = 1;
let result = a;
if (result.length < b.length)
result = repeat("0", b.length - a.length) + result;
if (b.length < result.length)
b = repeat("0", result.length - b.length) + b;
while (i <= result.length) {
// console.log("u:", u);
// console.log("result:", result);
const addDigitResult = addDigit(result[result.length - i], b[b.length - i]);
// console.log("addDigitResult:", addDigitResult)
result = setCharAt(result, result.length - i, addDigitResult.dig.toString());
const uString = u.toString();
const addUResult = addDigit(result[result.length - i], uString[uString.length - 1]);
// console.log("addUResult:", addUResult);
result = setCharAt(result, result.length - i, addUResult.dig.toString());
u = uString.length > 1 ? BigInt(uString.substring(0, uString.length - 1)) : 0n;
u += BigInt(addDigitResult.u + addUResult.u);
i++;
}
if (u !== 0n)
result = u.toString() + result;
return removeLeadingZeroes(result, false);
}
export function sub(a: string, b: string): string {
if (isNegative(a) && !isNegative(b))
return "-" + add(flipSign(a), b);
if (!isNegative(a) && isNegative(b))
return add(a, flipSign(b));
if (isNegative(a) && isNegative(b))
return sub(flipSign(b), flipSign(a));
{
const comp = compare(a, b);
if (comp == 0)
return "0";
if (comp < 0)
return flipSign(sub(b, a));
}
let u = 0n;
let i = 1;
let result = a;
if (result.length < b.length)
result = repeat("0", b.length - a.length) + result;
if (b.length < result.length)
b = repeat("0", result.length - b.length) + b;
while (i <= result.length) {
// console.log("u:", u);
// console.log("result:", result);
const subDigitResult = subDigit(result[result.length - i], b[b.length - i]);
// console.log("subDigitResult:", subDigitResult)
result = setCharAt(result, result.length - i, subDigitResult.dig.toString());
const uString = u.toString();
const subUResult = subDigit(result[result.length - i], uString[uString.length - 1]);
// console.log("subUResult:", subUResult);
result = setCharAt(result, result.length - i, subUResult.dig.toString());
u = uString.length > 1 ? BigInt(uString.substring(0, uString.length - 1)) : 0n;
u += BigInt(subDigitResult.u + subUResult.u);
i++;
}
if (u !== 0n)
result = u.toString() + result + " ERROR";
return removeLeadingZeroes(result, false);
}
export function repeat(str: string, num: number): string {
let result = "";
for (let i = 0; i < num; i++) {
result += str;
}
return result;
}
export function addDigit(digA: string, digB: string): { dig: number, u: number } {
// console.log({ digA, digB });
const a = parseInt(digA);
const b = parseInt(digB);
const sum = a + b;
// console.log({ a, b });
return { dig: sum % 10, u: Math.floor(sum / 10) };
}
export function subDigit(digA: string, digB: string): { dig: number, u: number } {
// console.log({ digA, digB });
const a = parseInt(digA);
const b = parseInt(digB);
const diff = (a < b ? a + 10 : a) - b;
// console.log({ a, b, diff });
return { dig: diff, u: a < b ? 1 : 0 };
}
export function setCharAt(str: string, position: number, char: string): string {
return str.substring(0, position) + char + str.substring(position + 1);
}
/**
* Compares two numbers represented as strings
*
* @param a The first number
* @param b The second number
*
* @returns 0 if a and b are equal, 1 if a is larger than b and -1 if a is smaller than b
*/
export function compare(a: string, b: string): 1 | 0 | -1 {
// check sign
if (isNegative(a) && !isNegative(b)) {
return -1;
}
if (!isNegative(a) && isNegative(b)) {
return 1;
}
let changeSign = 1;
if (isNegative(a) && isNegative(b)) {
changeSign = -1;
}
const cleanA = removeLeadingZeroes(a, true);
const cleanB = removeLeadingZeroes(b, true);
if (cleanA === cleanB)
return 0;
if (cleanA.length > cleanB.length)
return (1 * changeSign) as 1 | 0 | -1;
if (cleanA.length < cleanB.length)
return (-1 * changeSign) as 1 | 0 | -1;
// a and b are of equal length, compare digits, starting with the most significant one
for (let i = 0; i < a.length; i++) {
const compareResult = compareDigit(cleanA[i], cleanB[i]);
if (compareResult !== 0) {
return (compareResult * changeSign) as 1 | -1;
}
}
// a and b must be equal, but that hasn't been detected before (if you reach this part, panic)
console.error("a and b must be equal, but this hasn't been detected before. PANIK");
return 0;
}
export function compareDigit(digA: string, digB: string): 1 | 0 | -1 {
if (digA === digB)
return 0;
if (parseInt(digA) > parseInt(digB))
return 1;
return -1;
}
export function removeLeadingZeroes(str: string, removeSign: boolean): string {
str = str.trim();
for (let i = 0; i < str.length; i++) {
if (str[i] !== "0" && (!removeSign || str[i] !== "-")) {
if (removeSign)
return str.substring(i);
const shortened = str.substring(i);
if (shortened.startsWith("-"))
return "-" + removeLeadingZeroes(shortened, true);
return shortened;
}
}
return "0";
}
export function isNegative(num: string): boolean {
return num.trim().startsWith("-");
}
export function removeSign(num: string): string {
num = num.trim();
if (num.startsWith("-"))
return num.substring(1);
return num;
}
export function flipSign(num: string): string {
num = num.trim();
if (num.startsWith("-"))
return removeLeadingZeroes(num, true);
return "-" + removeLeadingZeroes(num, false);
}