[Bug c/45691] New: Floating point comparison failure

2010-09-16 Thread ian at macky dot net
After upgrading to gcc 4.5.0 from 3.3.4, some of my floating point code fails. 
Searched for and could not find a matching bug.  It boils down to this very
simple example:

#include stdio.h

#define MY_PI   3.14159265358979323846

int main()
{
double z = MY_PI;
puts(z == MY_PI ? == : !=);
return 0;
}

If this is compiled gcc -o bug -Wall bug.c it works: there is equality. 
Doesn't matter if optimization is used or not (-g, -O, -O2 all give same
results).

But if compiled with -ansi or -std=c99, then the equality fails, again
regardless of optimization!!

The preprocessed code looks as expected:

int main()
{
double z = 3.14159265358979323846;
puts(z == 3.14159265358979323846 ? == : !=);
return 0;
}

Cannot see how this is correct behavior since the exact same expression was
used to initialize the variable and to test for equality.  Do not see anything
in my ANSI/ISO C reference that sheds any light.

I can work around this by using actual double constants instead of preprocessor
expressions (double my_pi_2 = MY_PI_2 and setting/testing with my_pi_2, etc),
but this should work as is!


-- 
   Summary: Floating point comparison failure
   Product: gcc
   Version: 4.5.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: ian at macky dot net


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45691



[Bug c/45691] Floating point comparison failure

2010-09-16 Thread ian at macky dot net


--- Comment #6 from ian at macky dot net  2010-09-16 17:44 ---
Subject: Re:  Floating point comparison failure

Thanks everyone.  I usually do fuzzy floating-point comparison, except in
certain special circumstances.  I will switch to using double constants;
I'm trying for a code that is maximally portable, so having to worry about
what exact compiler switches to use is anathema.

And might I add: you people are super-fast!  I'm very impressed.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45691