【发布时间】:2020-04-25 12:29:26
【问题描述】:
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))
这是代码示例。我找不到找到浮点数立方根的方法。不知道在哪里插入命令,不知何故网站需要更多细节,所以这就是我写这么多的原因
【问题讨论】:
-
要取立方根,您可以使用
pow:cube_root_of_x = pow(x, 1.0/3)。 -
这能回答你的问题吗? How to find cube root using Python?
-
嗯,这个程序是针对整数的,而我的问题是如何取 0 的数字的立方根。