-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbignum-native.js
347 lines (286 loc) · 7.19 KB
/
bignum-native.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
class BigNum {
constructor(a, options) {
options = options || {};
if(a === null || typeof(a) === 'undefined') {
// Initialize value of 0
this.i = 0n;
this.e = 0;
return;
}
var aStr = typeof(a) === 'string' ? a : a.toString();
var result = BigNum._numberRegex.exec(a.toString());
if(result) {
//console.log(result);
// Construct from string if possible
//this.i = parseInt(result[1] + result[2] + result[4]);
if(typeof(result[2]) === 'undefined') result[2] = '';
if(typeof(result[4]) === 'undefined') result[4] = '';
this.i = BigInt(result[1] + result[2] + result[4]);
this.e = result[4] ? -result[4].length : 0;
this._normalize();
}
else {
// Conversion to string failed, so construct from number
var m = 1n;
if(a < 0) {
a = -a;
m = -1n;
}
if (a === 0) {
this.i = 0;
this.e = 0;
}
else {
// Multiply the number by 10 until it is an integer
var e = -Math.floor(Math.log10(a));
if(e > 0) {
a *= Math.pow(10, e);
}
else {
e = 0;
}
while(a != Math.round(a)) {
e++;
a *= 10;
}
// Divide the number by 10 until the one's digit is non-zero
while(a % 10 === 0 && a > 0) {
e--;
a /= 10;
}
a = BigInt(a);
this.i = a*m;
this.e = -e;
}
}
}
/**
* Returns a new BigNum with the opposite sign of a.
* @param {BigNum} a
* @returns {BigNum}
*/
static minus(a) {
var negated = new BigNum();
negated.i = -a.i;
negated.e = a.e;
return negated;
}
/**
* Returns a new BigNum that has the opposite sign of this BigNum.
* @returns {BigNum}
*/
minus() {
return BigNum.minus(this);
}
/**
* Returns a new BigNum that is the sum of a and b.
* @param {BigNum} a
* @param {BigNum} b
* @returns {BigNum}
*/
static add(a, b) {
// a+b = a.i * 10^a.e + b.i * 10^b.e
// a+b = ( a.i * 10^(a.e-b.e) + b.i ) * 10^b.e
var a_plus_b;
// Order arguments so that a.e >= b.e
if(a.e < b.e) {
var c = a;
a = b;
b = c;
}
var a_tens = BigNum.multiplyByTens(a.i, a.e - b.e);
a_plus_b = new BigNum();
a_plus_b.i = a_tens + b.i;
a_plus_b.e = b.e;
a_plus_b._normalize();
return a_plus_b;
}
/**
* Returns a new BigNum that is the sum of this and b.
* @param {BigNum} b
* @returns {BigNum}
*/
add(b) {
return BigNum.add(this, b);
}
/**
* Subtracts b from a.
* @param {BigNum} a
* @param {BigNum} b
* @returns {BigNum}
*/
static subtract(a, b) {
return BigNum.add(a, BigNum.minus(b));
}
/**
* Returns a new BigNum that is the difference between this and b.
* @param {BigNum} b
*/
subtract(b) {
return BigNum.subtract(this, b);
}
/**
* Returns a new BigNum that is the product of a and b
* @param {BigNum} a
* @param {BigNum} b
* @returns {BigNum}
*/
static multiply(a, b) {
// a * b = (a.i * b.i) * 10^(a.e + b.e)
var a_times_b = new BigNum();
a_times_b.i = a.i * b.i;
a_times_b.e = a.e + b.e;
a_times_b._normalize();
return a_times_b;
}
/**
* Returns a new BigNum that is the product of this and b
* @param {BigNum} b
*/
multiply(b) {
return BigNum.multiply(this, b);
}
/**
* Returns a new BigNum that is the quotient of a and b
* @param {BigNum} a
* @param {BigNum} b
* @returns {BigNum}
*/
static divide(a, b) {
// We need to increase the number of digits of a and b so that a / b will have the desired precision.
// In order for the integer quotient to have the correct precision, a.e - b.e must be greater than that precision.
// So we need only to increase a.e.
// TODO: Revisit the value of increaseA. Is it correct in every circumstance?
var increaseA = Math.max(BigNum.precision - (a.e + b.e), 0);
var a_increased = new BigNum();
a_increased.i = BigNum.multiplyByTens(a.i, increaseA);
var quotient = new BigNum();
quotient.i = a_increased.i / b.i;
quotient.e = a.e - b.e - increaseA;
// No need to truncate since we already increased the number of digits before dividing
quotient._normalize();
return quotient;
}
/**
* Returns a new BigNum that is the quotient of this and b
* @param {BigNum} b
*/
divide(b) {
return BigNum.divide(this, b);
}
/**
* Returns the square root of this BigNum.
*/
sqrt() {
BigNum._precision++;
// Let the guess value be 10^(e/2), where e is this.e
var x = new BigNum();
x.i = 1n;
x.e = Math.floor(this.e / 2);
var onehalf = new BigNum('0.5');
var xt = x.clone();
xt._truncate();
xt._normalize();
var lastxt;
var i;
for(i=0; i<100; i++) {
lastxt = xt;
x = this.divide(x).add(x).multiply(onehalf);
xt = x.clone();
xt._truncate();
xt._normalize();
if(xt.i === lastxt.i && xt.e === lastxt.e)
break;
}
if(i>=100) {
console.log("Warning: sqrt exceeded maximum iterations. (Did it enter a cycle?)");
}
BigNum._precision--;
xt._truncate();
xt._normalize();
return xt;
}
/**
* Returns the square root of x.
* @param {BigNum} x
*/
static sqrt(x) {
return x.sqrt();
}
clone() {
var a = new BigNum();
a.i = this.i;
a.e = this.e;
return a;
}
/**
* Returns a BigInt that is equal to i times 10^e
* TODO: Make this better
* @param {BigInt} i
* @param {Number} e
* @returns {BigInt}
*/
static multiplyByTens(i, e) {
// The implementation is faster than 10n ** BigInt(e). But could we make it faster still?
var ii = i;
for(var j=0; j<e; j++) {
ii *= 10n;
}
return ii;
}
/**
* Remove zeroes in the least-significant digit of this BigNum
*/
_normalize() {
while(this.i % 10n === 0n && this.i !== 0n) {
this.i /= 10n;
this.e++;
}
}
/**
* Truncate this BigNum to the configured precision
* TODO: Make this better
* TODO: Rounding
*/
_truncate() {
var trunc = -this.e - BigNum._precision;
if(trunc > 0) {
this.i /= 10n ** BigInt(trunc-1);
this.e += trunc;
var nextDigit = this.i % 10n;
if(nextDigit >= 5) {
this.i += 10n;
}
this.i /= 10n;
}
// while(-this.e > BigNum._precision) {
// this.i /= 10n;
// this.e++;
// }
}
toString() {
var s = this.i.toString();
var m = '';
if(s[0] === '-') {
s = s.substring(1, s.length);
m = '-';
}
if(this.e > 0) {
return m + s + '0'.repeat(this.e);
}
else if(this.e < 0) {
if(-this.e >= s.length) {
return m + '0.' + '0'.repeat(-this.e-s.length) + s;
}
return m + s.slice(0, s.length+this.e) + '.' + s.slice(s.length+this.e, s.length);
}
else {
return s;
}
}
static get precision() { return BigNum._precision; }
static set precision(value) { BigNum._precision = value; }
}
BigNum._numberRegex = /^(-?)([0-9]+)(\.([0-9]+))?$/;
BigNum._precision = 30;
module.exports = BigNum;