Reputation: 3176
Here is the 2 code which should return '-inf' but 2nd is giving only 'inf'. Can any body help me why this is happening
>>> -2**float('inf')
-inf
>>> float(-2)**float('inf')
inf
Upvotes: 1
Views: 112
Reputation: 13393
check out Python's operator precedence table, the **
is evaluated before the unary -
.
so -2**float('inf')
is -(2**float('inf'))
which is -(inf)
which is -inf
.
(-2)**float('inf')
is also inf
just like float(-2)**float('inf')
Upvotes: 4