From 54a9ea092ee2375ccef60de31593be7fe2dc2f60 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Wed, 18 Nov 2026 10:11:01 +0530 Subject: [PATCH] Fix div rounding when the first quotient word has leading zeros BigDecimal#div(value, digits) marks a nonzero remainder by bumping frac[idx], the word after the requested digits. idx was roomof(digits, BASE_FIG), which is only right when frac[0] has 9 digits. When it has fewer, frac[idx] still holds requested digits, so the remainder was lost and the result was treated as exact: BigDecimal.mode(BigDecimal::ROUND_MODE, :up) BigDecimal(6).div(BigDecimal('0.66'), 27) # => 0.909090909090909090909090909e1 (should end in 910) Count the leading zeros of frac[0] when picking idx. --- ext/bigdecimal/bigdecimal.c | 10 +++++++++- test/bigdecimal/test_bigdecimal.rb | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 2c28e13a..685c1cb1 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -1864,7 +1864,15 @@ BigDecimal_div2(VALUE self, VALUE b, VALUE n) // ROUND_UP cv = 0.1e0 with idx=10 will be: // 0.1e0 if remainder == 0 // 0.1000000001e0 if remainder != 0 - size_t idx = roomof(ix, BASE_FIG); + // frac[idx] must be the first word after the ix-th digit, so count + // the leading zeros of frac[0] too (9.0909... is 000000009_090909090_...). + size_t leading_zeros = BASE_FIG; + DECDIG v = cv.real->frac[0]; + while (v) { + v /= 10; + leading_zeros--; + } + size_t idx = roomof(ix + leading_zeros, BASE_FIG); while (cv.real->Prec <= idx) cv.real->frac[cv.real->Prec++] = 0; if (cv.real->frac[idx] == 0 || cv.real->frac[idx] == HALF_BASE) cv.real->frac[idx]++; } diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index fe92ecdf..425151b2 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -1210,6 +1210,16 @@ def test_div_rounding_with_small_remainder assert_equal(BigDecimal("0.100000000000000000000000000001e1"), BigDecimal("3.#{'0' * 40}1").div(3, 30)) assert_equal(BigDecimal("0.10000000000000000001e1"), BigDecimal("3.#{'0' * 40}1").div(3, 20)) assert_equal(BigDecimal("0.10000000000000000001e6"), BigDecimal("3.#{'0' * 40}1e5").div(3, 20)) + + # The first word of the quotient has leading zeros (9.0909... is stored as 000000009_090909090_...) + assert_equal(BigDecimal("0.909090909090909090909090910e1"), BigDecimal(6).div(BigDecimal("0.66"), 27)) + assert_equal(BigDecimal("-0.123750001e-78"), BigDecimal("-495e-18").div(BigDecimal("3#{'9' * 40}e23"), 9)) + + # 1.000000005 / 0.999...9 = 1.000000005000...0001000000005... + BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_DOWN) + assert_equal(BigDecimal("0.100000001e1"), BigDecimal("1.000000005").div(BigDecimal("0.#{'9' * 40}"), 9)) + BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_EVEN) + assert_equal(BigDecimal("0.100000001e1"), BigDecimal("1.000000005").div(BigDecimal("0.#{'9' * 40}"), 9)) end def test_div_with_float