I’m getting a strange result when squaring -1 in idle. What’s going on?
Unexpected result:
>>>| -1 ** 2 >>>| -1
Expected result:
>>>| pow(-1,2) >>>| 1 >>>| my_var = -1 >>>| my_var **= 2 >>>| my_var >>>| 1
Advertisement
Answer
Operator precedence (the -
is a unary minus operator):
>>> -1 ** 2 -1 >>> -(1 ** 2) -1 >>> (-1) ** 2 1