Rahul Verma
Rahul Verma

Reputation: 3176

Negative Number power infinity is not giving correct when Using 'float'

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

Answers (1)

Adam.Er8
Adam.Er8

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

Related Questions