Date: Mon, 07 Nov 1994 11:42:24 +0100 From: Peter Csizmadia To: djgpp AT sun DOT soe DOT clarkson DOT edu Subject: Re: 2.4/3-2.4/3!=0 I wrote a second message about the second bug, (in reply to Stephen Turnbull's, who totally misunderstood the problem), but it seems it wasn't arrived, so I write again. I just received Bill Metzenthen's and Eli Zaretskii's messages. Their explanation for the first bug is acceptable. Bill partly explained the anomalous behaviour of the second program, but the bug is still unexplained. I simpified the program: #include main(int argc, char* argv[]) { double a = atof(argv[1]); double x = a/sqrt(1+a*a); printf("1-x=%le\n", 1.0-x); printf("x%s1\n", (x==1.0)? "==":"!="); !!! simple comparision !!! printf("1-x=%le\n", 1.0-x); } gcc g.c -o g -O1 go32 g 10 1-x=4.96280979001086e-03 x!=1 1-x=4.96280979001096e-03 !!! IT CHANGED !!! go32 g 100 1-x=4.99962503124712e-05 x!=1 1-x=4.99962503125451e-05 !!! IT CHANGED !!! go32 g 1e17 1-x=-3.25260651745651e-19 x==1 ??? 1-x=0.00000000000000e+00 !!! IT CHANGED !!! A simple comparison changed the value of a variable (x) when I compiled the program with -O1. When I compiled a program with these operations without optimizations (or with optimizations, but declaring all double variables volatile), 1-x was always >=0. When I optimized it, there was this bug, which caused the program to terminate with a floating point exception when it tried to calculate sqrt(1-x). I hardly found this bug, because when I compared 1-x to 0, (if(1-x<0) printf("akurvaeletbe\n"); or something like that before sqrt), the program did not hang (the comparison corrected the value of x, so it became 0). So my question is: Is it possible to compile this program with -O1 (and -ffloat-store), but without this bug? If it is, which optimization flag must I switch off after -O1?