[前][次][番号順一覧][スレッド一覧]

ruby-changes:70480

From: Kenta <ko1@a...>
Date: Fri, 24 Dec 2021 02:30:06 +0900 (JST)
Subject: [ruby-changes:70480] 75f552e973 (master): [ruby/bigdecimal] Fix the precision of the adjusted quotient

https://git.ruby-lang.org/ruby.git/commit/?id=75f552e973

From 75f552e973ba565ef8615a0c5fd375d3a052b82e Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Fri, 22 Jan 2021 13:50:26 +0900
Subject: [ruby/bigdecimal] Fix the precision of the adjusted quotient

https://github.com/ruby/bigdecimal/commit/8dc8cd339d
---
 ext/bigdecimal/bigdecimal.c        | 6 ++++--
 test/bigdecimal/test_bigdecimal.rb | 4 ++++
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 0558a17fd9c..1cbdbd88326 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) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L1630
     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 26d346b3646..784560d1d49 100644
--- a/test/bigdecimal/test_bigdecimal.rb
+++ b/test/bigdecimal/test_bigdecimal.rb
@@ -1048,6 +1048,10 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L1048
     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
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]