This question already has answers here:
Calculation error with pow operator

(4个答案)


已关闭6年。




在Python中
>>> i = 3
>>> -i**4
-81

为什么-i**4不评估为(-i)**4,而是评估为-(i**4)

我猜想有人可能会争辩说,提高幂次要优先于i乘以(隐式)乘以减一(即,您应该阅读-1*i**4)。
但是在我学习数学的地方,-i**n甚至ni都是肯定的,应该表现出积极性。

最佳答案

**运算符的绑定(bind)比Python中-运算符的绑定(bind)更紧密。如果您想覆盖它,可以使用括号,例如(-i)**4

https://docs.python.org/2/reference/expressions.html#operator-precedence
https://docs.python.org/3/reference/expressions.html#operator-precedence

关于python - 在Python中,为什么将负数提高到偶数幂仍保持负数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27726592/

10-11 22:22
查看更多