Why is -2 ** 2 == -4, but math.pow (-2, 2) == 4.0?

Why are these two statements not equivalent?

>> math.pow(-2,2)
4.0
>> -2 ** 2
-4

Python 3.5.3 (default, January 19, 2017, 14:11:04)

+4
source share
3 answers

Here the order of executing the operators is performed (operator priority): at -2**2first, raising to the power 2 of power 2 is performed, then the negative sign.

Use parentheses to get the result you want.

(- 2) ** 2 = 4

+6
source

since the priority “-” is behind the priority “**”, use (-2) ** 2 to calculate -2 first

+1
source

Python3.

-2 ** 2

: -(2**2)= -4.

0

Source: https://habr.com/ru/post/1684578/


All Articles