Search code examples
pythoninteger-divisionfloorceil

Python division operator gives different results


In Python I am trying to divide an integer by half and I came across two different results based on the sign of the number.

Example:

5/2 gives 2
and 
-5/2 gives -3

How to get -2 when I divide -5/2 ?


Solution

  • As of this accepted answer:

    > int(float(-5)/2)
    -2
    
    > int(float(5)/2)
    2