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]

Re: How to determine that 2.0 == (float) 2 in a front-end.


dewar@gnat.com (Robert Dewar)  wrote on 12.07.02 in <20020712163252.D2579F2CB6@nile.gnat.com>:

> <<Why is that?  Naively, I would think that x*x*x*x (evaluated left to
> right) has 3 multiplications in a row, while (x**2)**2 has only two.  So,
> from an error propagation point of view, the latter should be more accurate.
> >>
>
> It is always hard to get an intuitive feel for these results (the above
> result is fairly easy to prove). Here is an attempt. In the case of
> x*x*x*x, you have 3 multiplications but in the 2nd and 3rd, one of the
> operands at least is exact. In the case of (x**2)**2 the second
> multiplication has two potentially inexact operands so the error is
> multiplied.

Let's see. (And let's assume x>0 so we can ignore the sign, which makes no  
difference to the error anyway.)

With eps being the largest possible error for a multiplication:

x*x     = x^2+/-eps
x*x*x   = x^3+/-eps*x+/-eps = x^3+/-eps*(x+1)
x*x*x*x = x^4+/-eps*(x+1)*x+/-eps = x^4+/-eps*(x^2+x+1)

(x*x)*(x*x) = x^4+/-2*eps+eps^2+/-eps = x^4+/-eps*(3+eps)

Assuming I did not make a mistake here ... So, what is larger - x^2+x+1 or  
eps+3? It seems there is no one true answer - for small x, x*x*x*x is  
better, whereas for large x, (x*x)*(x*x) is better. |x|=1 seems to be  
close to the dividing point.

MfG Kai


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