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

What's the rules behind it?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top