python上负数的三次根 [英] Cubic root of the negative number on python
本文介绍了python上负数的三次根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮我找到有关如何使用python计算负数的立方根的解决方案吗?
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?
推荐答案
您可以使用:
-math.pow(3, float(1)/3)
或更笼统地说:
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屋!
查看全文