Trouble with float on C [duplicate]
I have this little program in C that calculates the square root x
of a positive integer N
using a recursive function (implemented using a while loop). If I calculate x
using this:
x = (1/2)*(x + N/x) //x0 = 1.0
Then x
keeps growing to inf and then nan. However if I use this:
x = (x + N/x)/2 //x0 = 1.0
It works fine, why? Thanks.
1/2
does integer division, its result is 0
, change either or both operand to double
, e.g:
1.0/2