0

I am new to python, I know that the operator // is integer division, that is it removes the reminder. but when I executed the following code:

print 15 // -4

I thought the answer would be -3
but instead the answer was -4
please can anybody explain to me why?

Anas Shaikhany
  • 65
  • 1
  • 1
  • 5
  • 1
    afaik it always picks the closest integer that is *lower* than the result one would obtain with floating point. – Willem Van Onsem Jan 29 '17 at 23:11
  • It’s to preserve an identity: `(15 // -4) * -4 + 15 % -4 == 15` – Ry- Jan 29 '17 at 23:15
  • Question is closed. afaik means "*as far as I know*", and [this blogpost](http://python-history.blogspot.be/2010/08/why-pythons-integer-division-floors.html) might be of interest for you. – Willem Van Onsem Jan 29 '17 at 23:16
  • `//` _isn't_ integer division, it's _floor_ division. Thus `16.0 // 3.0` gives a `float` result, even in Python 2. FWIW, there's a neat trick to get ceiling division: `-(-a // b)`. – PM 2Ring Feb 01 '17 at 05:34

0 Answers0