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.
Code:
#include <stdio.h>
int main()
{
double k,prevroot,nextroot,precision;
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;
}
printf("%f\n",nextroot);
}
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?
Actually, scratch that, this seems to work (I just took it from the link):
Code:
#include <stdio.h>
int main()
{
float x;
x = 29.456;
float xhalf;
xhalf = 0.5f * x;
int i;
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);
}
I don't even begin to understand what's going on there, but thanks so much
And one last edit: That fastsqrt actually makes my code run 20% slower than the library sqrt function haha. Any other ideas?