C traps and defects-negative modulo operation

Source: Internet
Author: User
Tags floor division truncated

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.