Modulus and redundancy

Source: Internet
Author: User
Tags modulus rounds

Always thought that the mold and the remainder is one thing, found that the two are different. The following information is reproduced on the Internet:

In general, modulo operations (mods) and Redundancy (REM) operations are confused because in most programming languages, the '% ' notation is used to denote modulo or remainder operations. Here to remind you to pay great attention to the specific meaning of the '% ' operator in the current environment, because in the case of a negative existence, the results are different.

For the integer number, a, the method of modulo operation or redundancy operation is:

1. Seek integer quotient: c = A/b;

2. Calculate the modulus or number: R = a-c*b.

The modulo operation and the remainder operation are different in the first step: when the modulo operation takes the value of C, it rounds to the 0 direction (fix () function), while the remainder operation calculates the value of C and rounds (the floor () function) to the infinity direction.

Therefore, when the A and B symbols are consistent, the value of the C values obtained by the modulo operation and the remainder operation is consistent, and the result is consistent. But when the symbols are inconsistent, the results are different. The symbol of the result of the modulo operation is consistent with B, and the sign of the result of the remainder operation is consistent with a.

In the C language, the% symbol represents the remainder operation, and in the Python script,% represents the modulo. (Generally, B is not allowed to be negative in the modulo operation, but in Python 2.5.1 can be followed by a negative number in%, because the result of division in the Python language is rounded to 0, so the result is modulo!) )

The following table is a typical modulus or redundancy value.

A B C Language: A%b (for redundancy) Python shell:a%b (modulo)
-3 -5 -3 -3
-3 4 -3 1
-3 2 -1 1
-1 6 -1 5
-4 -3 -1 -1
2 4 2 2
5 3 2 2
4 -7 4 -3
4 -3 1 -2
-6 -5 -1 -1

(turn) to die and to seek redundancy

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.