Mail Archives: djgpp/1996/12/11/21:53:34
John M. Aldrich wrote:
>
> #include <math.h>
>
> double s;
> s = sqrt( pow( r, 2 ) - pow( m, 2 ) );
>
This is *very* likely to be extremely slow... Power functions are
usually much slower than multiplies...
I'd suggest either :
sqrt(r*r-m*m);
or, better,
sqrt((r-m)*(r+m));
(only one multiply)
The latter formula has another advantage, it should be a less
floating-point-error-prone : if r and m are close and very big,
r*r - m*m will result in incorrect results
Still better, formulae like
r * sqrt((1-m/r)*(1+m/r));
(if r and m are both positive).
Which are less rounding error prone, and will overflow less easily.
Francois
- Raw text -