In Flanagan and Matz's The Ruby Programming Language, I read this:
The
Numeric
classes perform simple type conversions in their==
operators, so that (for example) theFixnum 1
and theFloat 1.0
compare as equal.
Given that even two Float
s representing 1.0
can fail equality tests due to rounding, how can equality be guaranteed between a Fixnum
and a Float
? Couldn't it only be guaranteed between a Decimal
and a Float
?
Or is the book just being inexact because that's not a focus in the context of the chapter?
An edit, in hopes of adding clarity:
I just read that IEEE754 (floating point) can represent integers up to 224 exactly, and double up to 253. According to this question, 253+1 (9,007,199,254,740,993) is the first integer that cannot be represented exactly by double (and hence float). Then my question is, how does
9007199254740993.0 == 9007199254740993
evaluate to true
? Shouldn't rounding have caused the left-hand side (not representable by double
or float
) to round to a value that wouldn't match the right-hand side (an exact integer)?