digitalmars.D.bugs - [Issue 1756] New: comparing a constant to artihmetic expression with floating point types fails
- d-bugmail puremagic.com (30/30) Dec 31 2007 http://d.puremagic.com/issues/show_bug.cgi?id=1756
- Christopher Wright (4/25) Dec 31 2007 Is this exactly what you tried?
- d-bugmail puremagic.com (6/10) Dec 31 2007 no, i've tried
- Jarrett Billingsley (14/32) Dec 31 2007 You forgot one.
- d-bugmail puremagic.com (11/11) Dec 31 2007 http://d.puremagic.com/issues/show_bug.cgi?id=1756
- d-bugmail puremagic.com (10/10) Aug 28 2008 http://d.puremagic.com/issues/show_bug.cgi?id=1756
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
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
http://d.puremagic.com/issues/show_bug.cgi?id=1756 ------- Comment #2 from sven.stoenner googlemail.com 2007-12-31 07:55 -------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
<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=1756the 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); // failsYou 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
http://d.puremagic.com/issues/show_bug.cgi?id=1756 ------- Comment #4 from wbaxter gmail.com 2007-12-31 13:11 ------- 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
http://d.puremagic.com/issues/show_bug.cgi?id=1756 bugzilla digitalmars.com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |WONTFIX ------- Comment #5 from bugzilla digitalmars.com 2008-08-28 17:56 ------- Due to roundoff error, expressions that are mathematically the same are not the same with computer floating point. --
Aug 28 2008