-
Notifications
You must be signed in to change notification settings - Fork 55
/
big_rational.js
457 lines (389 loc) · 12.3 KB
/
big_rational.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// big_rational.js
// Douglas Crockford
// 2018-11-05
// You can access the big rational object in your module by importing it.
// import big_rational from "./big_rational.js";
/*jslint bitwise */
/*property
abs, abs_lt, add, coefficient, create, dec, demoninator, denominator, div,
divrem, eq, exponent, fraction, freeze, gcd, inc, integer, isFinite,
isSafeInteger, is_big_integer, is_big_rational, is_integer, is_negative,
is_zero, length, lt, make, match, mul, neg, normalize, number, numerator,
padStart, power, reciprocal, remainder, sign, string, sub, ten, two, wun,
zero
*/
import big_integer from "./big_integer.js";
function is_big_rational(a) {
return (
typeof a === "object"
&& big_integer.is_big_integer(a.numerator)
&& big_integer.is_big_integer(a.denominator)
);
}
function is_integer(a) {
return (
big_integer.eq(big_integer.wun, a.denominator)
|| big_integer.is_zero(
big_integer.divrem(a.numerator, a.denominator)[1]
)
);
}
function is_negative(a) {
return big_integer.is_negative(a.numerator);
}
function make_big_rational(numerator, denominator) {
const new_big_rational = Object.create(null);
new_big_rational.numerator = numerator;
new_big_rational.denominator = denominator;
return Object.freeze(new_big_rational);
}
const zero = make_big_rational(big_integer.zero, big_integer.wun);
const wun = make_big_rational(big_integer.wun, big_integer.wun);
const two = make_big_rational(big_integer.two, big_integer.wun);
function normalize(a) {
// Normalize a big rational by dividing the two components by their greatest
// common divisor. If their gcd is '1', then the number was already normalized.
let {numerator, denominator} = a;
if (big_integer.eq(big_integer.wun, denominator)) {
return a;
}
let g_c_d = big_integer.gcd(numerator, denominator);
return (
big_integer.eq(big_integer.wun, g_c_d)
? a
: make(
big_integer.div(numerator, g_c_d),
big_integer.div(denominator, g_c_d)
)
);
}
function deconstruct(number) {
// This function deconstructs a number, reducing it to its components:
// a sign, an integer coefficient, and an exponent, such that
// ' number = sign * coefficient * (2 ** exponent)'
let sign = 1;
let coefficient = number;
let exponent = 0;
// Remove the sign from the coefficient.
if (coefficient < 0) {
coefficient = -coefficient;
sign = -1;
}
if (Number.isFinite(number) && number !== 0) {
// Reduce the coefficient: We can obtain the exponent by dividing the number by
// two until it goes to zero. We add the number of divisions to -1128, which is
// the exponent of 'Number.MIN_VALUE' minus the number of bits in the
// significand minus the bonus bit.
exponent = -1128;
let reduction = coefficient;
while (reduction !== 0) {
// This loop is guaranteed to reach zero. Each division will decrement the
// exponent of the reduction. When the exponent is so small that it can not
// be decremented, then the internal subnormal significand will be shifted
// right instead. Ultimately, all of the bits will be shifted out.
exponent += 1;
reduction /= 2;
}
// Reduce the exponent: When the exponent is zero, the number can be viewed
// as an integer. If the exponent is not zero, then adjust to correct the
// coefficient.
reduction = exponent;
while (reduction > 0) {
coefficient /= 2;
reduction -= 1;
}
while (reduction < 0) {
coefficient *= 2;
reduction += 1;
}
}
// Return an object containing the three components and the original number.
return {
sign,
coefficient,
exponent,
number
};
}
const number_pattern = /^(-?)(?:(\d+)(?:(?:\u0020(\d+))?\/(\d+)|(?:\.(\d*))?(?:e(-?\d+))?)|\.(\d+))$/;
function make(numerator, denominator) {
// If there are two arguments, both will be converted to big integers.
// The return value is an object containing the numerator and the
// denominator.
// If called with 1 argument, we will try to make sense of that
// argument. If the argument is a string, we will try to parse it
// as a mixed fraction or as a decimal literal. If the argument is
// a number, we will deconstruct it. Otherwise, we will assume that the
// missing argument was a 1.
if (denominator !== undefined) {
// Make a rational number from a numerator and a denominator. You may
// pass in big integers, integers, or strings.
numerator = big_integer.make(numerator);
// If the numerator is zero, we do not care about the denominator.
if (big_integer.zero === numerator) {
return zero;
}
denominator = big_integer.make(denominator);
if (
!big_integer.is_big_integer(numerator)
|| !big_integer.is_big_integer(denominator)
|| big_integer.zero === denominator
) {
return undefined;
}
// If the denominator is negative, move the sign to the numerator.
if (big_integer.is_negative(denominator)) {
numerator = big_integer.neg(numerator);
denominator = big_integer.abs(denominator);
}
return make_big_rational(numerator, denominator);
}
// Is the argument a string? If so, try to parse it.
if (typeof numerator === "string") {
let parts = numerator.match(number_pattern);
if (!parts) {
return undefined;
}
//. Capturing groups:
//. [1] sign
//. [2] integer
//. [3] top
//. [4] bottom
//. [5] frac
//. [6] exp
//. [7] naked frac
if (parts[7]) {
return make(
big_integer.make(parts[1] + parts[7]),
big_integer.power(big_integer.ten, parts[7].length)
);
}
if (parts[4]) {
let bottom = big_integer.make(parts[4]);
if (parts[3]) {
return make(
big_integer.add(
big_integer.mul(
big_integer.make(parts[1] + parts[2]),
bottom
),
big_integer.make(parts[3])
),
bottom
);
}
return make(parts[1] + parts[2], bottom);
}
let frac = parts[5] || "";
let exp = (Number(parts[6]) || 0) - frac.length;
if (exp < 0) {
return make(
parts[1] + parts[2] + frac,
big_integer.power(big_integer.ten, -exp)
);
}
return make(
big_integer.mul(
big_integer.make(parts[1] + parts[2] + parts[5]),
big_integer.power(big_integer.ten, exp)
),
big_integer.wun
);
}
// Is the argument a number? If so, deconstruct it and reconstruct it.
if (typeof numerator === "number" && !Number.isSafeInteger(numerator)) {
let {sign, coefficient, exponent} = deconstruct(numerator);
if (sign < 0) {
coefficient = -coefficient;
}
coefficient = big_integer.make(coefficient);
if (exponent >= 0) {
return make(
big_integer.mul(
coefficient,
big_integer.power(big_integer.two, exponent)
),
big_integer.wun
);
}
return normalize(make(
coefficient,
big_integer.power(big_integer.two, -exponent)
));
}
return make(numerator, big_integer.wun);
}
function neg(a) {
return make(big_integer.neg(a.numerator), a.denominator);
}
function abs(a) {
return (
is_negative(a)
? neg(a)
: a
);
}
function inc(a) {
return make(
big_integer.add(a.numerator, a.denominator),
a.denominator
);
}
function dec(a) {
return make(
big_integer.sub(a.numerator, a.denominator),
a.denominator
);
}
function conform_op(op) {
return function (a, b) {
try {
if (big_integer.eq(a.denominator, b.denominator)) {
return make(
op(a.numerator, b.numerator),
a.denominator
);
}
return normalize(make(
op(
big_integer.mul(a.numerator, b.denominator),
big_integer.mul(b.numerator, a.denominator)
),
big_integer.mul(a.denominator, b.denominator)
));
} catch (ignore) {
}
};
}
const add = conform_op(big_integer.add);
const sub = conform_op(big_integer.sub);
function mul(multiplicand, multiplier) {
return make(
big_integer.mul(multiplicand.numerator, multiplier.numerator),
big_integer.mul(multiplicand.denominator, multiplier.denominator)
);
}
function div(a, b) {
return make(
big_integer.mul(a.numerator, b.denominator),
big_integer.mul(a.denominator, b.numerator)
);
}
function remainder(a, b) {
const quotient = div(normalize(a), normalize(b));
return make(
big_integer.divrem(quotient.numerator, quotient.denominator)[1]
);
}
function reciprocal(a) {
return make(a.denominator, a.numerator);
}
function integer(a) {
return (
a.denominator === wun
? a
: make(big_integer.div(a.numerator, a.denominator), big_integer.wun)
);
}
function fraction(a) {
return sub(a, integer(a));
}
function eq(comparahend, comparator) {
return (
comparahend === comparator
? true
: (
big_integer.eq(comparahend.denominator, comparator.denominator)
? big_integer.eq(comparahend.numerator, comparator.numerator)
: big_integer.eq(
big_integer.mul(comparahend.numerator, comparator.denominator),
big_integer.mul(comparator.numerator, comparahend.denominator)
)
)
);
}
function lt(comparahend, comparator) {
return (
is_negative(comparahend) !== is_negative(comparator)
? is_negative(comparator)
: is_negative(sub(comparahend, comparator))
);
}
function number(a) {
return big_integer.number(a.numerator) / big_integer.number(a.demoninator);
}
function string(a, nr_places) {
if (a === zero) {
return "0";
}
let {numerator, denominator} = normalize(a);
// Divide the numerator by the demominator.
// If there was no remainder, then we have our result.
let [quotient, remains] = big_integer.divrem(numerator, denominator);
let result = big_integer.string(quotient);
if (remains !== big_integer.zero) {
// If 'nr_places' was provided, then the result will be in the decimal
// format. We scale the remains by a power of ten and do an integer
// division. If the residue is not less than half the denominator,
// then round up.
remains = big_integer.abs(remains);
if (nr_places !== undefined) {
let [fractus, residue] = big_integer.divrem(
big_integer.mul(
remains,
big_integer.power(big_integer.ten, nr_places)
),
denominator
);
if (!big_integer.abs_lt(
big_integer.mul(residue, big_integer.two),
denominator
)) {
fractus = big_integer.add(fractus, big_integer.wun);
}
result += "." + big_integer.string(fractus).padStart(
big_integer.number(nr_places),
"0"
);
} else {
// The result will be in mixed fraction form.
result = (
(
result === "0"
? ""
: result + " "
)
+ big_integer.string(remains)
+ "/"
+ big_integer.string(denominator)
);
}
}
return result;
}
export default Object.freeze({
abs,
add,
dec,
div,
eq,
fraction,
inc,
integer,
is_big_rational,
is_integer,
is_negative,
lt,
make,
mul,
neg,
normalize,
number,
wun,
reciprocal,
remainder,
string,
sub,
two,
zero
});