C
CBFalconer
Eric said:James said:James Hu wrote:
[...]
#define ISQRT_G_1(N) (N>>((sizeof(N)*CHAR_BIT)/2))
#define ISQRT_G_2(N) ((ISQRT_G_1(N)+N/ISQRT_G_1(N))/2)
#define ISQRT_G_3(N) ((ISQRT_G_2(N)+N/ISQRT_G_2(N))/2)
#define ISQRT_G_4(N) ((ISQRT_G_3(N)+N/ISQRT_G_3(N))/2)
#define ISQRT_G_5(N) ((ISQRT_G_4(N)+N/ISQRT_G_4(N))/2)
#define ISQRT_G_6(N) ((ISQRT_G_5(N)+N/ISQRT_G_5(N))/2)
#define ISQRT(N) ISQRT_G_6(N)
This isn't going to work very well for N less than
the square root of its type's maximum value: the initial
guess will be zero, and after that ...
I wrote that macro specifically to find the square root of
ULONG_MAX.
Aha. I hadn't realized the limited intent; sorry.
If you are willing to settle for the root of (<T>_MAX + 1), which
must be of the form 2**n, simpler methods are available. At worst
the root of 2 may be involved, for n odd.