这是我注意到的一些奇怪的事情。
-5**2 给出 -25 而 math.pow(-5,2) 给出 25 的任何特定原因?答案应该是25,那么为什么会出现-25呢?
>>> -5**2
-25
>>> -5**4
-625
>>> 5**2
25
>>> 5**4
625
>>> import math
>>> pow(-5,2)
25
>>> pow(-5,4)
625
>>>
最佳答案
在 Python 中,**
运算符有 higher precedence比 -
运算符,所以在你的表达式中 5 ** 2
首先被评估,然后被否定。您可以先使用括号应用否定:
>>> -5**2
-25
>>> (-5)**2
25
关于python - 为什么 `-(num)**(even_number)` 结果是 `-(num^(even_number))`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36733958/