What determines the sign of m % n for integers?
问题 The modulo in Python is confusing. In Python, % operator is calculating the remainder: >>> 9 % 5 4 However: >>> -9 % 5 1 Why is the result 1 ? and not -4 ? 回答1: Because in python, the sign matches the denominator. >>> 9 % -5 -1 >>> -9 % 5 1 For an explanation of why it was implemented this way, read the blog post by Guido. 回答2: -10 % 5 is 0, ie, -10 is evenly divided by 5. You ask why -9 % 5 is not -4, and the answer is that both 1 and -4 can be correct answers, it depends on what -9 divided