D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7546 - 64-bit floating-point issue with negative zero: -0.0 == 0.0 is false.
Summary: 64-bit floating-point issue with negative zero: -0.0 == 0.0 is false.
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 All
: P2 normal
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-02-19 11:15 UTC by kennytm
Modified: 2012-05-14 01:27 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description kennytm 2012-02-19 11:15:03 UTC
Test case:

----------------------
void main()
{
    auto p = -0.0;
    assert(p == 0);
}
----------------------

Running the program with -m64 caused an Assertion failure. (This works in 32-bit.)

An equivalent test which also fails in 64-bit:

----------------------
void main()
{
    double p = -0.0;
    assert(p == -0.0);
}
----------------------
Comment 1 kennytm 2012-02-19 11:38:41 UTC
The problem is that, when comparing with 0.0 or -0.0, the backend will generate an integer 'cmp' instruction, but -0.0 has a different bit pattern than 0.0, so the equality will fail.

     cmp    QWORD PTR [rbp-0x8],0x0
     je     ...

The code works if the type is 'float' because it uses an 'add eax, eax' trick make both 0.0f and -0.0f set the ZF flag.

This code also work if the type if 'real' because it uses x87.
Comment 2 bearophile_hugs 2012-02-19 12:05:22 UTC
I think 0.0 == -0.0, but 0.0 !is -0.0.
Comment 3 github-bugzilla 2012-05-11 19:02:54 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/a8ffc8ab60aa3e1ae965e5764f0900e14a0881c1
Fix issue 7546 64-bit floating-point issue with negative zero: -0.0 == 0.0 is false

Just duplicate the code for float == float.