-2

In Python, 6/-132 gets an answer of -1, but should that be a 0?

What's the rules behind it?

Andrew Brēza
  • 6,751
  • 3
  • 32
  • 39
can.
  • 1,956
  • 8
  • 28
  • 40

1 Answers1

1

Python floors the result, which means that 1/2 floors to zero, but 1/-2 floors to -1. This is different than C, which 'truncates toward 0'. AFAIK, most languages follow C. Python uses different rules to keep division 'in sync' with modulo. This article does a good job of explaining.

http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html

zanerock
  • 2,544
  • 2
  • 25
  • 31