x=float(raw_input('Enter a number to show its square root'))
precise = 0.01
g=x/2.0
while abs(g**2-x)>=precise:
g=g-(g**2-x)/2*g
print g
This is a python code based on Newton-Raphson Root Finding method. When I run this in Canopy, I can find root of 1. But when i input 25 to find the root, it says OverflowError: (34, 'Result too large')
pointing out to the while abs(g**2-x)>=precise:
line. Help appreciated
Are you sure about your algorithm. Move your print g
inside your while loop and you will see that g
is getting really really big, really fast. Then you are trying to square it. Is your denominator supposed to be 2*g
? If so then you should put parenthesis around it like (2*g)
, because you are dividing by 2
and then multiplying by g
. Probably not what you wanted to do.