0
-10 // 3 = -4

 10 // -3 = -4

-10 % 3 = 2

 10 % -3 = -2

I don't get the logic of these operations.

10 // 3 = 3, but -10 // 3 = -4 

which I cant make sense of.

Please, explain.

user134627
  • 43
  • 1
  • 3
  • 2
    [From the docs](https://docs.python.org/3.4/reference/expressions.html#binary-arithmetic-operations): The floor division and modulo operators are connected by the following identity: `x == (x//y)*y + (x%y)` – Blender Jun 22 '14 at 20:56
  • http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html – Bill Lynch Jun 22 '14 at 20:58

2 Answers2

1

This is Because python rounds down. 3.33333 rounded down is 3, wheras -3.33333 rounded down is -4. If you want it to round up, do floating point division, then convert the float to an int.

CrazedCoder
  • 294
  • 3
  • 14
1

The operator // is the floor division operator.

The result of -10/3 is -3.3333. Then, the result of -10//3 will be rounded to the next integer (lower than the result), so the result will be -4.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73