1

1 // 10 == 0 with integer division so how come -1 // 10 != 0 ?

MPython 3.7.0b3 (v3.7.0b3:4e7efa9c6f, Mar 29 2018, 18:42:04) [MSC v.1913 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> -1 // 10
-1

Is this supposed to happen?

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
VicVer
  • 143
  • 6
  • Basically, it floors the result of the plain division. This happens so that it coheres with the behavior of Python's `%` operator, which will give you a positive result when you take the modulus of a negative number. As an aside, please **always** use the generic [python] tag for all python related questions. This behavior is not specific to Python 3, and honestly Python 3 *is* Python. Python 2 is rapidly approaching it's end of life. – juanpa.arrivillaga Aug 28 '19 at 17:32
  • As for what I meant by "coheres", I meant that the following holds: `a = (a // b) * b + (a % b)`, check out the linked duplicate for more details – juanpa.arrivillaga Aug 28 '19 at 17:38

2 Answers2

2

it is easier to explain like that:

operation // rounds "to the left" integer, i.e.

1//10 -> 0.1 -> 0
-1 //10 -> -0.1 -> -1 (as -1 on the X axis is to the left of -0.1)
Evgeny
  • 4,173
  • 2
  • 19
  • 39
0

Floor division always rounds down.

1 / 10 = .1 

which rounds down to 0.

-1 / 10 = -.1 

which rounds down to - 1.

Mike
  • 1,471
  • 12
  • 17