This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Possible bug in floating point comparison
- From: Andrew Haley <aph at redhat dot com>
- To: K S Sreeram <sreeram at tachyontech dot net>
- Cc: gcc at gcc dot gnu dot org
- Date: Mon, 22 Dec 2003 13:07:46 +0000
- Subject: Possible bug in floating point comparison
- References: <3FE6DD46.9040909@tachyontech.net>
K S Sreeram writes:
> Hi
>
> The following function goes into an infinite loop for some cases!!!.
>
> void func( double a, double b, double c ) {
> while ( a < (b+c) ) {
> a = b+c;
> }
> }
>
> I've attached a test program which has one sample input set which makes
> the above function go into an infinite loop
>
> I've tested with following versions of gcc:
> Redhat 9.0 (gcc 3.2.2), cygwin(2.95) and cygwin(3.3.1).
> In all the cases i used only the default gcc options "gcc <filename>".
>
> I've also tested with MSVC and it doesnt fail at all. From initial
> investigation it seems that the 'fcompp' instruction used by gcc does
> not work as expected in some cases.
That's not the problem. You're relying on exact floating-point
equality.
In this case, b+c is added, and the full 80-bit result is used.
However, you only pass (as a double) a 64-bit value to func.
0x400abb7a25748dfbc000 (2999.63414435827508) is added to
0x4008c7000a4a9cadb800 (796.0006281404556603) to give
0x400aed3a280735272e00 (3795.63477249873074)
which is compared to the number you passed in, which is
0x400aed3a280735273000 (3795.634772498730854)
Try this with -ffloat-store:
void TheCup( double a, double b, double c ) {
double z = b+c;
while ( a > z ) {
a = b + c;
}
}
Andrew.