C + + Memo (1)

Source: Internet
Author: User

    • Take the mold:

1. Reprinted from: http://ceeji.net/blog/mod-in-real/

1 background2 3 recently, in a Java exercise, you see such a problem:4 5What isThe output when ThisStatement executed:6System. out. printf (-7%3);7 8 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. 9 Ten Explore One  A 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, satisfying a = qd + R and 0≤r <d. where q is called quotient, R is called the remainder.  the  -Is it possible to follow such a definition for negative numbers? We find that if we follow the rules of positive redundancy (-7) MoD3Can be expressed as a result of-7For (-3)*3+2。 of which, 2 is the remainder,-3 is a business.  -  - 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 AJava (1.6) System. out. println (-7) %3); -1 atPython2.6(-7) %3    2 -Baidu Calculator (-7) MoD3    2 -Google Calculators (-7) MoD3    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: in  -A = qd + R, Q is an integer, and 0≤|r| < |d|.  to  +As you can see, this definition leads to negative redundancy, which is not as simple as we might think, for example,1And2All are (-7) MoD3The correct result, 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 put-1And2is called the positive and negative remainder respectively. Typically, when divided by D, if the positive remainder is R1 and the negative remainder is R2, then there is -  theR1 = 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. Panax Notoginseng  -Read it (-7) MoD3, let's take a look at7MoD (-3) (see Clearly, the front is7With a minus sign, now is3with minus sign). According to Definition 2,7= (-3) * (-2) +1or 7 = (-3) * (-3) -2, so the remainder is1Or2.  the  + Language statement Output AC + + (g++ compilation) cout <<7% (-3);1 theJava (1.6) System. out. println (7% (-3));1 +Python2.6    7% (-3)    -2 -Baidu Calculator7MoD (-3)    -2 $Google Calculators7MoD (-3)    -2 $ we have seen a few interesting phenomena: -  -Java follows C + +the pace, 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.  theIt can be inferred that C + + and Java usually try to make the quotient larger. For example, in (-7MoD 3, they take-2For the quotient, the remainder is-1。 In Python and Google calculators, try to make your business smaller, so-3For business. In7MoD (-3) has the same effect: C + + selected3As a business, Python has chosen2As a business. But in a positive integer operation, all languages and calculators follow the principle of keeping the quotient small, so7MoD3Result is1There is no controversy, and no one will say that its remainder is--2.  -If we follow the 2nd inference, we test (-7) MoD (-3), the result should be a previous set of languages (C++,java) returned2, the latter group returns-1. (Please note that this is just a hypothesis)Wuyi so I did the actual test: the  - Language statement Output WuC + + (g++ compilation) cout <<-7% (-3); -1 -Java (1.6) System. out. println (-7% (-3)); -1 AboutPython2.6-7% (-3)    -1 $Baidu Calculator-7MoD (-3)    -1 -Google Calculators-7MoD (-3)    -1 - the results were so surprising that all languages and computers returned in exactly the same results.  -  A Summary +  the we can then summarize the following two conclusions: -  $ 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.  theFor two integers of the same number, c++/The principle of the Java language is to make the business as large as possible, and many new language and Web Calculator principles are to make the business as small as possible.  the Expand the  the Finally, the expansion of time. For real numbers, we can also define modulo operations (definition 3).  -  inWhen 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) the  theAs 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!
View Code

  

2.5%7=5 4%9=4

Decimals In addition to the large number of quotient more than 0 itself

    • If there is no curly brace "{" "}" after the while, the sentence followed by default is the loop body.

C + + Memo (1)

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.