Search code examples
pythonpython-2.7integer-division

Why division value of -ve int is different from +ve one?


I tried this over Python. I am aware that using ints and doing their division will only return the integer value of quotient, truncating the decimal part. On this note:

3 / 2 = 1

Sounds perfectly all right... However when the dividend is negative, then it's mind boggling for me to understand what's happening behind the scenes ??

-3 / 2 = -2

Here's the output:

enter image description here


Solution

  • The integer division there is just taking the floor of the number obtained at the end.

    3/2  -> floor(1.5)  ->  1
    -3/2 -> floor(-1.5) -> -2