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

ruby-changes:63445

From: Kenta <ko1@a...>
Date: Mon, 26 Oct 2020 18:43:51 +0900 (JST)
Subject: [ruby-changes:63445] 69837229d7 (master): rational.c: convert a numerator to rational before calling fdiv in Kernel.Rational() (#3702)

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

From 69837229d74813c807e9d079aaee33aec2c7a4d0 Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@u...>
Date: Mon, 26 Oct 2020 18:43:30 +0900
Subject: rational.c: convert a numerator to rational before calling fdiv in
 Kernel.Rational() (#3702)

This makes `Rational(BigDecimal(1), 60) == Rational(1, 60)`.
[Bug #16518]

diff --git a/rational.c b/rational.c
index 3176870..abf3331 100644
--- a/rational.c
+++ b/rational.c
@@ -2673,8 +2673,16 @@ nurat_convert(VALUE klass, VALUE numv, VALUE denv, int raise) https://github.com/ruby/ruby/blob/trunk/rational.c#L2673
             }
         }
         if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
-                (!f_integer_p(a1) || !f_integer_p(a2)))
+                (!f_integer_p(a1) || !f_integer_p(a2))) {
+            VALUE tmp = rb_protect(to_rational, a1, &state);
+            if (!state) {
+                a1 = tmp;
+            }
+            else {
+                rb_set_errinfo(Qnil);
+            }
             return f_div(a1, a2);
+        }
     }
 
     a1 = nurat_int_value(a1);
diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb
index 5bdf5b7..e89b74d 100644
--- a/test/ruby/test_rational.rb
+++ b/test/ruby/test_rational.rb
@@ -165,6 +165,14 @@ class Rational_Test < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rational.rb#L165
     if (1.0/0).infinite?
       assert_raise(FloatDomainError){Rational(1.0/0)}
     end
+
+    bug16518 = "[ruby-core:96942] [Bug #16518]"
+    cls = Class.new(Numeric) do
+      def /(y); 42; end
+      def to_r; 1r; end
+      def to_int; 1; end
+    end
+    assert_equal(1/2r, Rational(cls.new, 2))
   end
 
   def test_attr
-- 
cgit v0.10.2


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

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