This code gives pretty accurate result when deltanum = 0.0000000000001
but gets into an infinite loop when deltanum = 0.00000000000001
(adding another zero into deltanum
).
It occurs only for non-perfect cubes, it works fine for perfect cubes like 1000. Why?
I am new to programming, following OSSU.
num = 100
high = num
low = 0
icount = 0
cuberoot = (high + low)/2 #cuberoot of num
deltanum = 0.00000000000001
while abs(cuberoot**3 - num)>=deltanum:
icount+=1
print(icount)
if cuberoot**3 > num:
high = cuberoot
elif cuberoot**3 < num:
low = cuberoot
else:
break
cuberoot = (high + low)/2
print("Cube root: " + str(cuberoot))
print("Number of iterations: " + str(icount))