1

In Python, the principle behind integer division, which is denoted by two forward slashes //, is to divide one integer by another, and rather than giving the exact answer, it rounds down the answer to an integer. I can understand that part.

print(8//5)
# output : 1

But why does the output differ when the first number is negative?

print(-8//5)
# output : -2
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 2
    https://stackoverflow.com/questions/5535206/negative-integer-division-surprising-result – konstantin durant Jun 16 '22 at 14:32
  • 1 is the nearest integer below 1.6, but -2 is the nearest integer below -1.6. – Erik Blomgren Jun 16 '22 at 14:35
  • It is related to remainder(Modular Arithmetic) properties. In python `8/5` gives ` `1.6` and then it performs a floor operation. So 1.6 becomes 1. But in case of negative numbers, floor of -1.6 is -2. (Closest Integer Smaller than it) – Tinfinity Jun 16 '22 at 14:51

0 Answers0