Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 72x 72x 814x 437960x 437960x 437960x 437960x 814x 467129x 467129x 467129x 467129x 467129x 467129x 467129x 814x 814x 814x 814x 814x 814x 814x 814x 39976x 39976x 814x 814x 148742x 148742x 148742x 814x 3281x 3281x 3281x 3281x 6562x 3281x 3281x 3232x 3232x 3281x 92x 92x 92x 3232x 3281x 814x 72x | 'use strict';
module.exports = ({ decimal: { parseDecimal, divideDecimals, formatDecimal } }) => {
function gcd(a, b) {
while (b)
[a, b] = [b, a % b];
return a < 0n ? -a : a;
}
function fraction(n, d) {
if (!d)
throw new RangeError('Cannot divide by zero');
if (!n)
return { n: 0n, d: 1n };
const divisor = gcd(n, d) * (d < 0n ? -1n : 1n);
return { n: n / divisor, d: d / divisor };
}
const zero = fraction(0n, 1n);
const one = fraction(1n, 1n);
const neg = (a) => ({ n: -a.n, d: a.d });
const add = (a, b) => fraction(a.n * b.d + b.n * a.d, a.d * b.d);
const sub = (a, b) => add(a, neg(b));
const mul = (a, b) => fraction(a.n * b.n, a.d * b.d);
const div = (a, b) => fraction(a.n * b.d, a.d * b.n);
const cmp = (a, b) => {
const difference = a.n * b.d - b.n * a.d;
return difference < 0n ? -1 : Number(difference > 0n);
};
function parse(value) {
const decimal = parseDecimal(value);
return fraction(decimal.coefficient, 10n ** BigInt(decimal.scale));
}
function format(value) {
// Bounds may be repeating fractions. Keep those exact in diagnostics as well.
let denominator = value.d;
for (const factor of [2n, 5n])
while (denominator % factor === 0n)
denominator /= factor;
if (denominator !== 1n)
return `${value.n}/${value.d}`;
let scale = 0;
let power = 1n;
while (power % value.d) {
power *= 10n;
scale++;
}
return formatDecimal(divideDecimals({ coefficient: value.n, scale: 0 }, { coefficient: value.d, scale: 0 }, scale));
}
return { zero, one, fraction, neg, add, sub, mul, div, cmp, parse, format };
};
|