www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7546] New: 64-bit floating-point issue with negative zero: -0.0 == 0.0 is false.

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7546

           Summary: 64-bit floating-point issue with negative zero: -0.0
                    == 0.0 is false.
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: kennytm gmail.com



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);
}
----------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 19 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7546




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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 19 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7546


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



I think 0.0 == -0.0, but 0.0 !is -0.0.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 19 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7546




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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 11 2012