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


>> You'd think (f(x)==f(x)) would always be true if f has no internal state,
>> but it turns out that it still depends on the details of f, and it depends
>> on whether optimization is on, and it depends on whether you are running
>> on Intel.  Even if (1.0==1.0) is taken to be always false, at least 
>> (1.0-1.0)<precision is true for some small number 'precision', but the same
>> cannot be said for f(x)-f(x) which can diverge uncontrollably.
>
>How so? If f() computes the same thing each time, you'd be able to put a
>bound on the propagated error. Sure, it won't be the same for each f().
>Note that I understand that if you say "1.0" or "x" you mean "the same
>value, computed two different ways".  Iff it is computed in a sensible way,
>that avoids massive rounding errors or under/overflows both times ;-)


Well, I don't mean "the same value, computed two different ways".
I mean, "the same value".  Look at this example:



beefalo% cat fp_problem_p.c
#include <stdio.h>

double f(double x)
{
  return x/3.0;
}

int main()
{
  int count;
  double x = 1.0;

  if(x != x) {
    printf("Fine, so be it...\n");
    exit(0);
  }
  
  if(f(x) != f(x)) {
    printf("Isn't this a problem?\n");
  }

  return 0;
}
beefalo% gcc fp_problem_p.c 
beefalo% a.out 
Isn't this a problem?


>From this, I draw two conclusions, one of which is a given, and the other
one which is surprising, at least to me, and makes me unhappy.
	1.  FP calculations are not exact
	2.  FP calculations are not deterministic

The first, I think I understand.  I understand that most numbers don't
have an exact binary representation.  I understand that the conditional
x==y tests for the equality of the their binary representations.  And
now, thanks to a previous post, I understand that this is happening to me
now that I am running on Intel specifically because of the 80 bit 
representation internal to the CPU, which is different from 64.  But
_accuracy_ isn't the problem.  The problem is that when I write f(x, y, ...)
in C, I will get some complicated spectrum of answers, depending on
the context in which f was called.  As far as the C programmer is
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?


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