[英]How to find the cube root in Python?
I've tried to find the cube root in Python but I have no idea how to find it.我试图在 Python 中找到立方根,但我不知道如何找到它。 There was 1 line of code that worked but he wouldn't give me the full number.有 1 行代码有效,但他没有给我完整的数字。 Example:例子:
math.pow(64, 1/3)
This doesn't give me 4 tough but 3.99999.这不会给我 4 强,而是 3.99999。 Does anyone know how I am supposed to fix this?有谁知道我应该如何解决这个问题?
You can use the power operator **
with fractions like:您可以将幂运算符**
与以下分数一起使用:
Python3:蟒蛇3:
>>> 8**(1/3)
2.0
Python2:蟒蛇2:
>>> 8**(1.0/3)
2.0
This is one option without using math library这是不使用数学库的一种选择
>>> 64**(1/3)
3.9999999999999996
>>> round(64**(1/3.),2)
4.0
If you want to do with your code, you can apply 'round()' method如果你想处理你的代码,你可以应用'round()'方法
>>>import math
>>>round(math.pow(64,1/3.))
4
in Python 3.11
, math.cbrt
在Python 3.11
, math.cbrt
x = 64
math.cbrt(x)
(or) (或者)
use numpy
使用numpy
import numpy as np
x = 64
np.cbrt(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.