Re-exploration and redundancy of Java's doubts

Source: Internet
Author: User

Integers don't matter

The key is negative.

C++,java's Redundancy:

The divisor and dividend are obtained by absolute value, and the final symbol follows the divisor.

such as: -7%3=-1;-7% (-3) =-1

Python, etc.,

A%b

1, first seek tmp=|a|%| b|

2. If a<0&&b<0,-tmp

3. If a<0&&b>0,tmp+b

4. If A>0&&b<0,tmp-b

http://ceeji.net/blog/mod-in-real/
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, it can be proved that there are two unique integers q and R, satisfying a = qd + C13>r and 0≤ R < D. whereQ is called quotient,R is called the 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.

2 Td>
statement Span style= "font-size:18px" > output
c++ (g++ compile) cout < < ( -7)% 3; -1
java (1.6) system.out.println ( ( -7)% 3); -1
python 2.6 ( -7)% 3
Baidu calculator Span style= "font-size:18px" > ( -7) mod 3 2
google calculator ( -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.

system.out.println (7% ( -3)); 7% ( -3) Td> Td>
language Statement Output
c++ (g++ compile) cout < < 7 ( -3); 1
java (1.6) 1
python 2.6 -2
Baidu calculator 7 mod ( -3) -2
google calculator 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:

-1 -7 mod ( -3)
statement Span style= "font-size:18px" > output
c++ (g++ compile) cout < <-7% ( -3); -1
java (1.6) system.out.println ( -7% ( -3)); -1
python 2.6 -7% ( -3) -1
Baidu calculator -7 mod ( -3)
google calculator -1

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

Summary

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|. (Transfer 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!



Re-exploration and redundancy of Java's doubts

Related Article

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.