Reputation: 11686
I just came across this behavior but have not seen it explained anywhere. I would think float('-inf')
should be less than 0.
>>> cmp(0, float('-inf'))
1
Upvotes: 0
Views: 80
Reputation: 70572
It might help to know that cmp(a, b)
is essentially the sign of a-b
. So it's +1 if a > b
, -1 if a < b
, and 0 if a == b
.
Upvotes: 1
Reputation: 1121176
float('-inf')
is smaller than 0
.
cmp()
returns a positive value when the first argument is greater than the second. Since float('-inf')
is smaller than 0
, that also means that 0
is greater than float('-inf')
so you would expect cmp()
to return 1
.
From the cmp()
documentation:
Compare the two objects x and y and return an integer according to the outcome. The return value is negative if
x < y
, zero ifx == y
and strictly positive ifx > y
.
and also simply verified:
>>> cmp(0, -1)
1
Upvotes: 7