-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathInsaneInt.ts
More file actions
220 lines (181 loc) · 7.77 KB
/
InsaneInt.ts
File metadata and controls
220 lines (181 loc) · 7.77 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
export class InsaneInt {
startingECount: number;
coefficient: number;
exponent: number;
constructor(startingECount: number, coefficient: number, exponent: number) {
this.startingECount = startingECount;
this.coefficient = coefficient;
this.exponent = exponent;
}
static fromString (val: string | InsaneInt) {
if (val instanceof InsaneInt) {
return new InsaneInt(val.startingECount, val.coefficient, val.exponent);
}
console.log("Converting " + val + " to InsaneInt");
let startingECount: number;
let coefficient: number;
let exponent: number;
startingECount = 0;
while (val.startsWith('e')) {
startingECount += 1;
val = val.slice(1);
}
if (val.includes('e')) {
if (val.includes('#')) {
startingECount += Number(val.split('#')[1]);
val = val.split('#')[0];
}
[coefficient, exponent] = val.split('e').map(Number);
} else {
coefficient = Number(val);
exponent = 0;
}
return new InsaneInt(startingECount, coefficient, exponent);
}
toString() {
let result = "";
for (let i = 0; i < this.startingECount; i++) {
result += "e";
}
result += this.coefficient;
if (this.exponent != 0) {
result += "e" + this.exponent;
}
return result;
}
greaterThan(other: InsaneInt) {
if (this.startingECount != other.startingECount) {
return this.startingECount > other.startingECount;
}
if (this.exponent != other.exponent) {
return this.exponent > other.exponent;
}
return this.coefficient > other.coefficient;
}
equalTo(other: InsaneInt) {
return this.startingECount == other.startingECount && this.exponent == other.exponent && this.coefficient == other.coefficient;
}
lessThan(other: InsaneInt) {
return !this.equalTo(other) && !this.greaterThan(other);
}
isBalanced(): boolean {
// Wrong 0s
if (this.coefficient == 0 && (this.exponent != 0 || this.startingECount != 0)) return false;
if (this.exponent == 0 && this.startingECount != 0) return false;
// Improper balancing
if ((this.coefficient >= 10 && this.exponent > 0) || this.coefficient >= 100000000000) return false;
if (this.coefficient < 1 && this.exponent > 0) return false;
if (this.exponent > 0 && this.exponent < 1000000 && this.startingECount > 1) return false;
if (this.exponent >= 100000000000) return false;
// Decimals in exponents, or leading zeros
if (this.exponent % 1 != 0 || this.startingECount % 1 != 0) return false;
if (this.exponent > 0 && this.coefficient < 1) return false;
return true;
}
balance() {
while (!this.isBalanced()) {
if (this.coefficient === 0) {
this.exponent = 0;
this.startingECount = 0;
return;
}
// Ensure exponent is an integer
if (this.startingECount > 0 && this.exponent % 1 != 0) {
const prevECount = this.startingECount;
this.startingECount = Math.max(this.startingECount - 1, 0);
this.exponent *= Math.pow(10, prevECount - this.startingECount);
}
if (this.exponent % 1 != 0) {
this.coefficient *= 1 / (this.exponent % 1);
this.exponent = Math.floor(this.exponent);
}
// Balance coefficient and exponent
if ((this.coefficient >= 10 && this.exponent > 0) || this.coefficient >= 100000000000) {
const change = Math.floor(this.coefficient).toString().length - 1;
this.coefficient /= Math.pow(10, change);
this.exponent += change;
}
if (this.coefficient < 1 && this.exponent > 0) {
let change = Math.ceil(Math.log10(1 / this.coefficient));
if (change >= this.exponent) change = this.exponent - 1;
this.coefficient *= Math.pow(10, change);
this.exponent -= change / Math.pow(10, this.startingECount);
}
// Balance exponent and startingECount
if ((this.exponent >= 100000000 && this.startingECount > 0) || this.exponent >= 100000000000) {
const change = Math.floor(this.exponent).toString().length - 5;
this.exponent /= Math.pow(10, change);
this.startingECount += change;
}
if (this.exponent != 0 && this.exponent < 100000000000 && this.startingECount != 0) {
let change = 5 - Math.floor(this.exponent).toString().length;
if (change >= this.startingECount) change = this.startingECount - 1;
this.exponent *= Math.pow(10, change);
this.startingECount -= change;
}
};
}
add(other: InsaneInt) {
// Balance the numbers
this.balance();
other.balance();
// Make the exponents the same
let startingECount;
let coefficient;
let exponent;
const myStartingECount = this.startingECount;
let myCoefficient = this.coefficient;
let myExponent = this.exponent;
const otherStartingECount = other.startingECount;
let otherCoefficient = other.coefficient;
let otherExponent = other.exponent;
if (myStartingECount > otherStartingECount) {
otherExponent = (otherExponent / Math.pow(10, (myStartingECount - otherStartingECount)));
startingECount = myStartingECount;
} else if (myStartingECount < otherStartingECount) {
myExponent = (myExponent / Math.pow(10, (otherStartingECount - myStartingECount)));
startingECount = otherStartingECount;
} else {
startingECount = myStartingECount;
}
if (myExponent > otherExponent) {
coefficient = (otherCoefficient / Math.pow(10, (myExponent - otherExponent))) + myCoefficient;
exponent = myExponent;
} else if (myExponent < otherExponent) {
coefficient = (myCoefficient / Math.pow(10, (otherExponent - myExponent))) + otherCoefficient;
exponent = otherExponent;
} else {
coefficient = myCoefficient + otherCoefficient;
exponent = myExponent
}
let result = new InsaneInt(startingECount, coefficient, exponent);
result.balance();
return result;
}
div(other: InsaneInt) {
// Balance the numbers
this.balance();
other.balance();
if (other.coefficient === 0) {
console.log("ERROR: Tried dividing by 0, returning 0");
return new InsaneInt(0, 0, 0);
};
console.log("Dividing " + this.toString() + " by " + other.toString());
let startingECount = this.startingECount;
let coefficient = this.coefficient;
let exponent = this.exponent;
if (startingECount > other.startingECount) {
exponent = exponent - (other.exponent / Math.pow(10, (startingECount - other.startingECount)));
startingECount = this.startingECount;
} else if (startingECount < other.startingECount) {
exponent = (exponent / Math.pow(10, (other.startingECount - startingECount))) - other.exponent;
startingECount = other.startingECount;
} else {
exponent -= other.exponent;
}
coefficient /= other.coefficient;
let result = new InsaneInt(this.startingECount, coefficient, exponent);
result.balance();
return result;
}
}