Python 2.0 gives -9/4 as -3? shouldnt it be -2? -
i using python 2.0. tried doing -9/4 , expecting answer -2. however, returns -3. -9/4 -2.25 , thought int type round -2 instead of -3. when int(-2.25), returns -2. can please explain why int(-9/4) returns -3 int(-2.25) returns -2?
the 2 operations have different rounding modes. int() documentation, (reference)
if x floating point, conversion truncates towards zero.
whereas division rounds down, (reference)
plain or long integer division yields integer of same type; result of mathematical division ‘floor’ function applied result.
Comments
Post a Comment