The calculation of the modulus (redundancy) in the real range: How to find the negative number

Source: Internet
Author: User
Tags modulus

Background

Recently, in a Java exercise, you see such a problem:

What's the output when this statement executed:
System.out.printf (-7% 3);

We are all familiar with the calculation of positive integers, but for the calculation of negative numbers and real numbers, it really gives people a very fresh feeling. So I did some research on this. I have found that there is still quite a bit of something to explore.

Explore

First, take a look at the modulo operation of the natural number ( definition 1):

if a and d are two natural numbers, d nonzero, can prove that there are two unique integers  q   and  r , satisfies  a  = qd  + r   0≤ r  < d . Among them, q   called quotient, r   is called remainder.

Is it possible to follow such a definition for negative numbers? We find that if we ask for the result of ( -7) MoD 3 According to the rule of positive redundancy, we can say that 7 is (-3) * 3 +2. Of these, 2 is the remainder, and 3 is the quotient.

So, do the various programming languages and calculators follow this understanding? The following are some of the software's understanding of this.

language Statement Output
C + + (g++ compilation) cout << (-7)% 3; -1
Java (1.6) System.out.println ((-7)% 3); -1
Python 2.6 (-7)% 3 2
Baidu Calculator ( -7) MoD 3 2
Google Calculators ( -7) MoD 3 2

As you can see, the results are particularly interesting. This question is a matter of contention . It seems that we cannot directly add the law of positive numbers to negative numbers. In fact, in the range of integers , the law of natural numbers is not accepted by many people, and most of us agree with the definition below 2.

If a and d are integers ,d is nonzero, then the remainder R satisfies such a relationship:

A = qd + R, Q is an integer, and 0≤|r| < |d|.

As you can see, this definition leads to negative redundancy, which is not as simple as we might think, for example, 1 and 2 are ( -7) mod 3 correct results, because these two numbers are in accordance with the definition. In this case, there may be two numbers that can meet the requirements for the modulo operation. We call 1 and 2 respectively the positive and negative remainder . Typically, when divided by D, if the positive remainder is R1 and the negative remainder is R2, then there is

R1 = R2 + D

An ambiguous definition of the negative remainder can lead to serious computational problems, and a bad choice can lead to serious consequences for a system that handles critical tasks.

After reading ( -7) MoD 3, let's take a look at the case of 7 mod (-3) (see Clearly, preceded by 7 with a minus sign, now 3 with minus). by definition 2, 7 = (-3) * (-2) + 1 or 7 = (-3) * (-3)-2, so the remainder is 1 or-2.

language Statement Output
C + + (g++ compilation) cout << 7% (-3); 1
Java (1.6) SYSTEM.OUT.PRINTLN (7 (-3)); 1
Python 2.6 7% (-3) -2
Baidu Calculator 7 MoD (-3) -2
Google Calculators 7 MoD (-3) -2

We have seen a few interesting phenomena:

    • Java followed the pace of C + +, while Python, Google, Baidu Unison. Are they really birds of a feather? Lenovo, Google has been supporting Python,python is also quite a sense of Web features, and Google Application engine also used Python, the domestic search engine is also coincidentally in accordance with Google's definition of the operation.
    • It can be inferred that C + + and Java usually try to make the quotient larger. For example, in ( -7) MoD 3, they have a quotient of-2 and the remainder is-1. In Python and Google calculators, as much as possible to make the business smaller , so 3 as the quotient. The same effect in 7 mod (-3): C + + chose 3 as the quotient, Python chose 2 as the quotient. But in a positive integer operation, all languages and calculators follow the principle of trying to keep the quotient small, so 7 mod 3 results are 1 without controversy, and no one will say that its remainder is-2.
    • If according to the 2nd inference, we test ( -7) mod (-3), the result should be the previous set of languages (C++,java) return 2, the latter group returns-1. (Please note that this is just a hypothesis)

So I did the actual test:

language Statement Output
C + + (g++ compilation) cout <<-7% (-3); -1
Java (1.6) System.out.println (-7% (-3)); -1
Python 2.6 -7% (-3) -1
Baidu Calculator -7 MoD (-3) -1
Google Calculators -7 MoD (-3) -1

The results were so surprising that all languages and computers returned in exactly the same results.

Summarize

We can then summarize the following two conclusions:

    1. For any of the two integers of the same number , the result of the remainder is not disputed, and all the principles of the operation of the language are to make the quotient as small as possible .
    2. For the two integers of the c++/java, the principle of the language is to make the quotient as large as possible, and many new languages and web calculators are the principle of making the business as small as possible.
Expand

Finally, the expansion of time . For real numbers , we can also define modulo operations ( definition 3).

When A and d are real numbers, and d is nonzero, a divided by D will get another real number (quotient), with no so-called remaining numbers. However, if the requirement quotient is an integer, the concept of the remainder is still necessary. It can be proved that the existence of a unique integer quotient Q and a unique real number R makes: a = qd + R, 0≤r < |d|. (go from Wikipedia)

As the definition of extending the remainder in the real range is not important in mathematical theory, many programming languages implement this definition . As to which programming language implements this definition, let everybody explore it yourself!

14 Total Reviews
    • Anonymous said:December 2, 2014 17:45

      Take 0 as the starting point, go on both sides of the axis, Java is not going to come back, such as -7%3, first walk to 6 and then go to-7, the other is gone through the back, first to 9 and then rewind-7

The calculation of the modulus (redundancy) in the real range: How to find the negative number

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.