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

ruby-changes:20525

From: knu <ko1@a...>
Date: Sun, 17 Jul 2011 17:53:20 +0900 (JST)
Subject: [ruby-changes:20525] knu:r32573 (ruby_1_8): Backport r23730; fixes #2462.

knu	2011-07-17 17:53:08 +0900 (Sun, 17 Jul 2011)

  New Revision: 32573

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

  Log:
    Backport r23730; fixes #2462.
    
    * numeric.c (flo_cmp): Infinity is greater than any bignum
      number.  [ruby-dev:38672]
    
    * bignum.c (rb_big_cmp): ditto.

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/bignum.c
    branches/ruby_1_8/numeric.c
    branches/ruby_1_8/test/ruby/test_float.rb

Index: ruby_1_8/numeric.c
===================================================================
--- ruby_1_8/numeric.c	(revision 32572)
+++ ruby_1_8/numeric.c	(revision 32573)
@@ -934,6 +934,10 @@
 	break;
 
       case T_BIGNUM:
+	if (isinf(a)) {
+	    if (a > 0.0) return INT2FIX(1);
+	    else return INT2FIX(-1);
+	}
 	b = rb_big2dbl(y);
 	break;
 
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 32572)
+++ ruby_1_8/ChangeLog	(revision 32573)
@@ -1,3 +1,10 @@
+Sun Jul 17 17:52:19 2011  Yukihiro Matsumoto  <matz@r...>
+
+	* numeric.c (flo_cmp): Infinity is greater than any bignum
+	  number.  [ruby-dev:38672]
+
+	* bignum.c (rb_big_cmp): ditto.
+
 Sun Jul 17 16:55:34 2011  Nobuyoshi Nakada  <nobu@r...>
 
 	* configure.in (STRIP): use proper toolchain.  based on a patch
Index: ruby_1_8/bignum.c
===================================================================
--- ruby_1_8/bignum.c	(revision 32572)
+++ ruby_1_8/bignum.c	(revision 32573)
@@ -1119,8 +1119,16 @@
 	break;
 
       case T_FLOAT:
-	return rb_dbl_cmp(rb_big2dbl(x), RFLOAT(y)->value);
+	{
+	    double a = RFLOAT_VALUE(y);
 
+	    if (isinf(a)) {
+		if (a > 0.0) return INT2FIX(-1);
+		else return INT2FIX(1);
+	    }
+	    return rb_dbl_cmp(rb_big2dbl(x), a);
+	}
+
       default:
 	return rb_num_coerce_cmp(x, y);
     }
Index: ruby_1_8/test/ruby/test_float.rb
===================================================================
--- ruby_1_8/test/ruby/test_float.rb	(revision 32572)
+++ ruby_1_8/test/ruby/test_float.rb	(revision 32573)
@@ -144,4 +144,31 @@
     assert_operator((-4611686018427387905.0).to_i, :<, 0)
     assert_operator((-4611686018427387906.0).to_i, :<, 0)
   end
+
+  def test_cmp
+    inf = 1.0 / 0.0
+    nan = inf / inf
+    assert_equal(0, 1.0 <=> 1.0)
+    assert_equal(1, 1.0 <=> 0.0)
+    assert_equal(-1, 1.0 <=> 2.0)
+    assert_nil(1.0 <=> nil)
+    assert_nil(1.0 <=> nan)
+    assert_nil(nan <=> 1.0)
+
+    assert_equal(0, 1.0 <=> 1)
+    assert_equal(1, 1.0 <=> 0)
+    assert_equal(-1, 1.0 <=> 2)
+
+    assert_equal(-1, 1.0 <=> 2**32)
+
+    assert_equal(1, inf <=> (Float::MAX.to_i*2))
+    assert_equal(-1, -inf <=> (-Float::MAX.to_i*2))
+    assert_equal(-1, (Float::MAX.to_i*2) <=> inf)
+    assert_equal(1, (-Float::MAX.to_i*2) <=> -inf)
+
+    assert_raise(ArgumentError) { 1.0 > nil }
+    assert_raise(ArgumentError) { 1.0 >= nil }
+    assert_raise(ArgumentError) { 1.0 < nil }
+    assert_raise(ArgumentError) { 1.0 <= nil }
+  end
 end

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

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