https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112009-05-11T19:04:11ZRuby Issue Tracking SystemRuby master - Bug #1363: Wrong value for Hash of NaNhttps://redmine.ruby-lang.org/issues/1363?journal_id=39362009-05-11T19:04:11Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>=begin<br>
Hi,</p>
<p>At Wed, 8 Apr 2009 14:38:31 +0900,<br>
Heesob Park wrote in <a href="/issues/1363">[ruby-core:23154]</a>:</p>
<blockquote>
<p>Ruby cannot handle NaN as a unique key of Hash.</p>
</blockquote>
<p>It's easy to make them unique as key, I'm not sure which is<br>
"correct" behavior though.</p>
<blockquote>
<p>I think this is related with the NaN comparing problem:</p>
<p>irb(main):001:0> 0/0.0 == 0/0.0<br>
=> false<br>
irb(main):002:0> a = 0/0.0<br>
=> NaN<br>
irb(main):003:0> a == a<br>
=> false<br>
irb(main):004:0> a <=> a<br>
=> nil</p>
</blockquote>
<p>I think it's mathematically correct behavior, and different<br>
thing from Hash.</p>
<h1>
<br>
Index: numeric.c</h1>
<p>--- numeric.c (revision 23390)<br>
+++ numeric.c (working copy)<br>
@@ -1127,7 +1127,6 @@ flo_eql(VALUE x, VALUE y)<br>
double a = RFLOAT_VALUE(x);<br>
double b = RFLOAT_VALUE(y);<br>
-#if defined(_MSC_VER) && _MSC_VER < 1300</p>
<ul>
<li>if (isnan(a) || isnan(b)) return Qfalse;<br>
-#endif</li>
</ul>
<ul>
<li>if (isnan(a) && isnan(b))</li>
<li>
<pre><code> return memcmp(&a, &b, sizeof(a)) ? Qfalse : Qtrue;
</code></pre>
if (a == b)<br>
return Qtrue;<br>
</li>
</ul>
<p>--<br>
Nobu Nakada</p>
<p>=end</p> Ruby master - Bug #1363: Wrong value for Hash of NaNhttps://redmine.ruby-lang.org/issues/1363?journal_id=39382009-05-11T21:21:31Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>=begin<br>
Hi,</p>
<p>In message "Re: <a href="https://blade.ruby-lang.org/ruby-core/23423">[ruby-core:23423]</a> Re: [Bug <a class="issue tracker-1 status-6 priority-4 priority-default closed" title="Bug: Wrong value for Hash of NaN (Rejected)" href="https://redmine.ruby-lang.org/issues/1363">#1363</a>] Wrong value for Hash of NaN"<br>
on Mon, 11 May 2009 19:03:59 +0900, Nobuyoshi Nakada <a href="mailto:nobu@ruby-lang.org" class="email">nobu@ruby-lang.org</a> writes:</p>
<p>|At Wed, 8 Apr 2009 14:38:31 +0900,<br>
|Heesob Park wrote in <a href="/issues/1363">[ruby-core:23154]</a>:<br>
|> Ruby cannot handle NaN as a unique key of Hash.<br>
|<br>
|It's easy to make them unique as key, I'm not sure which is<br>
|"correct" behavior though.</p>
<p>Two NaN values are not equal with each other by definition, in that<br>
sense, even though some might want to use NaN as a hash key, the<br>
current behavior seems to be "correct".</p>
<pre><code> matz.
</code></pre>
<p>=end</p> Ruby master - Bug #1363: Wrong value for Hash of NaNhttps://redmine.ruby-lang.org/issues/1363?journal_id=39392009-05-11T21:34:29Zphasis68 (Heesob Park)phasis@gmail.com
<ul></ul><p>=begin<br>
In order to prevent confusion in using NaN as hash key,<br>
it would be better to raise an error like in Lua.</p>
<p>Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio</p>
<blockquote>
<p>h = {}<br>
h[0/0.0] = 1<br>
stdin:1: table index is NaN<br>
stack traceback:<br>
stdin:1: in main chunk<br>
[C]: ?</p>
</blockquote>
<p>=end</p> Ruby master - Bug #1363: Wrong value for Hash of NaNhttps://redmine.ruby-lang.org/issues/1363?journal_id=39402009-05-11T21:58:54Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>=begin<br>
Yukihiro Matsumoto wrote:</p>
<blockquote>
<p>Hi,</p>
<p>In message "Re: <a href="https://blade.ruby-lang.org/ruby-core/23423">[ruby-core:23423]</a> Re: [Bug <a class="issue tracker-1 status-6 priority-4 priority-default closed" title="Bug: Wrong value for Hash of NaN (Rejected)" href="https://redmine.ruby-lang.org/issues/1363">#1363</a>] Wrong value for Hash of NaN"<br>
on Mon, 11 May 2009 19:03:59 +0900, Nobuyoshi Nakada <a href="mailto:nobu@ruby-lang.org" class="email">nobu@ruby-lang.org</a> writes:</p>
<p>|At Wed, 8 Apr 2009 14:38:31 +0900,<br>
|Heesob Park wrote in <a href="/issues/1363">[ruby-core:23154]</a>:<br>
|> Ruby cannot handle NaN as a unique key of Hash.<br>
|<br>
|It's easy to make them unique as key, I'm not sure which is<br>
|"correct" behavior though.</p>
<p>Two NaN values are not equal with each other by definition, in that<br>
sense, even though some might want to use NaN as a hash key, the<br>
current behavior seems to be "correct".</p>
</blockquote>
<p>I don't argue about correctness on NaNs being different each other, but I also<br>
agree with the reporter with Hash being inconvenient when NaNs used as keys.</p>
<p>Attachment: signature.asc<br>
=end</p> Ruby master - Bug #1363: Wrong value for Hash of NaNhttps://redmine.ruby-lang.org/issues/1363?journal_id=39472009-05-12T15:51:06Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>=begin<br>
Hi,</p>
<p>In message "Re: <a href="https://blade.ruby-lang.org/ruby-core/23427">[ruby-core:23427]</a> Re: [Bug <a class="issue tracker-1 status-6 priority-4 priority-default closed" title="Bug: Wrong value for Hash of NaN (Rejected)" href="https://redmine.ruby-lang.org/issues/1363">#1363</a>] Wrong value for Hash of NaN"<br>
on Mon, 11 May 2009 21:58:42 +0900, Urabe Shyouhei <a href="mailto:shyouhei@ruby-lang.org" class="email">shyouhei@ruby-lang.org</a> writes:</p>
<p>|I don't argue about correctness on NaNs being different each other, but I also<br>
|agree with the reporter with Hash being inconvenient when NaNs used as keys.</p>
<p>Using float values as hash keys should be strongly discouraged,<br>
because you may not have expected result anyway. a 3.14 may be<br>
different in bit-wise representation from another 3.14. I have no<br>
plan to prohibit using them as keys, right now, but see no need to<br>
change the language to encourage bad habit. If I have to make a<br>
change related to this issue, I'd rather prohibit NaN as a key (or<br>
float keys in general), as in <a href="https://blade.ruby-lang.org/ruby-core/23426">[ruby-core:23426]</a>.</p>
<pre><code> matz.
</code></pre>
<p>=end</p> Ruby master - Bug #1363: Wrong value for Hash of NaNhttps://redmine.ruby-lang.org/issues/1363?journal_id=47762009-07-16T16:43:43Zyugui (Yuki Sonoda)yugui@yugui.jp
<ul><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>=begin</p>
<p>=end</p> Ruby master - Bug #1363: Wrong value for Hash of NaNhttps://redmine.ruby-lang.org/issues/1363?journal_id=101872010-04-14T20:43:23Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li><li><strong>Target version</strong> set to <i>2.0.0</i></li></ul><p>=begin<br>
Hi,</p>
<p>The behavior OP reported is not a bug. So I close the ticket.</p>
<p>Indeed, the behavior is confusing a little.<br>
IMO, it would be good to warn when Float is used as hash keys.<br>
Anyway, warning or prohibiting NaN is a new feature, not bug<br>
fix for this ticket. If you still want, please register a<br>
new ticket to Feature tracker.</p>
<p>Thanks,</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a><br>
=end</p>