python上负数的三次方根 [英] Cubic root of the negative number on python
本文介绍了python上负数的三次方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能帮我找到一个关于如何使用python计算负数的三次方根的解决方案?
<预><代码>>>>math.pow(-3, float(1)/3)南它不起作用.负数的三次方根是负数.有什么解决办法吗?
解决方案
您可以使用:
-math.pow(3, float(1)/3)
或更一般地说:
if x >0:返回 math.pow(x, float(1)/3)elif x <0:返回 -math.pow(abs(x), float(1)/3)别的:返回 0
Can someone help me to find a solution on how to calculate a cubic root of the negative number using python?
>>> math.pow(-3, float(1)/3)
nan
it does not work. Cubic root of the negative number is negative number. Any solutions?
解决方案
You could use:
-math.pow(3, float(1)/3)
Or more generally:
if x > 0:
return math.pow(x, float(1)/3)
elif x < 0:
return -math.pow(abs(x), float(1)/3)
else:
return 0
这篇关于python上负数的三次方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文