Issue 5800 - Wrong NAN bit pattern during array initialization
Summary: Wrong NAN bit pattern during array initialization
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-30 15:02 UTC by Ali Cehreli
Modified: 2011-03-30 18:07 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Ali Cehreli 2011-03-30 15:02:28 UTC
Environment: dmd 2.052, 64-bit Ubuntu.

The following program passes the two asserts, showing that the array elements are not initialized as NANs. The dumpBytes() function shows that the bit pattern is different than a local variable's:

import std.stdio;

void main()
{
    double[1] a;
    auto myNaN = double.nan;

    assert (a[0] != myNaN);        // <-- passes; BUG
    assert (a[0] != double.nan);   // <-- passes; BUG

    dumpBytes(a[0]);
    dumpBytes(myNaN);
}

void dumpBytes(T)(ref T var)
{
    const ubyte * beg = cast(ubyte*)&var;

    foreach (bayt; beg .. beg + T.sizeof) {
        writef("%02x ", *bayt);
    }

    writeln();
}

The bug manifests itself for float and real as well and the bit pattern changes depending on whether -m32 or -m64 is used. Here is one output of the program with -m64:

00 00 00 00 00 00 fc 7f 
00 00 00 00 00 00 f8 7f 

Ali
Comment 1 kennytm 2011-03-30 15:20:01 UTC
Isn't it expected that 'nan' does not equate to itself?
Comment 2 Ali Cehreli 2011-03-30 18:07:10 UTC
My mistake: "Equality Expressions" at http://digitalmars.com/d/2.0/expression.html says "If either or both operands are NAN, then both the == returns false and != returns true." [sic]