diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 0558a17fd9..1cbdbd8832 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -1630,9 +1630,11 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod) VpAddSub(c, a, res, -1); if (!VpIsZero(c) && (VpGetSign(a) * VpGetSign(b) < 0)) { - /* remainder adjustment for negative case */ + /* result adjustment for negative case */ + res = VpReallocReal(res, d->MaxPrec); + res->MaxPrec = d->MaxPrec; VpAddSub(res, d, VpOne(), -1); - GUARD_OBJ(d, VpCreateRbObject(GetAddSubPrec(c, b)*(VpBaseFig() + 1), "0", true)); + GUARD_OBJ(d, VpCreateRbObject(GetAddSubPrec(c, b) * 2*BASE_FIG, "0", true)); VpAddSub(d, c, b, 1); *div = res; *mod = d; diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 26d346b364..784560d1d4 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -1048,6 +1048,10 @@ class TestBigDecimal < Test::Unit::TestCase b = BigDecimal('1.23456789e10') q, r = a.divmod(b) assert_equal((a/b), q) + + b = BigDecimal('-1.23456789e10') + q, r = a.divmod(b) + assert_equal((a/b), q) end def test_divmod_error