Why are not these two statements equivalents?
>> math.pow(-2,2)
4.0
>> -2 ** 2
-4
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
The order of execution of the operators (operator precedence) matters here: with -2**2
, the exponentiation of 2 to the power 2 is first executed, then the negative sign.
Use parenthesis to obtain the desired result
(-2)**2 = 4
You can check the precedence from the Python3 documentation.
-2**2
calculates as : -(2**2)
= -4
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With