ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:23154] [Bug #1363] Wrong value for Hash of NaN
@ 2009-04-08  5:38 Heesob Park
  2009-05-11 10:03 ` [ruby-core:23423] " Nobuyoshi Nakada
  0 siblings, 1 reply; 10+ messages in thread
From: Heesob Park @ 2009-04-08  5:38 UTC (permalink / raw
  To: ruby-core

Bug #1363: Wrong value for Hash of NaN
http://redmine.ruby-lang.org/issues/show/1363

Author: Heesob Park
Status: Open, Priority: Normal
ruby -v: ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]

Ruby cannot handle NaN as a unique key of Hash.

Here is an example:

irb(main):001:0> h = {}
=> {}
irb(main):002:0> h[0/0.0]=1
=> 1
irb(main):003:0> h[0/0.0]=2
=> 2
irb(main):004:0> h[0/0.0]=3
=> 3
irb(main):005:0> h
=> {NaN=>1, NaN=>2, NaN=>3}
irb(main):006:0> h[0/0.0]
=> nil

I think this is related with the NaN comparing problem:

irb(main):001:0> 0/0.0 == 0/0.0
=> false
irb(main):002:0> a = 0/0.0
=> NaN
irb(main):003:0> a == a
=> false
irb(main):004:0> a <=> a
=> nil


----------------------------------------
http://redmine.ruby-lang.org

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:23423] Re: [Bug #1363] Wrong value for Hash of NaN
  2009-04-08  5:38 [ruby-core:23154] [Bug #1363] Wrong value for Hash of NaN Heesob Park
@ 2009-05-11 10:03 ` Nobuyoshi Nakada
  2009-05-11 12:21   ` [ruby-core:23425] " Yukihiro Matsumoto
  0 siblings, 1 reply; 10+ messages in thread
From: Nobuyoshi Nakada @ 2009-05-11 10:03 UTC (permalink / raw
  To: ruby-core

Hi,

At Wed, 8 Apr 2009 14:38:31 +0900,
Heesob Park wrote in [ruby-core:23154]:
> Ruby cannot handle NaN as a unique key of Hash.

It's easy to make them unique as key, I'm not sure which is
"correct" behavior though.

> I think this is related with the NaN comparing problem:
> 
> irb(main):001:0> 0/0.0 == 0/0.0
> => false
> irb(main):002:0> a = 0/0.0
> => NaN
> irb(main):003:0> a == a
> => false
> irb(main):004:0> a <=> a
> => nil

I think it's mathematically correct behavior, and different
thing from Hash.

\f
Index: numeric.c
===================================================================
--- numeric.c	(revision 23390)
+++ numeric.c	(working copy)
@@ -1127,7 +1127,6 @@ flo_eql(VALUE x, VALUE y)
 	double a = RFLOAT_VALUE(x);
 	double b = RFLOAT_VALUE(y);
-#if defined(_MSC_VER) && _MSC_VER < 1300
-	if (isnan(a) || isnan(b)) return Qfalse;
-#endif
+	if (isnan(a) && isnan(b))
+	    return memcmp(&a, &b, sizeof(a)) ? Qfalse : Qtrue;
 	if (a == b)
 	    return Qtrue;
\f

-- 
Nobu Nakada

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:23425] Re: [Bug #1363] Wrong value for Hash of NaN
  2009-05-11 10:03 ` [ruby-core:23423] " Nobuyoshi Nakada
@ 2009-05-11 12:21   ` Yukihiro Matsumoto
  2009-05-11 12:34     ` [ruby-core:23426] " Heesob Park
  2009-05-11 12:58     ` [ruby-core:23427] " Urabe Shyouhei
  0 siblings, 2 replies; 10+ messages in thread
From: Yukihiro Matsumoto @ 2009-05-11 12:21 UTC (permalink / raw
  To: ruby-core

Hi,

In message "Re: [ruby-core:23423] Re: [Bug #1363] Wrong value for Hash of NaN"
    on Mon, 11 May 2009 19:03:59 +0900, Nobuyoshi Nakada <nobu@ruby-lang.org> writes:

|At Wed, 8 Apr 2009 14:38:31 +0900,
|Heesob Park wrote in [ruby-core:23154]:
|> Ruby cannot handle NaN as a unique key of Hash.
|
|It's easy to make them unique as key, I'm not sure which is
|"correct" behavior though.

Two NaN values are not equal with each other by definition, in that
sense, even though some might want to use NaN as a hash key, the
current behavior seems to be "correct".

							matz.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:23426] [Bug #1363] Wrong value for Hash of NaN
  2009-05-11 12:21   ` [ruby-core:23425] " Yukihiro Matsumoto
@ 2009-05-11 12:34     ` Heesob Park
  2009-05-11 12:58     ` [ruby-core:23427] " Urabe Shyouhei
  1 sibling, 0 replies; 10+ messages in thread
From: Heesob Park @ 2009-05-11 12:34 UTC (permalink / raw
  To: ruby-core

Issue #1363 has been updated by Heesob Park.


In order to prevent confusion in using NaN as hash key,
it would be better to raise an error like in Lua.

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> h = {}
> h[0/0.0] = 1
stdin:1: table index is NaN
stack traceback:
        stdin:1: in main chunk
        [C]: ?


----------------------------------------
http://redmine.ruby-lang.org/issues/show/1363

----------------------------------------
http://redmine.ruby-lang.org

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:23427] Re: [Bug #1363] Wrong value for Hash of NaN
  2009-05-11 12:21   ` [ruby-core:23425] " Yukihiro Matsumoto
  2009-05-11 12:34     ` [ruby-core:23426] " Heesob Park
@ 2009-05-11 12:58     ` Urabe Shyouhei
  2009-05-12  6:50       ` [ruby-core:23434] " Yukihiro Matsumoto
  1 sibling, 1 reply; 10+ messages in thread
From: Urabe Shyouhei @ 2009-05-11 12:58 UTC (permalink / raw
  To: ruby-core

[-- Attachment #1: Type: text/plain, Size: 798 bytes --]

Yukihiro Matsumoto wrote:
> Hi,
> 
> In message "Re: [ruby-core:23423] Re: [Bug #1363] Wrong value for Hash of NaN"
>     on Mon, 11 May 2009 19:03:59 +0900, Nobuyoshi Nakada <nobu@ruby-lang.org> writes:
> 
> |At Wed, 8 Apr 2009 14:38:31 +0900,
> |Heesob Park wrote in [ruby-core:23154]:
> |> Ruby cannot handle NaN as a unique key of Hash.
> |
> |It's easy to make them unique as key, I'm not sure which is
> |"correct" behavior though.
> 
> Two NaN values are not equal with each other by definition, in that
> sense, even though some might want to use NaN as a hash key, the
> current behavior seems to be "correct".

I don't argue about correctness on NaNs being different each other, but I also
agree with the reporter with Hash being inconvenient when NaNs used as keys.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:23434] Re: [Bug #1363] Wrong value for Hash of NaN
  2009-05-11 12:58     ` [ruby-core:23427] " Urabe Shyouhei
@ 2009-05-12  6:50       ` Yukihiro Matsumoto
  2009-05-12 19:41         ` [ruby-core:23437] " Martin DeMello
  2010-04-14 11:43         ` [ruby-core:29508] [Bug #1363](Rejected) " Yusuke Endoh
  0 siblings, 2 replies; 10+ messages in thread
From: Yukihiro Matsumoto @ 2009-05-12  6:50 UTC (permalink / raw
  To: ruby-core

Hi,

In message "Re: [ruby-core:23427] Re: [Bug #1363] Wrong value for Hash of NaN"
    on Mon, 11 May 2009 21:58:42 +0900, Urabe Shyouhei <shyouhei@ruby-lang.org> writes:

|I don't argue about correctness on NaNs being different each other, but I also
|agree with the reporter with Hash being inconvenient when NaNs used as keys.

Using float values as hash keys should be strongly discouraged,
because you may not have expected result anyway.  a 3.14 may be
different in bit-wise representation from another 3.14.  I have no
plan to prohibit using them as keys, right now, but see no need to
change the language to encourage bad habit.  If I have to make a
change related to this issue, I'd rather prohibit NaN as a key (or
float keys in general), as in [ruby-core:23426].

							matz.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:23437] Re: [Bug #1363] Wrong value for Hash of NaN
  2009-05-12  6:50       ` [ruby-core:23434] " Yukihiro Matsumoto
@ 2009-05-12 19:41         ` Martin DeMello
  2009-05-12 20:28           ` [ruby-core:23438] " Joel VanderWerf
  2010-04-14 11:43         ` [ruby-core:29508] [Bug #1363](Rejected) " Yusuke Endoh
  1 sibling, 1 reply; 10+ messages in thread
From: Martin DeMello @ 2009-05-12 19:41 UTC (permalink / raw
  To: ruby-core

On Tue, May 12, 2009 at 12:20 PM, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> Using float values as hash keys should be strongly discouraged,
> because you may not have expected result anyway.  a 3.14 may be
> different in bit-wise representation from another 3.14.  I have no
> plan to prohibit using them as keys, right now, but see no need to
> change the language to encourage bad habit.  If I have to make a
> change related to this issue, I'd rather prohibit NaN as a key (or
> float keys in general), as in [ruby-core:23426].

Personally, I would love to see floats prohibited as hash keys, with
the effect rippling outwards so that composite objects containing
floats weren't valid hash keys either unless you overrode their hash
method. I know the philosophy of ruby is to give users the power to
shoot themselves in the foot, but as you say, people who know what
they're doing won't use it anyway, and people who don't know about the
pitfalls involved would appreciate the safeguard. If you are even
vaguely contemplating this change, isn't now a good time to do it?

martin

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:23438] Re: [Bug #1363] Wrong value for Hash of NaN
  2009-05-12 19:41         ` [ruby-core:23437] " Martin DeMello
@ 2009-05-12 20:28           ` Joel VanderWerf
  2009-05-13  6:29             ` [ruby-core:23441] " Martin DeMello
  0 siblings, 1 reply; 10+ messages in thread
From: Joel VanderWerf @ 2009-05-12 20:28 UTC (permalink / raw
  To: ruby-core

Martin DeMello wrote:
> Personally, I would love to see floats prohibited as hash keys, with
> the effect rippling outwards so that composite objects containing
> floats weren't valid hash keys either unless you overrode their hash
> method. I know the philosophy of ruby is to give users the power to
> shoot themselves in the foot, but as you say, people who know what
> they're doing won't use it anyway, and people who don't know about the
> pitfalls involved would appreciate the safeguard. If you are even
> vaguely contemplating this change, isn't now a good time to do it?

By composite objects, you mean arrays, rather than instances of 
user-defined classes or hashes, right? The latter hash by object id, not 
by contents, IIRC.

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:23441] Re: [Bug #1363] Wrong value for Hash of NaN
  2009-05-12 20:28           ` [ruby-core:23438] " Joel VanderWerf
@ 2009-05-13  6:29             ` Martin DeMello
  0 siblings, 0 replies; 10+ messages in thread
From: Martin DeMello @ 2009-05-13  6:29 UTC (permalink / raw
  To: ruby-core

On Wed, May 13, 2009 at 1:58 AM, Joel VanderWerf
<vjoel@path.berkeley.edu> wrote:
> Martin DeMello wrote:
>>
>> Personally, I would love to see floats prohibited as hash keys, with
>> the effect rippling outwards so that composite objects containing
>> floats weren't valid hash keys either unless you overrode their hash
>> method. I know the philosophy of ruby is to give users the power to
>> shoot themselves in the foot, but as you say, people who know what
>> they're doing won't use it anyway, and people who don't know about the
>> pitfalls involved would appreciate the safeguard. If you are even
>> vaguely contemplating this change, isn't now a good time to do it?
>
> By composite objects, you mean arrays, rather than instances of user-defined
> classes or hashes, right? The latter hash by object id, not by contents,
> IIRC.

Yeah, I meant arrays of floats, and arrays of arrays of floats etc.

martin

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:29508] [Bug #1363](Rejected) Wrong value for Hash of NaN
  2009-05-12  6:50       ` [ruby-core:23434] " Yukihiro Matsumoto
  2009-05-12 19:41         ` [ruby-core:23437] " Martin DeMello
@ 2010-04-14 11:43         ` Yusuke Endoh
  1 sibling, 0 replies; 10+ messages in thread
From: Yusuke Endoh @ 2010-04-14 11:43 UTC (permalink / raw
  To: ruby-core

Issue #1363 has been updated by Yusuke Endoh.

Status changed from Open to Rejected
Target version set to 1.9.x

Hi,

The behavior OP reported is not a bug.  So I close the ticket.

Indeed, the behavior is confusing a little.
IMO, it would be good to warn when Float is used as hash keys.
Anyway, warning or prohibiting NaN is a new feature, not bug
fix for this ticket.  If you still want, please register a
new ticket to Feature tracker.

Thanks,

-- 
Yusuke Endoh <mame@tsg.ne.jp>
----------------------------------------
http://redmine.ruby-lang.org/issues/show/1363

----------------------------------------
http://redmine.ruby-lang.org

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-04-14 11:45 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-08  5:38 [ruby-core:23154] [Bug #1363] Wrong value for Hash of NaN Heesob Park
2009-05-11 10:03 ` [ruby-core:23423] " Nobuyoshi Nakada
2009-05-11 12:21   ` [ruby-core:23425] " Yukihiro Matsumoto
2009-05-11 12:34     ` [ruby-core:23426] " Heesob Park
2009-05-11 12:58     ` [ruby-core:23427] " Urabe Shyouhei
2009-05-12  6:50       ` [ruby-core:23434] " Yukihiro Matsumoto
2009-05-12 19:41         ` [ruby-core:23437] " Martin DeMello
2009-05-12 20:28           ` [ruby-core:23438] " Joel VanderWerf
2009-05-13  6:29             ` [ruby-core:23441] " Martin DeMello
2010-04-14 11:43         ` [ruby-core:29508] [Bug #1363](Rejected) " Yusuke Endoh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).