The double could be anything, but reasonably it won't be much more than say, 500.
How about something like this for a quick hack at getting 1 decimal accuracy using Newton's method. Right now I haven't done I/O stuff, I am just altering numbers as needed.
My question is that since Newton's method depends on the initial guess, what is a good initial guess for sqrt(k)? And would this be any faster than tradition methods?
k = 29.5546743;
prevroot = 15;
nextroot = 0;
precision = 1.0;
while (precision > 0.05)
nextroot = (prevroot * prevroot + k) / (2 * prevroot);
precision = nextroot - prevroot;
if (precision < 0)
precision *= -1;
prevroot = nextroot;
Actually, scratch that, this seems to work (I just took it from the link):
I don't even begin to understand what's going on there, but thanks so much
x = 29.456;
xhalf = 0.5f * x;
i = *(int*)&x; // store floating-point bits in integer
i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method
x = *(float*)&i; // convert new bits into float
x = x*(1.5f - xhalf*x*x); // One round of Newton's method
printf("%f\n",1.0 / x);
And one last edit: That fastsqrt actually makes my code run 20% slower than the library sqrt function haha. Any other ideas?