-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastBigInt.js
241 lines (219 loc) · 6.99 KB
/
FastBigInt.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
export default class FastBigInt {
// Initialise a new instance of FastBigInt with a main number, an exponent and a remainder.
// For instance, you can define 145003 using either new FastBigInt(145, 1, 3) or new FastBigInt(0, 0, 145003)
// Usually you can just initialise using only the remainder, unless you need to initialise to a number bigger than Number.MAX_SAFE_INTEGER
constructor(main = 0, exponent = 0, remainder = 0) {
this.main = main;
this.exponent = exponent;
this.remainder = remainder;
this.readjust();
}
// You don't need to call this method, it is usually called automatically
readjust() {
this._readjust_balance();
this._readjust_exponent();
this._readjust_clamp();
}
_readjust_clamp() {
if (this.main < 0) {
this.main = 0;
this.remainder = 0;
this.exponent = 0;
}
if (this.main >= 1000 && this.exponent === FastBigInt.maxExponent) {
this.main = 999;
this.remainder = Math.pow(10, 3 * this.exponent) - 1;
}
}
_readjust_exponent() {
while (this.remainder < 0 && this.exponent > 0) {
this.decrementExponent();
}
while (this.main === 0 && this.exponent > 0) {
this.decrementExponent();
}
while (this.main >= 1000 && this.exponent < FastBigInt.maxExponent) {
this.incrementExponent();
}
if (this.exponent === 0) {
this.main += this.remainder;
this.remainder = 0;
}
while (this.remainder >= Math.pow(10, 3 * this.exponent)
&& this.exponent < FastBigInt.maxExponent) {
this.incrementExponent();
}
}
_readjust_balance() {
if (this.remainder >= Math.pow(10, 3 * this.exponent)) {
let remainder = this.remainder % Math.pow(10, this.exponent * 3);
let remToAddToMain = (this.remainder - remainder) / Math.pow(10, this.exponent * 3);
this.main += remToAddToMain;
this.remainder = remainder;
}
if (this.remainder < 0) {
let mainRem = this.main % 1000;
this.main = (this.main - mainRem)/1000;
mainRem = mainRem * Math.pow(10, this.exponent * 3);
this.remainder += mainRem;
}
}
// You usually don't need to call this, but basically it increments the exponent. So 100K becomes 0.1M
incrementExponent() {
if (this.exponent === FastBigInt.maxExponent)
return;
let main = this.main;
let mainRem = this.main % 1000;
this.main = (main - mainRem)/1000;
mainRem = mainRem * Math.pow(10, this.exponent * 3);
this.exponent++;
this.remainder += mainRem;
}
// You usually don't need to call this, but basically it decrements the exponent. So 0.1M becomes 100K
decrementExponent() {
if (this.exponent === 0)
return;
this.main = this.main * 1000;
this.exponent--;
let remainder = this.remainder % Math.pow(10, this.exponent * 3);
let remToAddToMain = (this.remainder - remainder) / Math.pow(10, this.exponent * 3);
this.main += remToAddToMain;
this.remainder = remainder;
}
// Addition. Can take a Number or another FastBigInt as an argument.
add(other) {
if (typeof other === "number") {
this.remainder += other;
}
else if (other instanceof FastBigInt) {
while (other.exponent < this.exponent) {
other.incrementExponent();
}
while (other.exponent > this.exponent) {
other.decrementExponent();
}
this.remainder += other.remainder;
this.main += other.main;
}
else {
throw new Error("Cannot add anything that is not a number of a FastBigInt");
}
this.readjust();
}
// Subtraction. Can take a Number or another FastBigInt as an argument.
subtract(other) {
if (typeof other === "number") {
this.remainder -= other;
}
else if (other instanceof FastBigInt) {
while (other.exponent < this.exponent) {
other.incrementExponent();
}
while (other.exponent > this.exponent) {
other.decrementExponent();
}
this.remainder -= other.remainder;
this.main -= other.main;
}
else {
throw new Error("Cannot subtract anything that is not a number of a FastBigInt");
}
this.readjust();
}
// Comparison. Compares the current value to either a Number or another FastBigInt. Returns 0 if equal, 1 if superior and -1 if inferior
compare(other) {
if (typeof other === "number") {
return this.compare(new FastBigInt(0, 0, other));
}
if (other instanceof FastBigInt) {
this.readjust();
other.readjust();
if (this.exponent > other.exponent) {
return 1;
}
if (this.exponent < other.exponent) {
return -1;
}
if (this.main > other.main) {
return 1;
}
if (this.main < other.main) {
return -1;
}
if (this.remainder > other.remainder) {
return 1;
}
if (this.remainder < other.remainder) {
return -1;
}
return 0;
}
else {
throw new Error("Cannot subtract anything that is not a number of a FastBigInt");
}
}
// Set the value. Can take a Number or another FastBigInt
setValue(other) {
if (typeof other === "number") {
this.remainder = other;
this.main = 0;
this.exponent = 0;
}
else if (other instanceof FastBigInt) {
this.remainder = other.remainder;
this.main = other.main;
this.exponent = other.exponent;
}
else {
throw new Error("Cannot set value to anything that is not a number of a FastBigInt");
}
this.readjust();
}
// Formats the remainder to a string with all the leading zeroes necessary for it to print properly
// Aka (new FastBigInt(1, 2, 3)).formatedRemainder returns '000003'
get formatedRemainder() {
let res = this.remainder.toString().padStart(this.exponent * 3, '0');
return res.slice(Math.max(0, res.length - this.exponent * 3), res.length);
}
// Stringifies the number to the shortened value. (new FastBigInt(1, 1, 234)).toString() returns 1.23K
toString() {
let nb = this.main.toString();
let rem = this.formatedRemainder;
if (nb.length == 2 && rem.length > 0 && rem[0] != '0') nb += "," + rem.toString()[0]
if (nb.length == 1 && rem.length > 0 && (rem[0] != '0' || rem[1] != '0')) nb += "," + rem.toString()[0] + rem.toString()[1]
return `${nb}${FastBigInt.exponentShorthands[this.exponent]}`
}
// Stringifies the number to the full value. (new FastBigInt(1, 1, 234)).toString() returns 1234
fullNumber() {
return `${this.main}${this.formatedRemainder}`
}
// Saves the state of the FastBigInt object to a JSON object
saveToJson() {
return {
main: this.main,
exponent: this.exponent,
remainder: this.remainder
}
}
// Loads the state of the FastBigInt object from a JSON object
loadFromJson(o) {
this.main = o.main;
this.exponent = o.exponent;
this.remainder = o.remainder;
}
}
const exponents = Object.freeze(['', 'K', 'M', 'B', 't', 'q', 'Q', 's', 'S', 'o', 'n', 'd', 'U', 'D', 'T', 'Qt', 'Qd', 'Sd', 'St', 'O', 'N', 'v', 'c']);
// The maximum exponent the FastBigInt class supports. You can extend this when needed
Object.defineProperty(FastBigInt, 'maxExponent', {
value: exponents.length - 1,
writable : false,
enumerable : true,
configurable : false
});
// The exponent shorthands the FastBigInt class supports. You can extend this when needed
Object.defineProperty(FastBigInt, 'exponentShorthands', {
value: exponents,
writable : false,
enumerable : true,
configurable : false
});