From: "Eregon (Benoit Daloze) via ruby-core" Date: 2026-10-19T19:25:23+00:00 Subject: [ruby-core:127177] [Ruby Bug#22452] `rb_equal` returns true for `NaN == NaN` Issue #22452 has been updated by Eregon (Benoit Daloze). There is also `equal?`: ```ruby irb(main):003> RUBY_DESCRIPTION => "ruby 4.0.7 (2026-09-15 revision 229531a6cf) +PRISM [x86_64-linux]" irb(main):001> Float::NAN == Float::NAN => false irb(main):002> Float::NAN.equal?(Float::NAN) => true irb(main):008> Float::NAN.equal?(-(0.0/0.0)) => false irb(main):009> Float::NAN.equal?(0.0/0.0) => false irb(main):001> [Float::NAN, 0.0/0.0, -(0.0/0.0)].pack('d3').bytes.each_slice(8).to_a => [[0, 0, 0, 0, 0, 0, 248, 127], [0, 0, 0, 0, 0, 0, 248, 127], [0, 0, 0, 0, 0, 0, 248, 255]] ``` And on TruffleRuby: ```ruby irb(main):008> RUBY_DESCRIPTION => "truffleruby 40.0.0-dev-da7373fe (2026-09-03), like ruby 4.0.2, Oracle GraalVM Native [x86_64-linux]" irb(main):003> Float::NAN == Float::NAN => false irb(main):005> Float::NAN.equal?(Float::NAN) => true irb(main):006> Float::NAN.equal?(-(0.0/0.0)) => true irb(main):007> Float::NAN.equal?(0.0/0.0) => false irb(main):002> [Float::NAN, 0.0/0.0, -(0.0/0.0)].pack('d3').bytes.each_slice(8).to_a => [[0, 0, 0, 0, 0, 0, 248, 127], [0, 0, 0, 0, 0, 0, 248, 255], [0, 0, 0, 0, 0, 0, 248, 127]] ``` (for I'm not sure what reason the NaN bit pattern is inverted for `0.0/0.0` and `-(0.0/0.0)`, on CRuby and TruffleRuby, maybe on C vs JVM) ---------------------------------------- Bug #22452: `rb_equal` returns true for `NaN == NaN` https://bugs.ruby-lang.org/issues/22452#change-119590 * Author: byroot (Jean Boussier) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- I noticed this while trying to cleanup and simplify `rb_equal`. ```ruby >> Float::NAN == Float::NAN => false >> [Float::NAN].index(Float::NAN) => 0 ``` I would expect it to return `nil`. `Array#index` is just an example, any core method, and even C extension relying on `rb_equal` is similarly impacted. This bug is present on all Ruby versions, except for versions between 2.1 and 2.4. The bug seem to have been re-introduced in 2.5 by [6e0e067de31511cebec01d176f628442371db879](https://github.com/ruby/ruby/commit/6e0e067de31511cebec01d176f628442371db879) JRuby seem to have the same behavior, but TruffleRuby correctly returns`nil`. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/