Modulus and remainder

Source: Internet
Author: User
Tags modulus rounds

I always thought that modulo and remainder are the same thing. I found that the two are different. The following documents are reprinted on the Internet:


In general, MOD and REM operations are confused, because in most programming languages, the '%' symbol is used to represent a modulo or remainder operation. Note the specific meaning of the '%' operator in the current environment, because the results of the two operators are different when a negative number exists.

For integer a and B, the modulo operation or remainder operation methods are as follows:

1. Calculate the integer quotient: c = A/B;

2. computing model or number: r = a-c * B.

The modulo operation and the remainder operation are different in the first step: When the modulo operation is used to obtain the value of C, it rounds to 0 (the fix () function ); when the remainder operation calculates the value of C, it rounds the floor () function to an infinitely small direction ).

Therefore, when the symbols A and B are the same, the result is the same because the modulo operation and the result of the remainder operation are the same. But when the symbols are inconsistent, the results are different. The symbols of the result of the modulus operation are the same as those of B, and the symbols of the result of the remainder operation are the same as those of.

In C, the % symbol represents the remainder operation. In a Python script, % Represents the modulo. (In the modulo operation, B is not allowed to be a negative number, but in Python 2.5.1, it can be followed by a negative number after %, because in Python, the division result is rounded to 0, therefore, the calculation result is a modulo !)

The following table lists some typical modulo or remainder values.

 

A
B
C language: A % B (remainder)
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


Modulus and remainder

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.