This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Possible bug in floating point comparison


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.

Regards
Sreeram
Tachyon Technologies



#include <stdio.h>
#include <string.h>

void TheCup( double a, double b, double c ) {
    while ( a > (b + c) ) {
        a = b + c;
    }
}

int main() {
    unsigned char data[] = { 
        0xE6, 0xA4, 0xE6, 0x00, 0x45, 0xA7, 0xAD, 0x40, 
        0x78, 0xBF, 0x91, 0xAE, 0x44, 0x6F, 0xA7, 0x40, 
        0xB7, 0x95, 0x53, 0x49, 0x01, 0xE0, 0x88, 0x40 
    };

    double a, b, c;

    memcpy( &a, data, sizeof(double) );
    memcpy( &b, data+sizeof(double), sizeof(double) );
    memcpy( &c, data+2*sizeof(double), sizeof(double) );

    printf( "Calling TheCup() with:\n" );
    printf( "a = %lf\n", a );
    printf( "b = %lf\n", b );
    printf( "c = %lf\n", c );
    printf( "b+c = %lf\n", b+c );

    printf( "Binary rep:\n" );
    printf( "a = %llX\n", a );
    printf( "b = %llX\n", b );
    printf( "c = %llX\n", c );
    printf( "b+c = %llX\n", b+c );

    TheCup( a, b, c );

    return 0;
}

Attachment: pgp00000.pgp
Description: PGP signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]