【问题标题】:How to take the cube root of floats using python如何使用python获取浮点数的立方根
【发布时间】: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))

这是代码示例。我找不到找到浮点数立方根的方法。不知道在哪里插入命令,不知何故网站需要更多细节,所以这就是我写这么多的原因

【问题讨论】:

  • 要取立方根,您可以使用powcube_root_of_x = pow(x, 1.0/3)
  • 这能回答你的问题吗? How to find cube root using Python?
  • 嗯,这个程序是针对整数的,而我的问题是如何取 0 的数字的立方根。

标签: python cube bisection


【解决方案1】:

你可以简单地把根写成幂。

例如,x ** (1/3) 给出了x 的三次根。

【讨论】:

  • 但是你看这是OCW的开放课程课件给的挑战。他们建议我们应该对算法进行更改,而不是定义函数。
猜你喜欢
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 2020-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 2017-10-17
相关资源
最近更新 更多