[Bug c/45691] Floating point comparison failure
------- Comment #4 from jakub at gcc dot gnu dot org 2010-09-16 17:13 -------
i?86 is a FLT_EVAL_METHOD 2 target, so for strict C compliance all floating
operations and constants are supposed to be evaluated in the precision
of long double. The assignment of the constant to a double var or explicit
cast to double cause rounding to happen, so your testcase is evaluated as:
int main()
{
double z = 3.14159265358979323846L;
puts((long double) z == 3.14159265358979323846L ? "==" : "!=");
return 0;
}
and of course (double) 3.14159265358979323846L != 3.14159265358979323846L.
You can use -fexcess-precision=fast (the default unless -std=c99/-std=c89
is requested) for the old behavior, or you can add explicit cast,
z == (double) M_PI, or (as usually a good idea) avoid floating point
equality/non-equality comparisons, instead check whether difference is
smaller than some epsilon.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45691
------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.
Reply to: