--- a/decimal.js +++ b/decimal.js @@ -2786,6 +2786,26 @@ yd = yd[0]; sd++; + // Check for a repeating fraction only when many quotient limbs remain. + if (base == BASE && sd - xL >= 24) { + // Four fraction limbs suffice for any terminating divisor below BASE. + for (; (i < xL || k) && i < xL + 4; i++) { + t = k * base + (xd[i] || 0); + qd[i] = t / yd | 0; + k = t - qd[i] * yd; + } + + if (k) { + t = k * base; + rem0 = t / yd | 0; + // An unchanged remainder proves every subsequent limb is rem0. + if (t - rem0 * yd == k) { + for (; i < sd; i++) qd[i] = rem0; + } + } + sd -= i; + } + // k is the carry. for (; (i < xL || k) && sd--; i++) { t = k * base + (xd[i] || 0); --- a/decimal.mjs +++ b/decimal.mjs @@ -2782,6 +2782,26 @@ yd = yd[0]; sd++; + // Check for a repeating fraction only when many quotient limbs remain. + if (base == BASE && sd - xL >= 24) { + // Four fraction limbs suffice for any terminating divisor below BASE. + for (; (i < xL || k) && i < xL + 4; i++) { + t = k * base + (xd[i] || 0); + qd[i] = t / yd | 0; + k = t - qd[i] * yd; + } + + if (k) { + t = k * base; + rem0 = t / yd | 0; + // An unchanged remainder proves every subsequent limb is rem0. + if (t - rem0 * yd == k) { + for (; i < sd; i++) qd[i] = rem0; + } + } + sd -= i; + } + // k is the carry. for (; (i < xL || k) && sd--; i++) { t = k * base + (xd[i] || 0); --- a/test/modules/div.js +++ b/test/modules/div.js @@ -1070,4 +1070,60 @@ } })(); + // Fixed remainders, a finite prefix before repetition, and a longer-period control. + // Expected digits come from the displayed expansions, independently of division. + (function () { + var a, b, c, i, j, mode, p, sign, text, next, up, + precisions = [153, 154, 155, 161, 200], + cases = [ + [1, 3, '', '3'], + [2, 3, '', '6'], + [5, 9, '', '5'], + [1, 6, '1', '6'], + [5, 12, '41', '6'], + [7, 21, '', '3'], + [1, 7, '', '142857'], + [9999998, 9999999, '', '9999998'] + ], + D = Decimal.clone({ toExpNeg: -1000, toExpPos: 1000 }); + + for (p = 0; p < precisions.length; p++) { + D.set({ precision: precisions[p] }); + for (mode = 0; mode < 9; mode++) { + D.set({ rounding: mode }); + for (sign = -1; sign <= 1; sign += 2) { + for (i = 0; i < cases.length; i++) { + c = cases[i]; + text = c[2]; + while (text.length <= precisions[p]) text += c[3]; + next = +text.charAt(precisions[p]); + text = text.slice(0, precisions[p]); + // These expansions have a nonzero infinite tail, including after a 5. + up = mode == 0 || mode == 2 && sign > 0 || mode == 3 && sign < 0 || + mode >= 4 && next >= 5; + if (up) { + a = text.split(''); + for (j = a.length; j-- && a[j] == '9';) a[j] = '0'; + a[j] = +a[j] + 1; + text = a.join(''); + } + text = (sign < 0 ? '-' : '') + '0.' + text.replace(/0+$/, ''); + T.assertEqual(text, new D(sign * c[0]).div(c[1]).valueOf()); + // Scale and reverse both signs without changing the coefficient expansion. + T.assertEqual(text, new D(-sign * c[0] + 'e-14').div(-c[1] + 'e-14').valueOf()); + } + + // Input limbs must all be consumed before assuming that the tail repeats. + a = new Array(precisions[p] - 7).join('6'); + up = mode == 0 || mode == 2 && sign > 0 || mode == 3 && sign < 0 || mode >= 4; + b = (sign < 0 ? '-' : '') + '3333333.' + a + (up ? '7' : '6'); + T.assertEqual(b, new D(sign * 10000001).div(3).valueOf()); + T.assertEqual((sign < 0 ? '-' : '') + '0.00000011920928955078125', + new D(sign).div(8388608).valueOf()); + T.assertEqual((sign < 0 ? '-' : '') + '0.625', new D(sign * 5).div(8).valueOf()); + } + } + } + })(); + });