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]

Re: optimization problem with 1.0.3



> >> concerned, f is no longer even deterministic.  Isn't this a bad thing?
> >> Shouldn't the code generated by gcc prevent this kind of thing from
> >> happening?
> >
> >I don't see why.  how about a diagnostic message that says
> >"warning: you can never compare different floats/doubles with == or !=,
> >even if you think they're the same value."

The message would be wrong.  I have quite a bit of code that correctly
and safely compares floats and doubles with == and !=.  The code is
deterministic and correct on any machine that uses IEEE representations
for float and double, and is also deterministic and correct on the
other floating point representations I am aware of (Vax, Cray, TI
TMS320C3x and C4x).  That's because it deals only in values that can be
exactly represented as floating point values.  (The code does fixed
point math).


> But now look at this:
> 
> 
> beefalo% cat fp_problem2_p.c 
> #include <stdio.h>
> 
> double f(double x)
> {
>   return x/3.0;
> }
> 
> int main()
> {
>   int count;
>   double x = 1.0, y = 1.0;
> 
>   if(f(x) < f(y)) {
>     printf("f(x) is less than f(y)\n");
>     if(f(y) < f(x)) {
>       printf("AND\nf(y) is less than f(x)\n");
>     }
>   }
> 
>   return 0;
> }
> beefalo% gcc fp_problem2_p.c
> beefalo% a.out
> f(x) is less than f(y)
> AND
> f(y) is less than f(x)
> 
> 
> 
> Surely _this_ is bad?

Yes, it is bad.  It was this exact kind of thing that Kahan railed
against -- defects in floating point representation was breaking
code for finding roots of equations and the like because of oddball
behavior like this.


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