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

ruby-changes:16755

From: nobu <ko1@a...>
Date: Sun, 25 Jul 2010 05:57:26 +0900 (JST)
Subject: [ruby-changes:16755] Ruby:r28751 (trunk): * numeric.c (flo_cmp): honor the result of infinite? method of the

nobu	2010-07-25 05:37:31 +0900 (Sun, 25 Jul 2010)

  New Revision: 28751

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

  Log:
    * numeric.c (flo_cmp): honor the result of infinite? method of the
      other.  [ruby-core:31470]

  Modified files:
    trunk/ChangeLog
    trunk/numeric.c
    trunk/test/ruby/test_float.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 28750)
+++ ChangeLog	(revision 28751)
@@ -1,5 +1,8 @@
-Sun Jul 25 05:26:23 2010  Nobuyoshi Nakada  <nobu@r...>
+Sun Jul 25 05:37:20 2010  Nobuyoshi Nakada  <nobu@r...>
 
+	* numeric.c (flo_cmp): honor the result of infinite? method of the
+	  other.  [ruby-core:31470]
+
 	* test/ruby/envutil.rb (EnvUtil#.suppress_warning): added.
 
 	* test/ruby/test_float.rb (TestFloat#test_Float): suppress
Index: numeric.c
===================================================================
--- numeric.c	(revision 28750)
+++ numeric.c	(revision 28751)
@@ -1016,7 +1016,7 @@
 static VALUE
 flo_cmp(VALUE x, VALUE y)
 {
-    double a, b;
+    double a, b, i;
 
     a = RFLOAT_VALUE(x);
     if (isnan(a)) return Qnil;
@@ -1038,8 +1038,12 @@
 	break;
 
       default:
-	if (isinf(a) && (!rb_respond_to(y, rb_intern("infinite?")) ||
-			 !RTEST(rb_funcall(y, rb_intern("infinite?"), 0, 0)))) {
+	if (isinf(a) && (i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0)) != Qundef) {
+	    if (RTEST(i)) {
+		int j = rb_cmpint(i, x, y);
+		j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
+		return INT2FIX(j);
+	    }
 	    if (a > 0.0) return INT2FIX(1);
 	    return INT2FIX(-1);
 	}
Index: test/ruby/test_float.rb
===================================================================
--- test/ruby/test_float.rb	(revision 28750)
+++ test/ruby/test_float.rb	(revision 28751)
@@ -242,6 +242,20 @@
     assert_equal(-1, (Float::MAX.to_i*2) <=> inf)
     assert_equal(1, (-Float::MAX.to_i*2) <=> -inf)
 
+    bug3609 = '[ruby-core:31470]'
+    def (pinf = Object.new).infinite?; +1 end
+    def (ninf = Object.new).infinite?; -1 end
+    def (fin = Object.new).infinite?; nil end
+    nonum = Object.new
+    assert_equal(0, inf <=> pinf, bug3609)
+    assert_equal(1, inf <=> fin, bug3609)
+    assert_equal(1, inf <=> ninf, bug3609)
+    assert_nil(inf <=> nonum, bug3609)
+    assert_equal(-1, -inf <=> pinf, bug3609)
+    assert_equal(-1, -inf <=> fin, bug3609)
+    assert_equal(0, -inf <=> ninf, bug3609)
+    assert_nil(-inf <=> nonum, bug3609)
+
     assert_raise(ArgumentError) { 1.0 > nil }
     assert_raise(ArgumentError) { 1.0 >= nil }
     assert_raise(ArgumentError) { 1.0 < nil }

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

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