From the difference between C + + and Python division, we talk about modulo (modulus) and redundancy (remainder)

Source: Internet
Author: User
Tags integer division modulus

A very interesting phenomenon has been found today.

As a division, Python2 and C + + have different divisible results in the case of negative numbers:

When I was 5/3,

Results for C + +: 1

Results for Python2:2

(Note that 5/-3 is still available in C + +-1, Python2-2)

It can be seen that C + + in the negative division of the implementation of the number is directly after the decimal point after the operation, that is, return and 0 is closer to the number.

In Python2, however, the largest integer that is less than or equal to the quotient is returned, which is the number closer to the return and-∞.

When doing the% operation, it is based on this logic:

A = b * C + R

Where a is the divisor, B is the divisor, C is quotient, and R is the result of the% operation.

In the above example, A and B are-5 and 3.

In the case of C + +, quotient c is-1, so r can be calculated to be-2.

In Python case quotient C is-2, so r can be calculated as 1.

!!! It is worth noting that when the operation is changed to 5-3, the result of C + + is that the result of 2,python2 is-1, and the previous result is just the opposite of the sign.

If a classmate thinks it's messy, remember a = b * C + R.

With the same logic, when a is 5,b is-3, C + + because quotient c is-1, so R is 2,python when quotient C is-2 so R is-1.

So what is the cause of this discrepancy?

In fact, because of the difference between "modulus" and "remainder".

Many students may think that it is one thing to find a modulo operation and take the remainder operation. In fact, they do exhibit exactly the same properties within the range of positive integers. But in the case of negative numbers, they behave differently.

The% c + + shows the result of the "take out" operation.

Python's% shows the result of the "modulo" operation.

Whether C + + or Python, both the modulo and the remainder are defined according to the formula A = b * C + R, and the only difference is whether quotient C is rounding in 0 directions or-∞ direction.

When quotient c is rounded to 0, the resulting R is the "remainder", which is the result in C + +.

When quotient c is rounded to the-∞ direction, the resulting r is the "modulus", which is the result in Python2.

In Python3, both sides of the "/" are integers, and the operations are converted to real operations and return real numbers. The integer division is denoted by "//".

With this divisible nature of python, we can get a little trick:

Many times we will encounter "something X a group, total need y, at least how many groups?" "Such a problem.

In C + + we have to write this logic: answer = y/x; if (y% x! = 0) ans + = 1;

In Python we can take advantage of the negative character to get the result easily: Answer =-(-y//x).

From the difference between C + + and Python division, we talk about modulo (modulus) and redundancy (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: 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.