Re: square root



Peter Spellucci wrote:

???????? x=2^{-12} -> sqrt(x)=2^{-6} giving almost 100% error

actually I don't think so.
I took an array 100 numbers uniformly spreaded between 0 and 1
and I've seen the difference between the classic sqrt and the
approximation at x,
I've calculated the max difference, the min difference, and the
standard deviation.
as result I've obtained,
for the approximation sqrt(x)=x for x in [0..1] I've obtained:
min difference=0 (because of course the sqrt(x) between 0 and 1 is
above the function y=x)
max difference=0.25
and a variance of 0.58
that's bad, I know, but is absolutely fast, and I can't say that it
have almost 100% error!

I've tried with the Newton iteration, I've implemented in that way (I
don't know if it is correct):

b.i = 0;
b.i = (a.i & (0xFF << 21));
b.f = 0.50086730334047+0.47140452079103*b.f;
b.f= 0.5*(b.f+a_/b.f);

return b.f;

where a and b are union of float (a.f and b.f) and int (a.i and b.i)..

and I've obtained:
max difference = -7.7486e-07
that sounds excellent!!
but the min difference=0.25
and a standard deviation of 0.385

Probably I did something wrong in my function, but... if I did correct,
I have to say that I prefere the approximation sqrt(x)=x because it
sucks as well, but at least is fast!

as alternative I've tried to do a sort of interpolation, approximating
the sqrt with two straight lines, for little values (let's say less
then 0.05) to the function y=3*x and for the bigger value (till 1) to
the line: y=0.2 * 0.8*x... the function is really little:
inline float interpolate_sqrt(float x){ return x = (x >= 0.05?
0.2+0.8*x : 3*x ); }
and it gives
min diff = -0.01 (very good!!!)
max diff=0.1125 (not bad!!)
and a variance of 0.48 (bad!)

I still prefere the approximation y=x because it gives an uniform
distribution of the errors...

so what to do mean by "good approximation"?

good question. I don't have a precise answer yet!

if you are indeed satisfied by a very low precision approximation
then exponent extraction plus the linear minimax approximation on [1/2,2]
should indeed be perfect

as I wrote above, in my intention,
exponent extraction:
b.i = (a.i & (0xFF << 21));
linear minimax approx:
b.f = 0.50086730334047+0.47140452079103*b.f;
and one newton iteration
b.f= 0.5*(b.f+a_/b.f);

is it correct?

bye, and thanks for the time you spent helping me!

tirzan

.