Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behaviour if BigDecimal.new("NaN") is used as a key of hash #31

Closed
yui-knk opened this issue Nov 29, 2016 · 4 comments
Closed

Comments

@yui-knk
Copy link
Contributor

yui-knk commented Nov 29, 2016

$ ruby -v
ruby 2.4.0preview3 (2016-11-07 trunk 56661) [x86_64-darwin14]
irb(main):001:0> require "bigdecimal"
=> true
irb(main):002:0>  nan = BigDecimal.new("NaN")
=> #<BigDecimal:7ff5f213bad8,'NaN',9(9)>
irb(main):003:0> h = {nan => 1000}
=> {#<BigDecimal:7ff5f213bad8,'NaN',9(9)>=>1000}
irb(main):004:0> h[BigDecimal.new("NaN")]
=> nil
irb(main):005:0> h[nan]
=> 1000

I expected h[nan] to return nil, because nan.eql? nan returns false.
This is because hash compare pointers first.
https://github.com/ruby/ruby/blob/v2_4_0_preview3/hash.c#L99-L102

@mrkn
Copy link
Member

mrkn commented Dec 21, 2016

NaN is difficult even if it is a Float.

irb(main):001:0> a = Float::NAN
=> NaN
irb(main):002:0> b = 0.0 / 0.0
=> NaN
irb(main):003:0> c = Float::INFINITY * 0.0
=> NaN
irb(main):004:0> h = { a => 1, b => 2, c => 3 }
=> {NaN=>1, NaN=>2, NaN=>3}
irb(main):005:0> h[a]
=> 1
irb(main):006:0> h[b]
=> 2
irb(main):007:0> h[c]
=> 3

@mrkn
Copy link
Member

mrkn commented Dec 21, 2016

IMO at least we need to discuss how to define the equivalence of NaN including Float rather than only BigDecimal in bugs.ruby-lang.org.

@yui-knk
Copy link
Contributor Author

yui-knk commented Jan 31, 2017

https://bugs.ruby-lang.org/issues/13146 discusses about Float NaN.

@mrkn
Copy link
Member

mrkn commented Nov 22, 2018

https://bugs.ruby-lang.org/issues/13146 was rejected.

@mrkn mrkn closed this as completed Nov 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants