-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
  • 7,705
  • 3
  • 34
  • 40
can.
  • 2,098
  • 8
  • 29
  • 42

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
  • 3,042
  • 3
  • 26
  • 35