Bug #3609: Float Infinity comparisons in 1.9
http://redmine.ruby-lang.org/issues/show/3609

Author: Tomasz Wegrzanowski
Status: Open, Priority: Normal
ruby -v: ruby 1.9.1p429 (2010-07-02 revision 28523) [i386-darwin9]

The way <=> works on pretty much everything in Ruby
is that if a <=> b return 0, 1, or -1, it completely
determines the entire set of comparisons
a==b, a>=b, a>b, a<=b, a<b,
b<=>a, b==a, b>=a, b>a, b<=a, b<a.
(and if it doesn't, a==b/b==a will be both true or both false,
everything else will raise exception or return false/nil)

Float Infinity in 1.9 but not 1.8 seems to violate that.
Comparing it with strange things returns 1 if it's on the left,
but raises exception in every other way.

inf = 1.0/0.0 
inf <=> "foo" # => 1
"foo" <=> inf # ArgumentError: comparison of String with Float failed

This interacts even more strangely with very large bignums and the
"if bignum converts to float, it equals that float" thing Ruby currently does
[ruby-core:31376].

inf=1.0/0.0
huge=10**500

Consistent either way:
inf  >= huge # => true
huge <= inf  # => true
inf  < huge  # => false
huge > inf   # => false

Consistent only with mathematical interpretation
(or with "equal if converts, except for special cases
for infinities"):
inf <=> huge # =>  1
huge<=> inf  # => -1
huge <  inf  # => true
huge >= inf  # => false

Consistent only with "equal if converts":
inf  == huge  # => true
huge == inf   # => true
inf   > huge  # => false
inf  <= huge  # => true

Now I'd definitely prefer mathematical interpretation of floats,
to "equal if converts", but this just doesn't make any sense
no matter which way I look at it.


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