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

ruby-changes:7921

From: tadf <ko1@a...>
Date: Sun, 21 Sep 2008 10:30:44 +0900 (JST)
Subject: [ruby-changes:7921] Ruby:r19442 (trunk): * complex.c (nucomp_s_canonicalize_internal): checks exactness of

tadf	2008-09-21 10:30:25 +0900 (Sun, 21 Sep 2008)

  New Revision: 19442

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19442

  Log:
    * complex.c (nucomp_s_canonicalize_internal): checks exactness of
      imag only.
    
    * rational.c (nurat_s_convert): accepts non real value
      (Rational(a,b) as a/b).
    
    * complex.c (nucomp_s_convert): refined.

  Modified files:
    trunk/ChangeLog
    trunk/complex.c
    trunk/rational.c
    trunk/test/ruby/test_complex.rb
    trunk/test/ruby/test_rational.rb

Index: complex.c
===================================================================
--- complex.c	(revision 19441)
+++ complex.c	(revision 19442)
@@ -336,8 +336,7 @@
 {
 #define CL_CANON
 #ifdef CL_CANON
-    if (f_zero_p(imag) && f_unify_p(klass) &&
-	k_exact_p(real) && k_exact_p(imag))
+    if (f_zero_p(imag) && k_exact_p(imag) && f_unify_p(klass))
 	return real;
 #else
     if (f_zero_p(imag) && f_unify_p(klass))
@@ -657,7 +656,7 @@
 static VALUE
 nucomp_expt(VALUE self, VALUE other)
 {
-    if (f_zero_p(other))
+    if (k_exact_p(other) && f_zero_p(other))
 	return f_complex_new_bang1(CLASS_OF(self), ONE);
 
     if (k_rational_p(other) && f_one_p(f_denominator(other)))
@@ -1225,9 +1224,10 @@
 static VALUE
 nucomp_s_convert(int argc, VALUE *argv, VALUE klass)
 {
+    int c;
     VALUE a1, a2, backref;
 
-    rb_scan_args(argc, argv, "02", &a1, &a2);
+    c = rb_scan_args(argc, argv, "02", &a1, &a2);
 
     backref = rb_backref_get();
     rb_match_busy(backref);
@@ -1276,14 +1276,21 @@
 
     switch (TYPE(a1)) {
       case T_COMPLEX:
-	if (NIL_P(a2) || f_zero_p(a2))
+	if (c == 1 || (k_exact_p(a2) && f_zero_p(a2)))
 	    return a1;
     }
 
-    if ((k_numeric_p(a1) && !f_real_p(a1)) ||
-	(k_numeric_p(a2) && !f_real_p(a2)))
-	return f_add(a1,
-		     f_mul(a2, f_complex_new_bang2(rb_cComplex, ZERO, ONE)));
+    if (c == 1) {
+	if (k_numeric_p(a1) && !f_real_p(a1))
+	    return a1;
+    }
+    else {
+	if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
+	    (!f_real_p(a1) || !f_real_p(a2)))
+	    return f_add(a1,
+			 f_mul(a2,
+			       f_complex_new_bang2(rb_cComplex, ZERO, ONE)));
+    }
 
     {
 	VALUE argv2[2];
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 19441)
+++ ChangeLog	(revision 19442)
@@ -1,3 +1,13 @@
+Sun Sep 21 10:19:04 2008  Tadayoshi Funaba  <tadf@d...>
+
+	* complex.c (nucomp_s_canonicalize_internal): checks exactness of
+	  imag only.
+
+	* rational.c (nurat_s_convert): accepts non real value
+	  (Rational(a,b) as a/b).
+
+	* complex.c (nucomp_s_convert): refined.
+
 Sun Sep 21 09:37:57 2008  James Edward Gray II  <jeg2@r...>
 
   * lib/csv/csv.rb:  Reworked CSV's parser and generator to be m17n.  Data
Index: test/ruby/test_complex.rb
===================================================================
--- test/ruby/test_complex.rb	(revision 19441)
+++ test/ruby/test_complex.rb	(revision 19442)
@@ -4,6 +4,14 @@
 
 class Complex_Test < Test::Unit::TestCase
 
+  def setup
+    @rational = defined?(Rational)
+    if @rational
+      @keiju = Rational.instance_variable_get('@RCS_ID')
+    end
+    @unify = defined?(Complex::Unify)
+  end
+
   def test_compsub
     c = ComplexSub.__send__(:new, 1)
     cc = ComplexSub.__send__(:convert, 1)
@@ -11,7 +19,7 @@
     assert_kind_of(Numeric, c)
     assert_kind_of(Numeric, cc)
 
-    if defined?(ComplexSub::Unify)
+    if @unify
       assert_instance_of(Fixnum, c)
       assert_instance_of(Fixnum, cc)
     else
@@ -42,7 +50,7 @@
     assert_equal(true, c.eql?(c2))
     assert_equal(false, c.eql?(c3))
 
-    if defined?(Complex::Unify)
+    if @unify
       assert_equal(true, c.eql?(0))
     else
       assert_equal(false, c.eql?(0))
@@ -72,7 +80,7 @@
   def test_freeze
     c = Complex(1)
     c.freeze
-    unless defined?(Complex::Unify)
+    unless @unify
       assert_equal(true, c.frozen?)
     end
     assert_instance_of(String, c.to_s)
@@ -110,7 +118,7 @@
 
   def test_new
     assert_instance_of(Complex, Complex.__send__(:new, 2,0.0))
-    if defined?(Complex::Unify)
+    if @unify
       assert_instance_of(Fixnum, Complex.__send__(:new, 2,0))
     else
       assert_instance_of(Complex, Complex.__send__(:new, 2,0))
@@ -152,20 +160,24 @@
     assert_equal(Complex.__send__(:new, -2**32,-2**32), c)
     assert_equal([-2**32,-2**32], [c.real,c.imag])
 
-    c = Complex(Complex(1),0)
-    assert_equal(Complex.__send__(:new, 1,0), c)
+    c = Complex(Complex(1,2),2)
+    assert_equal(Complex.__send__(:new, 1,4), c)
 
-    c = Complex(0,Complex(1))
+    c = Complex(2,Complex(1,2))
     assert_equal(Complex.__send__(:new, 0,1), c)
 
-    c = Complex(Complex(1,1),Complex(1))
-    assert_equal(Complex.__send__(:new, 1,2), c)
+    c = Complex(Complex(1,2),Complex(1,2))
+    assert_equal(Complex.__send__(:new, -1,3), c)
 
     c = Complex::I
     assert_equal(Complex.__send__(:new, 0,1), c)
 
     assert_equal(Complex.__send__(:new, 1),Complex(1))
     assert_equal(Complex.__send__(:new, 1),Complex('1'))
+    assert_equal(Complex.__send__(:new, 3.0,3.0),Complex('3.0','3.0'))
+    if @rational && !@keiju
+      assert_equal(Complex.__send__(:new, 1,1),Complex('3/3','3/3'))
+    end
     assert_raise(ArgumentError){Complex(nil)}
     assert_raise(ArgumentError){Complex(Object.new)}
     assert_raise(ArgumentError){Complex()}
@@ -232,7 +244,7 @@
   def test_attr2
     c = Complex(1)
 
-    if defined?(Complex::Unify)
+    if @unify
 =begin
       assert_equal(true, c.finite?)
       assert_equal(false, c.infinite?)
@@ -345,7 +357,7 @@
     assert_equal(Complex(3,2), c + 2)
     assert_equal(Complex(3.0,2), c + 2.0)
 
-    if defined?(Rational)
+    if @rational
       assert_equal(Complex(Rational(3,1),Rational(2)), c + Rational(2))
       assert_equal(Complex(Rational(5,3),Rational(2)), c + Rational(2,3))
     end
@@ -360,7 +372,7 @@
     assert_equal(Complex(-1,2), c - 2)
     assert_equal(Complex(-1.0,2), c - 2.0)
 
-    if defined?(Rational)
+    if @rational
       assert_equal(Complex(Rational(-1,1),Rational(2)), c - Rational(2))
       assert_equal(Complex(Rational(1,3),Rational(2)), c - Rational(2,3))
     end
@@ -375,7 +387,7 @@
     assert_equal(Complex(2,4), c * 2)
     assert_equal(Complex(2.0,4.0), c * 2.0)
 
-    if defined?(Rational)
+    if @rational
       assert_equal(Complex(Rational(2,1),Rational(4)), c * Rational(2))
       assert_equal(Complex(Rational(2,3),Rational(4,3)), c * Rational(2,3))
     end
@@ -386,7 +398,7 @@
     c = Complex(1,2)
     c2 = Complex(2,3)
 
-    if defined?(Rational)
+    if @rational
       assert_equal(Complex(Rational(8,13),Rational(1,13)), c / c2)
     else
       r = c / c2
@@ -404,14 +416,14 @@
     c = Complex(1,2)
     c2 = Complex(2,3)
 
-    if defined?(Rational)
+    if @rational
       assert_equal(Complex(Rational(1,2),1), c / 2)
     else
       assert_equal(Complex(0.5,1.0), c / 2)
     end
     assert_equal(Complex(0.5,1.0), c / 2.0)
 
-    if defined?(Rational)
+    if @rational
       assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2))
       assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
     end
@@ -421,7 +433,7 @@
     c = Complex(1,2)
     c2 = Complex(2,3)
 
-    if defined?(Rational)
+    if @rational
       assert_equal(Complex(Rational(8,13),Rational(1,13)), c.quo(c2))
     else
       r = c.quo(c2)
@@ -439,14 +451,14 @@
     c = Complex(1,2)
     c2 = Complex(2,3)
 
-    if defined?(Rational)
+    if @rational
       assert_equal(Complex(Rational(1,2),1), c.quo(2))
     else
       assert_equal(Complex(0.5,1.0), c.quo(2))
     end
     assert_equal(Complex(0.5,1.0), c.quo(2.0))
 
-    if defined?(Rational)
+    if @rational
       assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2))
       assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
     end
@@ -483,7 +495,7 @@
     assert_in_delta(-0.179, r.imag, 0.001)
 
     assert_equal(Complex(-3,4), c ** 2)
-    if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
+    if @rational && !@keiju
       assert_equal(Complex(Rational(-3,25),Rational(-4,25)), c ** -2)
     else
       r = c ** -2
@@ -498,7 +510,7 @@
     assert_in_delta(-0.12, r.real, 0.001)
     assert_in_delta(-0.16, r.imag, 0.001)
 
-    if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
+    if @rational && !@keiju
       assert_equal(Complex(-3,4), c ** Rational(2))
 #=begin
       assert_equal(Complex(Rational(-3,25),Rational(-4,25)),
@@ -541,7 +553,7 @@
   end
 
   def test_unify
-    if defined?(Complex::Unify)
+    if @unify
       assert_instance_of(Fixnum, Complex(1,2) + Complex(-1,-2))
       assert_instance_of(Fixnum, Complex(1,2) - Complex(1,2))
       assert_instance_of(Fixnum, Complex(1,2) * 0)
@@ -602,7 +614,7 @@
     assert_equal('1.0-2.0i', Complex(1.0,-2.0).to_s)
     assert_equal('-1.0-2.0i', Complex(-1.0,-2.0).to_s)
 
-    if defined?(Rational) && !defined?(Complex::Unify) && !Rational.instance_variable_get('@RCS_ID')
+    if @rational && !@unify && !@keiju
       assert_equal('0+2/1i', Complex(0,Rational(2)).to_s)
       assert_equal('0-2/1i', Complex(0,Rational(-2)).to_s)
       assert_equal('1+2/1i', Complex(1,Rational(2)).to_s)
@@ -638,7 +650,7 @@
     assert_equal(9, c2.instance_variable_get(:@ivar))
     assert_instance_of(Complex, c2)
 
-    if defined?(Rational)
+    if @rational
       c = Complex(Rational(1,2),Rational(2,3))
 
       s = Marshal.dump(c)
@@ -773,7 +785,7 @@
     assert_raise(ArgumentError){ Complex('5+3i_')}
     assert_raise(ArgumentError){ Complex('5+3ix')}
 
-    if defined?(Rational) && defined?(''.to_r)
+    if @rational && defined?(''.to_r)
       assert_equal(Complex(Rational(1,5)), '1/5'.to_c)
       assert_equal(Complex(Rational(-1,5)), '-1/5'.to_c)
       assert_equal(Complex(Rational(1,5),3), '1/5+3i'.to_c)
@@ -839,11 +851,11 @@
   end
 
   def test_to_r
-    if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
+    if @rational && !@keiju
       assert_equal(Rational(3), Complex(3).to_r)
       assert_equal(Rational(3), Rational(Complex(3)))
       assert_raise(RangeError){Complex(3,2).to_r}
-      assert_raise(RangeError){Rational(Complex(3,2))}
+#      assert_raise(RangeError){Rational(Complex(3,2))}
     end
   end
 
@@ -860,7 +872,7 @@
     c = 1.1.to_c
     assert_equal([1.1, 0], [c.real, c.imag])
 
-    if defined?(Rational)
+    if @rational
       c = Rational(1,2).to_c
       assert_equal([Rational(1,2), 0], [c.real, c.imag])
     end
@@ -933,14 +945,14 @@
     assert_equal(1.1, 1.1.conj)
     assert_equal(-1.1, -1.1.conj)
 
-    if defined?(Rational)
+    if @rational
       assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
     else
       assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2))
     end
 
 =begin
-    if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
+    if @rational && !@keiju
       assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
     end
 =end
@@ -948,7 +960,7 @@
     assert_equal(0.5, 1.fdiv(2))
     assert_equal(5000000000.0, 10000000000.fdiv(2))
     assert_equal(0.5, 1.0.fdiv(2))
-    if defined?(Rational)
+    if @rational
       assert_equal(0.25, Rational(1,2).fdiv(2))
     end
     assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2))
@@ -958,7 +970,7 @@
 #      assert_equal(true, Math.sqrt(-4.0).inexact?)
       assert_equal(Complex(0,2), Math.sqrt(-4))
 #      assert_equal(true, Math.sqrt(-4).exact?)
-      if defined?(Rational)
+      if @rational
 	assert_equal(Complex(0,2), Math.sqrt(Rational(-4)))
 #	assert_equal(true, Math.sqrt(Rational(-4)).exact?)
       end
@@ -967,7 +979,7 @@
 #      assert_equal(true, Math.sqrt(-9.0).inexact?)
       assert_equal(Complex(0,3), Math.sqrt(-9))
 #      assert_equal(true, Math.sqrt(-9).exact?)
-      if defined?(Rational)
+      if @rational
 	assert_equal(Complex(0,3), Math.sqrt(Rational(-9)))
 #	assert_equal(true, Math.sqrt(Rational(-9)).exact?)
       end
@@ -1070,7 +1082,7 @@
   end
 
   def test_fixed_bug
-    if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
+    if @rational && !@keiju
       assert_equal(Complex(1), 1 ** Complex(1))
     end
     assert_equal('-1.0-0.0i', Complex(-1.0, -0.0).to_s)
Index: test/ruby/test_rational.rb
===================================================================
--- test/ruby/test_rational.rb	(revision 19441)
+++ test/ruby/test_rational.rb	(revision 19442)
@@ -4,6 +4,14 @@
 
 class Rational_Test < Test::Unit::TestCase
 
+  def setup
+    @complex = defined?(Complex)
+    if @complex
+      @keiju = Complex.instance_variable_get('@RCS_ID')
+    end
+    @unify = defined?(Rational::Unify)
+  end
+
   def test_ratsub
     c = RationalSub.__send__(:new, 1)
     cc = RationalSub.__send__(:convert, 1)
@@ -11,7 +19,7 @@
     assert_kind_of(Numeric, c)
     assert_kind_of(Numeric, cc)
 
-    if defined?(RationalSub::Unify)
+    if @unify
       assert_instance_of(Fixnum, c)
       assert_instance_of(Fixnum, cc)
     else
@@ -41,7 +49,7 @@
     assert_equal(true, c.eql?(c2))
     assert_equal(false, c.eql?(c3))
 
-    if defined?(Rational::Unify)
+    if @unify
       assert_equal(true, c.eql?(0))
     else
       assert_equal(false, c.eql?(0))
@@ -67,7 +75,7 @@
   def test_freeze
     c = Rational(1)
     c.freeze
-    unless defined?(Rational::Unify)
+    unless @unify
       assert_equal(true, c.frozen?)
     end
     assert_instance_of(String, c.to_s)
@@ -108,7 +116,7 @@
 
 =begin
   def test_reduce
-    if defined?(Rational::Unify)
+    if @unify
       assert_instance_of(Fixnum, Rational.__send__(:reduce, 2,1))
     else
       assert_instance_of(Rational, Rational.__send__(:reduce, 2,1))
@@ -138,7 +146,7 @@
 =end
 
   def test_new
-    if defined?(Rational::Unify)
+    if @unify
       assert_instance_of(Fixnum, Rational.__send__(:new, 2,1))
     else
       assert_instance_of(Rational, Rational.__send__(:new, 2,1))
@@ -164,7 +172,7 @@
     assert_raise(ArgumentError){Rational.__send__(:new, nil)}
 =begin
     assert_raise(ArgumentError){Rational.__send__(:new, Rational(1))}
-    if defined?(Complex)
+    if @complex
       assert_raise(ArgumentError){Rational.__send__(:new, Complex(1))}
     end
 =end
@@ -199,6 +207,17 @@
     c = Rational(Rational(1,2),Rational(1,2))
     assert_equal(Rational.__send__(:new, 1), c)
 
+    if @complex && !@keiju
+      c = Rational(Complex(1,2),2)
+      assert_equal(Complex.__send__(:new, Rational(1,2),1), c)
+
+      c = Rational(2,Complex(1,2))
+      assert_equal(Complex.__send__(:new, Rational(2,5),Rational(-4,5)), c)
+
+      c = Rational(Complex(1,2),Complex(1,2))
+      assert_equal(Rational.__send__(:new, 1), c)
+    end
+
     assert_equal(Rational.__send__(:new, 3),Rational(3))
     assert_equal(Rational.__send__(:new, 1),Rational(3,3))
     assert_equal(3.3.to_r,Rational(3.3))
@@ -248,7 +267,7 @@
   def test_attr2
     c = Rational(1)
 
-    if defined?(Rational::Unify)
+    if @unify
 =begin
       assert_equal(true, c.finite?)
       assert_equal(false, c.infinite?)
@@ -408,7 +427,7 @@
     assert_equal(-2, (-c).div(c2))
     assert_equal(1, (-c).div(-c2))
 
-    unless defined?(Rational::Unify)
+    unless @unify
       c = Rational(11)
       c2 = Rational(3)
 
@@ -443,7 +462,7 @@
     assert_equal(Rational(99,100), (-c).modulo(c2))
     assert_equal(Rational(-101,100), (-c).modulo(-c2))
 
-    unless defined?(Rational::Unify)
+    unless @unify
       c = Rational(11)
       c2 = Rational(3)
 
@@ -478,7 +497,7 @@
     assert_equal([-2, Rational(99,100)], (-c).divmod(c2))
     assert_equal([1, Rational(-101,100)], (-c).divmod(-c2))
 
-    unless defined?(Rational::Unify)
+    unless @unify
       c = Rational(11)
       c2 = Rational(3)
 
@@ -514,7 +533,7 @@
     assert_equal(-1, (-c).quot(c2))
     assert_equal(1, (-c).quot(-c2))
 
-    unless defined?(Rational::Unify)
+    unless @unify
       c = Rational(11)
       c2 = Rational(3)
 
@@ -550,7 +569,7 @@
     assert_equal(Rational(-101,100), (-c).remainder(c2))
     assert_equal(Rational(-101,100), (-c).remainder(-c2))
 
-    unless defined?(Rational::Unify)
+    unless @unify
       c = Rational(11)
       c2 = Rational(3)
 
@@ -586,7 +605,7 @@
     assert_equal([-1, Rational(-101,100)], (-c).quotrem(c2))
     assert_equal([1, Rational(-101,100)], (-c).quotrem(-c2))
 
-    unless defined?(Rational::Unify)
+    unless @unify
       c = Rational(11)
       c2 = Rational(3)
 
@@ -643,7 +662,7 @@
     # p ** p
     x = 2 ** Rational(2)
     assert_equal(Rational(4), x)
-    unless defined?(Rational::Unify)
+    unless @unify
       assert_instance_of(Rational, x)
     end
     assert_equal(4, x.numerator)
@@ -651,7 +670,7 @@
 
     x = Rational(2) ** 2
     assert_equal(Rational(4), x)
-    unless defined?(Rational::Unify)
+    unless @unify
       assert_instance_of(Rational, x)
     end
     assert_equal(4, x.numerator)
@@ -659,7 +678,7 @@
 
     x = Rational(2) ** Rational(2)
     assert_equal(Rational(4), x)
-    unless defined?(Rational::Unify)
+    unless @unify
       assert_instance_of(Rational, x)
     end
     assert_equal(4, x.numerator)
@@ -668,7 +687,7 @@
     # -p ** p
     x = (-2) ** Rational(2)
     assert_equal(Rational(4), x)
-    unless defined?(Rational::Unify)
+    unless @unify
       assert_instance_of(Rational, x)
     end
     assert_equal(4, x.numerator)
@@ -676,7 +695,7 @@
 
     x = Rational(-2) ** 2
     assert_equal(Rational(4), x)
-    unless defined?(Rational::Unify)
+    unless @unify
       assert_instance_of(Rational, x)
     end
     assert_equal(4, x.numerator)
@@ -684,7 +703,7 @@
 
     x = Rational(-2) ** Rational(2)
     assert_equal(Rational(4), x)
-    unless defined?(Rational::Unify)
+    unless @unify
       assert_instance_of(Rational, x)
     end
     assert_equal(4, x.numerator)
@@ -728,7 +747,7 @@
     assert_equal(1, x.numerator)
     assert_equal(4, x.denominator)
 
-    unless defined?(Rational::Unify) # maybe bug mathn
+    unless @unify # maybe bug mathn
       assert_raise(ZeroDivisionError){0 ** -1}
     end
   end
@@ -789,7 +808,7 @@
   end
 
   def test_unify
-    if defined?(Rational::Unify)
+    if @unify
       assert_instance_of(Fixnum, Rational(1,2) + Rational(1,2))
       assert_instance_of(Fixnum, Rational(1,2) - Rational(1,2))
       assert_instance_of(Fixnum, Rational(1,2) * 2)
@@ -803,7 +822,7 @@
   def test_math
     assert_equal(Rational(1,2), Rational(1,2).abs)
     assert_equal(Rational(1,2), Rational(-1,2).abs)
-    if defined?(Complex) && !Complex.instance_variable_get('@RCS_ID')
+    if @complex && !@keiju
       assert_equal(Rational(1,2), Rational(1,2).magnitude)
       assert_equal(Rational(1,2), Rational(-1,2).magnitude)
     end
@@ -833,7 +852,7 @@
     assert_instance_of(String, c.to_s)
     assert_equal('1/2', c.to_s)
 
-    if defined?(Rational::Unify)
+    if @unify
       assert_equal('0', Rational(0,2).to_s)
       assert_equal('0', Rational(0,-2).to_s)
     else
@@ -991,8 +1010,8 @@
   end
 
   def test_to_c
-    if defined?(Complex) && !Complex.instance_variable_get('@RCS_ID')
-      if defined?(Rational::Unify)
+    if @complex && !@keiju
+      if @unify
 	assert_equal(Rational(3,2), Rational(3,2).to_c)
 	assert_equal(Rational(3,2), Complex(Rational(3,2)))
       else
@@ -1019,8 +1038,8 @@
     c = Rational(1,2).to_r
     assert_equal([1,2] , [c.numerator, c.denominator])
 
-    if defined?(Complex)
-      if Complex.instance_variable_get('@RCS_ID')
+    if @complex
+      if @keiju
 	assert_raise(NoMethodError){Complex(1,2).to_r}
       else
 	assert_raise(RangeError){Complex(1,2).to_r}
@@ -1083,7 +1102,7 @@
   end
 
   def test_fixed_bug
-    if defined?(Rational::Unify)
+    if @unify
       assert_instance_of(Fixnum, Rational(1,2) ** 0) # mathn's bug
     end
 
Index: rational.c
===================================================================
--- rational.c	(revision 19441)
+++ rational.c	(revision 19442)
@@ -1400,28 +1400,21 @@
 static VALUE
 nurat_s_convert(int argc, VALUE *argv, VALUE klass)
 {
+    int c;
     VALUE a1, a2, backref;
 
-    rb_scan_args(argc, argv, "02", &a1, &a2);
+    c = rb_scan_args(argc, argv, "02", &a1, &a2);
 
     switch (TYPE(a1)) {
       case T_COMPLEX:
-	if (k_inexact_p(RCOMPLEX(a1)->imag) || !f_zero_p(RCOMPLEX(a1)->imag)) {
-	    VALUE s = f_to_s(a1);
-	    rb_raise(rb_eRangeError, "can't accept %s",
-		     StringValuePtr(s));
-	}
-	a1 = RCOMPLEX(a1)->real;
+	if (k_exact_p(RCOMPLEX(a1)->imag) && f_zero_p(RCOMPLEX(a1)->imag))
+	    a1 = RCOMPLEX(a1)->real;
     }
 
     switch (TYPE(a2)) {
       case T_COMPLEX:
-	if (k_inexact_p(RCOMPLEX(a2)->imag) || !f_zero_p(RCOMPLEX(a2)->imag)) {
-	    VALUE s = f_to_s(a2);
-	    rb_raise(rb_eRangeError, "can't accept %s",
-		     StringValuePtr(s));
-	}
-	a2 = RCOMPLEX(a2)->real;
+	if (k_exact_p(RCOMPLEX(a2)->imag) && f_zero_p(RCOMPLEX(a2)->imag))
+	    a2 = RCOMPLEX(a2)->real;
     }
 
     backref = rb_backref_get();
@@ -1455,15 +1448,19 @@
 
     switch (TYPE(a1)) {
       case T_RATIONAL:
-	if (NIL_P(a2) || f_zero_p(a2))
+	if (c == 1 || (k_exact_p(a2) && f_one_p(a2)))
 	    return a1;
-	return f_div(a1, a2);
     }
 
-    switch (TYPE(a2)) {
-      case T_RATIONAL:
-	return f_div(a1, a2);
+    if (c == 1) {
+	if (k_numeric_p(a1) && !f_integer_p(a1))
+	    return a1;
     }
+    else {
+	if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
+	    (!f_integer_p(a1) || !f_integer_p(a2)))
+	    return f_div(a1, a2);
+    }
 
     {
 	VALUE argv2[2];

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

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