summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenta Murata <[email protected]>2021-01-22 13:50:26 +0900
committerKenta Murata <[email protected]>2021-12-24 02:28:56 +0900
commit75f552e973ba565ef8615a0c5fd375d3a052b82e (patch)
tree6aadd84ddc6f903c1fc9566241d9eccaab387a58
parent7b2cfce543b876744544c8b43abdee3c72cab910 (diff)
[ruby/bigdecimal] Fix the precision of the adjusted quotient
https://github.com/ruby/bigdecimal/commit/8dc8cd339d
-rw-r--r--ext/bigdecimal/bigdecimal.c6
-rw-r--r--test/bigdecimal/test_bigdecimal.rb4
2 files changed, 8 insertions, 2 deletions
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