[to] the difference between the modulo operation and the remainder operation

Source: Internet
Author: User
Tags modulus

[to] the difference between the modulo operation and the remainder operation

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 remainder: R = a-c*b.

It is commonly used to take modulo or redundancy between positive numbers. The following can not be concerned first.

————————————————————————————————————————————

The modulo operation and the remainder operation are different in the first step: the modulus of The remainder operation is rounded to the 0 direction when the value of C is taken (fix () function) , while the remainder modulo operation rounds (the floor () function) to the infinity direction when calculating the value of C.

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.

In particular, the symbol of the modulo operation result is consistent with B, and the sign of the result of the remainder operation is the same as 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

[to] the difference between the modulo operation and the remainder operation

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.