Bug 7836 – NaNs inside associative array don't use normal FP equality
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2012-04-05T17:45:00Z
Last change time
2013-11-07T08:47:27Z
Keywords
wrong-code
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-04-05T17:45:03Z
I don't know if this is a bug, or if here D/DMD is working as designed. I am not sure.
This code shows that floating point NaNs are not tested with == inside associative arrays (because NaNs != NaNs):
import std.stdio: writeln;
void main() {
int[double] aa;
aa[double.nan] = 1;
writeln(aa);
aa[double.nan] = 2;
writeln(aa);
}
Output DMD 2.059beta:
[nan:1]
[nan:2]
Python uses == to compare dict (associative array) keys, so NaNs can't overwrite each other:
>>> a = {}
>>> a[float("nan")] = 1
>>> a
{nan: 1}
>>> a[float("nan")] = 2
>>> a
{nan: 1, nan: 2}
So maybe DMD has to act as Python in this case.
Comment #1 by hsteoh — 2012-04-14T07:28:41Z
This is an AA implementation bug. Internally, AA's use bitwise comparison, which is wrong in many cases.
Comment #2 by hsteoh — 2013-07-08T19:57:27Z
Actually, this bug has nothing to do with AA's. The problem is that double's typeinfo.equals and typeinfo.compare does not respect NaNs:
import std.stdio: writeln;
void main() {
double x = double.nan, y = double.nan;
writeln(x == y); // prints false (OK)
writeln(typeid(double).equals(&x, &y)); // prints true (WRONG)
writeln(typeid(double).compare(&x, &y)); // prints 0 (WRONG)
}
Comment #3 by verylonglogin.reg — 2013-11-07T08:47:27Z
*** This issue has been marked as a duplicate of issue 5999 ***