Possible bug in floating point comparison

Andrew Haley aph@redhat.com
Mon Dec 22 15:31:00 GMT 2003


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.



More information about the Gcc mailing list