Issue #3609 has been updated by Marc-Andre Lafortune.

Category set to core

I completely agree that Math::Float <=> "foo" should return nil.

The current behavior is due to r23742 which wanted to address the fact that Float::Infinity <=> BigDecimal("1.0E500") was returning 0 (I think, see rubydev:38681)

To fix Float::Infinity <=> "foo", the minimum that must be done is:

diff --git a/numeric.c b/numeric.c
index eb3d4be..daa5d6d 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1038,7 +1038,7 @@ flo_cmp(VALUE x, VALUE y)
        break;
 
       default:
-       if (isinf(a) && (!rb_respond_to(y, rb_intern("infinite?")) ||
+       if (isinf(a) && (rb_respond_to(y, rb_intern("infinite?")) &&
                         !RTEST(rb_funcall(y, rb_intern("infinite?"), 0, 0)))) {
            if (a > 0.0) return INT2FIX(1);
            return INT2FIX(-1);


The fact that <=> is not consistent with <, etc, is also a problem that need to be fixed. Either the special treatment should be extended to the other comparison operators, or the special treatment for infinity should be removed from <=>

I believe the special treatment should be removed altogether:

diff --git a/numeric.c b/numeric.c
index eb3d4be..a6c5360 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1038,11 +1038,6 @@ 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 (a > 0.0) return INT2FIX(1);
-           return INT2FIX(-1);
-       }
        return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
     }
     return rb_dbl_cmp(a, b);


I understand the intent, but the fact is that Float::INFINITY is a very big value, but since it is the float representation of a lot of big real numbers, like 10**400, 10**40000 or even Infinity itself, I feel that r23742 introduces many inconsistencies. For example, currently:

1.0e200 ** 2 <=> BigDecimal("1.0e99999")  # => 1


----------------------------------------
http://redmine.ruby-lang.org/issues/show/3609

----------------------------------------
http://redmine.ruby-lang.org