Bug in egcs-2.91.66 - double comparisons

Richard Bielak richieb@calfp.com
Mon Nov 29 04:37:00 GMT 1999


"Martin v. Loewis" wrote:
> 
> > The correct answer is that both comparisons should come out the same.
> 
> The correct answer is that this depends. Please see
> 
> http://egcs.cygnus.com/faq.html#rounding
> 

Thanks. 

However, the problem with my example is that it either
fails or works with the same compiler on the same machine.

The problem seems to be with Intel floating point registers,
which hold more precision than a double. If the compiler
generates code that compares the registers then the
result can be different from what is expected.

Using the "-ffloat-store" option on the compile command line
is supposed to force storing of doubles back to memory
before comparisons to avoid this problem. However, my
example still fails with this option turned on.

When I use the -ffloat-store option the answer remains
wrong and doesn't change if optimization level is adjusted.

Sample code attached.


...richie

P.S. I'm using RH 6.1 and egcs-2.91.66.
PPS. The same exact example works as expected with Microsoft C compiler. :-(
#include <stdio.h>

static char digits[] = "0123456789ABCDEF";

void dump_double (char *l, double d) {
  char *p;
  int i;
  unsigned char k;

  p = (char *)&d;

  printf("%s", l);
  for (i=0;i<8;i++) {
	k = *p++;
	printf ("%c%c", digits [k / 16], digits [k % 16]);
  }
  printf ("\n");
}

int main (int argc, char **argv) {

  double t1, t2, t3, t4;

  t1 = 0.0400000000000000081;
  t2 = 0.20000000000000001; 
  /*  t3 = 0.20000000000000001; */
  t3 = t2;


  /* The comparison fails here */
  if (t1 <= t3 * t2) {
	printf ("OK \n");
  }
  else {
	printf ("Wrong!!! \n");
  }

  if (t1 == t2 * t3)
	printf ("Equal \n");
  else
	printf ("Not equal\n");

  t4 = t3 * t2;

  /* Comparison OK here */
  if (t1 <= t4) {
	printf ("OK \n");
  }
  else {
	printf ("Wrong!!! \n");
  }


  printf ("-----> t1=%1.50E \n", t1);
  printf ("-----> t2=%1.50E \n", t2);
  printf ("-----> t3=%1.50E \n", t3);
  printf ("--> t2*t3=%1.50E \n", t2*t3);
  printf ("-----> t4=%1.50E \n", t4);
  printf (" t1-t2*t3=%1.50E \n", t1 - t2*t3);
  printf ("--> t1-t4=%1.50E \n", t1 - t4);

  
  printf ("\n Hex dumps \n");

  dump_double ("-----> t1=", t1);
  dump_double ("-----> t2=", t2);
  dump_double ("-----> t3=", t3);
  dump_double ("-----> t4=", t4);
  dump_double ("--->t2*t3=", t2 * t3);
  dump_double ("->t1 - t4=", t1 - t4);
  dump_double (">t1-t2*t3=", t1 - t2*t3);
  
}


More information about the Gcc-bugs mailing list