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

ruby-changes:64899

From: Kenta <ko1@a...>
Date: Sat, 16 Jan 2021 00:15:03 +0900 (JST)
Subject: [ruby-changes:64899] b130644584 (master): [ruby/bigdecimal] Fix for the coerce cases in divide and DoDivmod

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

From b1306445842eee53e23fd932a85d252d183e63b5 Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Fri, 15 Jan 2021 10:44:45 +0900
Subject: [ruby/bigdecimal] Fix for the coerce cases in divide and DoDivmod

https://github.com/ruby/bigdecimal/commit/1cb92487f7

diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 8c79450..bcc2b32 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -1356,16 +1356,19 @@ BigDecimal_divide(VALUE self, VALUE r, Real **c, Real **res, Real **div) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L1356
     TypedData_Get_Struct(self, Real, &BigDecimal_data_type, a);
     SAVE(a);
 
-    VALUE rr = Qnil;
-    if (RB_TYPE_P(r, T_FLOAT)) {
+    VALUE rr = r;
+    if (is_kind_of_BigDecimal(rr)) {
+        /* do nothing */
+    }
+    else if (RB_INTEGER_TYPE_P(r)) {
+        rr = rb_inum_convert_to_BigDecimal(r, 0, true);
+    }
+    else if (RB_TYPE_P(r, T_FLOAT)) {
         rr = rb_float_convert_to_BigDecimal(r, 0, true);
     }
     else if (RB_TYPE_P(r, T_RATIONAL)) {
         rr = rb_rational_convert_to_BigDecimal(r, a->Prec*BASE_FIG, true);
     }
-    else {
-        rr = rb_convert_to_BigDecimal(r, 0, false);
-    }
 
     if (!is_kind_of_BigDecimal(rr)) {
         return DoSomeOne(self, r, '/');
@@ -1429,16 +1432,19 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L1432
     TypedData_Get_Struct(self, Real, &BigDecimal_data_type, a);
     SAVE(a);
 
-    VALUE rr = Qnil;
-    if (RB_TYPE_P(r, T_FLOAT)) {
+    VALUE rr = r;
+    if (is_kind_of_BigDecimal(rr)) {
+        /* do nothing */
+    }
+    else if (RB_INTEGER_TYPE_P(r)) {
+        rr = rb_inum_convert_to_BigDecimal(r, 0, true);
+    }
+    else if (RB_TYPE_P(r, T_FLOAT)) {
         rr = rb_float_convert_to_BigDecimal(r, 0, true);
     }
     else if (RB_TYPE_P(r, T_RATIONAL)) {
         rr = rb_rational_convert_to_BigDecimal(r, a->Prec*BASE_FIG, true);
     }
-    else {
-        rr = rb_convert_to_BigDecimal(r, 0, false);
-    }
 
     if (!is_kind_of_BigDecimal(rr)) {
         return Qfalse;
diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb
index 0063fb9..99f92c4 100644
--- a/test/bigdecimal/test_bigdecimal.rb
+++ b/test/bigdecimal/test_bigdecimal.rb
@@ -963,6 +963,15 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L963
     assert_kind_of(BigDecimal, BigDecimal("3") / 1.quo(3))
   end
 
+  def test_div_with_complex
+    q = BigDecimal("3") / 1i
+    assert_kind_of(Complex, q)
+  end
+
+  def test_div_error
+    assert_raise(TypeError) { BigDecimal(20) / '2' }
+  end
+
   def test_mod
     x = BigDecimal((2**100).to_s)
     assert_equal(1, x % 3)
@@ -1006,6 +1015,10 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L1015
     assert_raise(ZeroDivisionError){BigDecimal("0").divmod(0)}
   end
 
+  def test_divmod_error
+    assert_raise(TypeError) { BigDecimal(20).divmod('2') }
+  end
+
   def test_add_bigdecimal
     x = BigDecimal((2**100).to_s)
     assert_equal(3000000000000000000000000000000, x.add(x, 1))
-- 
cgit v0.10.2


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

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