Negative modulo operation

Source: Internet
Author: User
Tags floor division floor function truncated
Summary: We know that in different languages, the results of negative number modulo operations may be different. For example, (-11) % 5 calculates 4 in Python and-1 in C (c99. Truncate division floor division in most programming languages, if the integer...

We know that in different languages, the results of negative number modulo operations may be different. For example, (-11) % 5 calculates 4 in Python and-1 in C (c99.
  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 truncation ). 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, it can be considered that no matter whether the division result is a negative, the truncate division ends with zero, and the floor division ends with a negative infinity.
  Modulo operation
The modulo operation is actually to calculate the remainder after the 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, r = A-(A/B) * B is used in R calculation.
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.
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.
(4) Common Lisp
The special operator (special operator) "/" of Common Lisp produces scores, so there is no tail truncation problem. However, Common Lisp provides the TRUNCATE function and the FLOOR function, which correspond to the two division methods described above. Correspondingly, the REM function of Common Lisp is similar to the modulo operation in C/Java, while the MOD function is similar to the modulo operation in Python.

 

Reprinted address:Http://www.189works.com/article-12035-1.html

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.