Question

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

What's the rules behind it?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top