www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1756] New: comparing a constant to artihmetic expression with floating point types fails

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

           Summary: comparing a constant to artihmetic expression with
                    floating point types fails
           Product: D
           Version: 2.008
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: sven.stoenner googlemail.com


the direct comparison of a constant to an arithmetic expression does not work
correctly when using variables:

// float
float a = 0.6f, 0.8f;
float c = a / b;
assert(c == 0.6f / 0.8f); // ok
assert(c == a / b); // fails
assert(0.75f == 0.6f / 0.8f); // ok
assert(0.75f == a / b); // fails

// double
double a = 0.6, b = 0.8;
double c = a / b;
assert(c == 0.6 / 0.8); // fails
assert(c == a / b); // fails
assert(0.75 == 0.6 / 0.8); // ok
assert(0.75 == a / b); // fails


-- 
Dec 31 2007
next sibling parent Christopher Wright <dhasenan gmail.com> writes:
d-bugmail puremagic.com wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=1756
 
            Summary: comparing a constant to artihmetic expression with
                     floating point types fails
            Product: D
            Version: 2.008
           Platform: PC
         OS/Version: Windows
             Status: NEW
           Severity: normal
           Priority: P2
          Component: DMD
         AssignedTo: bugzilla digitalmars.com
         ReportedBy: sven.stoenner googlemail.com
 
 
 the direct comparison of a constant to an arithmetic expression does not work
 correctly when using variables:
 
 // float
 float a = 0.6f, 0.8f;
Is this exactly what you tried? It should of course be: float a = 0.6f, b = 0.8f;
Dec 31 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1756






 Is this exactly what you tried?
 It should of course be:
 float a = 0.6f, b = 0.8f;
 
no, i've tried float a = 0.6f, b = 0.8f; this failure is occured while editing this report, sorry. --
Dec 31 2007
prev sibling next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
<d-bugmail puremagic.com> wrote in message 
news:bug-1756-3 http.d.puremagic.com/issues/...
 http://d.puremagic.com/issues/show_bug.cgi?id=1756
 the direct comparison of a constant to an arithmetic expression does not 
 work
 correctly when using variables:

 // float
 float a = 0.6f, 0.8f;
 float c = a / b;
 assert(c == 0.6f / 0.8f); // ok
 assert(c == a / b); // fails
 assert(0.75f == 0.6f / 0.8f); // ok
 assert(0.75f == a / b); // fails

 // double
 double a = 0.6, b = 0.8;
 double c = a / b;
 assert(c == 0.6 / 0.8); // fails
 assert(c == a / b); // fails
 assert(0.75 == 0.6 / 0.8); // ok
 assert(0.75 == a / b); // fails
You forgot one. // real real a = 0.6, b = 0.8; real c = a / b; assert(c == 0.6 / 0.8); // ok assert(c == a / b); // ok assert(0.75 == 0.6 / 0.8); // ok assert(0.75 == a / b); // ok They all pass when you use real because the compiler does constant folding by casting all floating point constants to real. When you then compare it to a float or double, it fails because the constant is more precise than the float or double.
Dec 31 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1756






You should almost never rely on == to compare floating point numbers.  
The failure you're seeing is a perfect example of why not.
Here's the first thing I could google up about it:
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
but if you'll look you should find dozens of references saying the same thing.

That said, I can see why you would expect those particular cases to work, but
in general you should never be surprised by equality failing in floating point
comparisons.  There are just too many things that can go wrong.


-- 
Dec 31 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1756


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX





Due to roundoff error, expressions that are mathematically the same are not the
same with computer floating point.


-- 
Aug 28 2008