문제

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