[ruby/bigdecimal] Fix the precision of the adjusted quotient

https://github.com/ruby/bigdecimal/commit/8dc8cd339d
This commit is contained in:
Kenta Murata 2021-01-22 13:50:26 +09:00
parent 7b2cfce543
commit 75f552e973
No known key found for this signature in database
GPG Key ID: CEFE8AFB6081B062
2 changed files with 8 additions and 2 deletions

View File

@ -1630,9 +1630,11 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod)
VpAddSub(c, a, res, -1); VpAddSub(c, a, res, -1);
if (!VpIsZero(c) && (VpGetSign(a) * VpGetSign(b) < 0)) { 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); 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); VpAddSub(d, c, b, 1);
*div = res; *div = res;
*mod = d; *mod = d;

View File

@ -1048,6 +1048,10 @@ class TestBigDecimal < Test::Unit::TestCase
b = BigDecimal('1.23456789e10') b = BigDecimal('1.23456789e10')
q, r = a.divmod(b) q, r = a.divmod(b)
assert_equal((a/b), q) assert_equal((a/b), q)
b = BigDecimal('-1.23456789e10')
q, r = a.divmod(b)
assert_equal((a/b), q)
end end
def test_divmod_error def test_divmod_error