summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--numeric.c10
-rw-r--r--test/ruby/test_float.rb14
3 files changed, 26 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index fdb3fbae52..bae8fb22af 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sun Jul 25 05:37:20 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * numeric.c (flo_cmp): honor the result of infinite? method of the
+ other. [ruby-core:31470]
+
Wed Jul 21 15:22:17 2010 Evan Phoenix <evan@fallingsnow.net>
* lib/rubygems/custom_require.rb, gem_prelude.rb: Load code from
diff --git a/numeric.c b/numeric.c
index fdea3ce50c..d291a6dd85 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1016,7 +1016,7 @@ rb_dbl_cmp(double a, double b)
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 @@ flo_cmp(VALUE x, VALUE y)
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);
}
diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb
index 03d9c94766..5687729418 100644
--- a/test/ruby/test_float.rb
+++ b/test/ruby/test_float.rb
@@ -239,6 +239,20 @@ class TestFloat < Test::Unit::TestCase
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 }