|
|
Invisible wrote:
> If you're that hell-bent on avoiding the square root, it can be
> approximated as follows:
>
> Suppose that the most significant non-zero digit of x is in the 2^n
> place. Let y = x / 2^(n/2). Now Sqrt(x) is approximately x / y.
>
> Usually this estimated value is greater than the true square root (but
> always by less than 45%). However, exact powers of 2 seem to come out
> slightly below the true value. (By about 10^-16.) I don't know if that's
> a glitch in my test program or what... It shouldn't be insurmountable
> though.
One would have to be very hell-bent indeed to employ, in lieu of sqrt(),
functions that are probably just as computationally expensive, like ln()
and exp(). Unless you have a quick way of getting n and 2^(n/2) without
ln() and exp(), you'll need them.
Now if you know the internal representation of your floats (or doubles),
you can slap together a union like
union MajorKludge {
double dvalue;
char carray[sizeof double];
};
and get both n and 2^(n/2) using some very simple bitwise logical
operations (as well as code whose portability will be highly suspect).
Regards,
John
Post a reply to this message
|
|