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

ruby-changes:54529

From: mrkn <ko1@a...>
Date: Mon, 7 Jan 2019 13:49:41 +0900 (JST)
Subject: [ruby-changes:54529] mrkn:r66744 (trunk): complex.c (f_divide): canonicalize a quotient

mrkn	2019-01-07 13:49:34 +0900 (Mon, 07 Jan 2019)

  New Revision: 66744

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66744

  Log:
    complex.c (f_divide): canonicalize a quotient
    
    Cannonicalize resultant real and imaginary parts when complex number
    divided by non-complex number.
    
    [Fix GH-2065] [Bug #15505] [ruby-core:90891]
    
    From: Joe Peric <peric.joe@g...>

  Modified files:
    trunk/complex.c
    trunk/test/ruby/test_complex.rb
Index: test/ruby/test_complex.rb
===================================================================
--- test/ruby/test_complex.rb	(revision 66743)
+++ test/ruby/test_complex.rb	(revision 66744)
@@ -427,11 +427,13 @@ class Complex_Test < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/ruby/test_complex.rb#L427
     assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
 
     c = Complex(1)
-    r = c / c
-    assert_instance_of(Complex, r)
-    assert_equal(1, r)
-    assert_predicate(r.real, :integer?)
-    assert_predicate(r.imag, :integer?)
+    [ 1, Rational(1), c ].each do |d|
+      r = c / d
+      assert_instance_of(Complex, r)
+      assert_equal(1, r)
+      assert_predicate(r.real, :integer?)
+      assert_predicate(r.imag, :integer?)
+    end
   end
 
   def test_quo
Index: complex.c
===================================================================
--- complex.c	(revision 66743)
+++ complex.c	(revision 66744)
@@ -843,10 +843,11 @@ f_divide(VALUE self, VALUE other, https://github.com/ruby/ruby/blob/trunk/complex.c#L843
     }
     if (k_numeric_p(other) && f_real_p(other)) {
 	get_dat1(self);
-
-	return f_complex_new2(CLASS_OF(self),
-			      (*func)(dat->real, other),
-			      (*func)(dat->imag, other));
+        return f_complex_new2(CLASS_OF(self),
+                              rb_rational_canonicalize(
+                                  (*func)(dat->real, other)),
+                              rb_rational_canonicalize(
+                                  (*func)(dat->imag, other)));
     }
     return rb_num_coerce_bin(self, other, id);
 }

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

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