Aug-29-2018, 09:37 AM
I'm trying to create a function which calculate root using the Newton method, but with small numbers this doesn't work. Is it problem with Python or with my func? I have tried Decimal, __future__ division, nothing helped.
def square_root(num):
x1 = (num * 1.0) / 2.0
x2 = (x1 + (num / x1)) / 2.0
while abs(x1 - x2) >= num:
x1 = x2
x2 = (x1 + (num / x1)) / 2.0
return x2
print(square_root(4))
print(square_root(4e-128))Error:2.0
2.0000000000000003e-64
