From: eregontp@... Date: 2020-03-21T18:38:41+00:00 Subject: [ruby-core:97571] [Ruby master Bug#16700] Inconsistent behavior of equal? between Integer and Float Issue #16700 has been updated by Eregon (Benoit Daloze). It's a side effect of MRI using C's `==` to implement `equal?`. In TruffleRuby reference equality has its own logic, so the results are more consistent (e.g., all Floats are compared by their binary representation): https://github.com/oracle/truffleruby/blob/74a40b57ffaafbc26fe1b9d3cf22cf21ad7191bf/src/main/java/org/truffleruby/core/basicobject/BasicObjectNodes.java#L89-L169 I guess MRI could do the same, but it would likely occur some performance hit. In any case, comparing numbers by identity is usually an anti-pattern, and I think that's why MRI can be a bit loose on e.g. #equal? with Floats here. ---------------------------------------- Bug #16700: Inconsistent behavior of equal? between Integer and Float https://bugs.ruby-lang.org/issues/16700#change-84724 * Author: thyresias (Thierry Lambert) * Status: Rejected * Priority: Normal * ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [i386-mingw32] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- ~~~ruby 0.equal?(0) #=> true 0.0.equal?(0.0) #=> false x = 0.0 x.equal?(x) #=> true ~~~ Since Float objects are immutable, I would expect 0.0.equal?(0.0) to be true. -- https://bugs.ruby-lang.org/ Unsubscribe: