Reputation: 21
I am using Python 2.0. I tried doing -9/4
and was expecting the answer to be -2
. However, it returns -3
. Now -9/4
is -2.25
and I thought int type would round it up to -2
instead of -3
. When I do int(-2.25)
, it returns -2
. Can anyone please explain why int(-9/4)
returns -3
but int(-2.25)
returns -2
?
Upvotes: 2
Views: 205
Reputation: 213508
The two operations simply have different rounding modes. From the int()
documentation, (reference)
If x is floating point, the conversion truncates towards zero.
Whereas division rounds down, (reference)
Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the ‘floor’ function applied to the result.
Upvotes: 2