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

ruby-changes:50547

From: nobu <ko1@a...>
Date: Wed, 7 Mar 2018 20:54:12 +0900 (JST)
Subject: [ruby-changes:50547] nobu:r62688 (trunk): complex.c: check type

nobu	2018-03-07 20:54:07 +0900 (Wed, 07 Mar 2018)

  New Revision: 62688

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

  Log:
    complex.c: check type
    
    * complex.c (nucomp_s_canonicalize_internal): determine the type
      by the internal type, not by a method.

  Modified files:
    trunk/complex.c
    trunk/test/ruby/test_complex.rb
Index: test/ruby/test_complex.rb
===================================================================
--- test/ruby/test_complex.rb	(revision 62687)
+++ test/ruby/test_complex.rb	(revision 62688)
@@ -958,4 +958,16 @@ class Complex_Test < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/ruby/test_complex.rb#L958
   def test_known_bug
   end
 
+  def test_canonicalize_internal
+    obj = Class.new(Numeric) do
+      attr_accessor :real
+      alias real? real
+    end.new
+    obj.real = true
+    c = Complex.rect(obj, 1);
+    obj.real = false
+    c = c.conj
+    assert_equal(obj, c.real)
+    assert_equal(-1, c.imag)
+  end
 end
Index: complex.c
===================================================================
--- complex.c	(revision 62687)
+++ complex.c	(revision 62688)
@@ -354,20 +354,24 @@ nucomp_real_check(VALUE num) https://github.com/ruby/ruby/blob/trunk/complex.c#L354
 inline static VALUE
 nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE imag)
 {
+    int complex_r, complex_i;
 #ifdef CANONICALIZATION_FOR_MATHN
     if (k_exact_zero_p(imag) && canonicalization)
 	return real;
 #endif
-    if (f_real_p(real) && f_real_p(imag))
+    complex_r = RB_TYPE_P(real, T_COMPLEX);
+    complex_i = RB_TYPE_P(imag, T_COMPLEX);
+    if (!complex_r && !complex_i) {
 	return nucomp_s_new_internal(klass, real, imag);
-    else if (f_real_p(real)) {
+    }
+    else if (!complex_r) {
 	get_dat1(imag);
 
 	return nucomp_s_new_internal(klass,
 				     f_sub(real, dat->imag),
 				     f_add(ZERO, dat->real));
     }
-    else if (f_real_p(imag)) {
+    else if (!complex_i) {
 	get_dat1(real);
 
 	return nucomp_s_new_internal(klass,

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

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