x = -37
epsilon = 0.01
num_guess = 0
low = 0.0
high = abs(x)
ans = ((low + high)/2.0)
while abs(ans**3-abs(x)) >= epsilon:
#print("low = " + str(low) + " high " + str(high) + " ans = " + str(ans))
if ans**3 < abs(x):
low = ans
else :
high = ans
ans = ((low + high)/2.0)
num_guess += 1
if x < 0:
ans = -ans
print("Steps taken during bisecction search: ",num_guess)
print("The cube root of " + str(x) + " is " + str(ans))
这是代码示例。我找不到找到浮点数的立方根的方法。不知道在何处插入命令,并且网站不知何故需要更多详细信息,所以这就是为什么我写了这么多的原因
您可以简单地将根写为幂。
例如,x ** (1/3)
给出x
的立方根。