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
#define MY_PI 3.14159265358979323846
double z = MY_PI;
puts(z == MY_PI ? "==" : "!=");
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
But if compiled with -ansi or -std=c99, then the equality fails, again
regardless of optimization!!
The preprocessed code looks as expected:
double z = 3.14159265358979323846;
puts(z == 3.14159265358979323846 ? "==" : "!=");
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
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: ian at macky dot net