This article is reproduced in: http://blog.sina.com.cn/s/blog_6f5c63ff0100tucb.html
We know that in different languages, the results of negative number modulo operations may be different. For example, (-11) % 5 calculates 4 in Python, whileThe result calculated in C (c99) is-1.
truncate Division & floor Division
in most programming languages , if integer A is not an integer multiple of integer B, the fractional part of the actual result produced by division A and B will be truncated. This process is called truncatio N ). If the division result is a positive number, the general programming language will cut the result to zero, that is, directly remove the fractional part of the quotient. However, if the division result is a negative number, different languages usually adopt two different tail truncation Methods: one is to truncate toward zero ), the other is truncate toward negative infinity. Correspondingly, the two division methods are called truncate division and floor Division respectively.
In fact, The truncate division ends at zero regardless of whether the division result is negative, while the floor division ends at negative infinity.
modulo operation
the modulo operation is actually the remainder after division of two numbers. Assume that Q is the quotient generated by division A and B, and r is the corresponding remainder (remainder). In almost all computing systems, both meet a = B * q + R, where | r | <| A |. Therefore, R has two options: positive, negative, and Q. If A and B are all positive numbers, R is positive in general programming languages, or if a and B are all negative numbers, R is generally negative. However, if a and B are positive and negative, the R results may vary depending on the Division results, generally, the R calculation method meets the requirements of r = A-(A/B) * B
.
Common languages
(1) c/Java language
The division of C/Java language adopts zero-tail truncation (in fact, the result of c89 is undefined for the case where one of the divisor or divisor is negative; c99), namely, truncate division. Their modulo operator is %, and this operator only accepts integer operands. A rule is that the result symbol of the modulo operation is the same as that of the first operand (or 0 ). Therefore(-11) % 5 =-11-[(-11)/5] * 5 =-11-(-2) * 5 =-1.
(2) c ++ Language
The ending method of c ++ language depends on the specific machine. If both operands are positive, the result of the modulo operation is also positive (or 0). If both operands are negative, the result of the modulo operation is negative (or 0). If only one operand is negative, the result of the modulo operation depends on the specific implementation.
(3) Python language
In python, division is based on negative infinity truncation, that is, floor division. Its modulo operator is also %, and this operator can accept floating-point operands. A similar rule is that the result symbol of the modulo operation is the same as that of the second operand. Therefore(-11) % 5 =-11-[(-11)/5] * 5 =-11-(-3) * 5 = 4
.(So in N % 10, if n is a negative number, the result may be a positive number)
Note that python 3. the meaning of the "/" Operator in X changes, and the results produced by "/" are not truncated. The results of the corresponding "//" operator are truncated.