Why is the result of negative number remainder calculation in different programming languages inconsistent?
Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply content: Division and remainder of signed integers
This definition is correct in mathematics, and there are several
For example, press
Or press
The IEEE standard is true, but there are several round standards...
If round is the IEEE default round to the nearest, it seems to be 3 from the standard perspective... However, IEEE does not seem to have said that other round standards are incorrect...
PS: Many languages simply provide two functions. rem and mod are the choices for the quotient to take an integer in the 0 or negative infinity direction. c is required to take an integer to 0 from c99, py requires the negative infinity to be rounded up and selected. it doesn't matter if it's true or not, even different compilers in the same language will be different.
After I find this problem, whenever I update the IDE/compiler, I will run it again using my test program to see if it is different from the previous version.
Sometimes some minor changes are very difficult.
PS: sometimes it does not work. it cannot be guaranteed that the test results are unique. The key to the problem is whether the quotient is the negative infinity value or the 0 value-7/10 = 0 ......-7
Or
-7/10 =-1... 3
I don't know about other languages. it seems that c11 has set the remainder of the negative number to 0. many netizens upstairs have already explained the concept of pure mathematics. I want to talk about my opinion from another perspective. First, let's clarify: we are talking about the situation where "the divisor" (numerator) is a negative integer while the divisor (denominator) is a positive integer.
Let's take a look at an example: (-17) mod 5 =?
Answer 1: (-17) = (-3) * 5 + (-2), so the remainder is-2.
Answer 2: (-17) = (-4) * 5 + (+ 3), so the remainder is + 3.
First of all, from the concept of "pure mathematics", both answers are "right", just like the "plural solution" when the equation is solved. Therefore, various compilers generate the remainder according to their respective logic.
However, from the perspective of human thinking, the origin of "negative numbers" is "deficiency and deficiency". negative numbers are used to know how many defects are there and then remedy them. From this perspective, we can correctly understand the actual meaning of "negative remainder" (different from pure mathematical concepts ).
For example, if you are living in a dormitory with five other students, you are very diligent and the five are very lazy, so that you can buy breakfast, for example, fried dough sticks, soy milk ...... You came back with their breakfast and said to them, "a total of 17 yuan ." That is to say, five of them owe you 17 yuan in total. Each person gives you an average of 17/5 = 3.4 yuan.
So they rummaged through the box-instead of having no money, they were looking for a change, and each of them had to pay 0.4 yuan. well, you may look at the situation and find out if you have 4 cents on your own ...... The result is that no change can be found. what should I do? There are two methods:
Method 1: Each person gives you 3 yuan, but still owes you 2 yuan in total. The remainder =-2
Solution 2: Each person gives you 4 yuan, but you want to give them 3 yuan, the remainder = + 3
Now the question is: Which method do you prefer as a human?
As mentioned above, the reason why humans use negative numbers is to know the "deficiency" and then remedy it. Now that I owe you money to pay back, I think the five will give you four yuan, not to say that they want to pay you three yuan for errands, instead, they thought about "liao (le) this account". they didn't want five people to owe you 0.4 yuan in favor (besides, you are really running errands ), since I have paid back all the money, why do I have to leave a "legacy problem?
So,
In terms of human habits and actual needs(Again: different from pure mathematical concepts), the remainder should be positive regardless of whether the divisor (numerator) is positive or negative,This is in line with human thinking habits and actual needs.
Digress: just as the divisor (denominator) is usually a positive number. if a negative number is used as the divisor, it is difficult to understand.
In fact, if the remainder can be negative, 23% 5 also has two answers, which can be seen as + 3 or-2, but I believe no one will use-2, the first response must be + 3.
Now back to the subject's question: (-7) % 10 =? Is it + 3 or-7?
If we say this, we owe you seven yuan and ten people will pay for it. what should we do?
Method 1: Each person pays you a dollar, and the result is 3 more yuan, with the remainder equal to + 3.
Method 2: Each person returns it to you.
0 RMBThe remainder is-7. In the end, we still owe you 7 yuan ......
Mathematical Language: whether the negative infinity value is 0 ......
======================================================= ==== Split line ==========
Some people may say that mathematics is rigorous, not based on emotion or habits.
I strongly agree with this point of view. for a strictly and rigorous science like mathematics, the same operands and operators will inevitably produce the same calculation result. Therefore, the "positive and negative" dual standard of remainder is a Fatal Error that will destroy the foundation of the math Hall. Imagine that the-7 v and + 3 v are two opposite pulse signals, which is enough to let the space shuttle fall into the sea ......
Take (-7) % 10 as an example. The same theorem is one of the basic theorems of mathematics. The middle school students understand it. let's take a look:
Obviously, according to the remainder theorem, the answer is undoubtedly + 3, not-7. This proves to me the above points.
If we think (-7) % 10 =-7, the remainder theorem that even middle school students understand is not true, and mathematics becomes so fragile and vulnerable.
The remainder theorem is the theoretical basis for computers to represent negative numbers and population numbers. the chaotic results produced by those compilers just show that developers of these compilers do not understand the concept and significance of the remainder. After all, computer and mathematics are different fields.Because mathematics generally does not care about the remainder of a negative number, number theory generally deals with positive integers. It may be that the programmer does not partition the remainder and the modulo, Baidu takes the remainder and the modulo. For reference only, it seems that various programming languages are unified internally ..
Please first experiment with major versions of cl, gcc, clang, cmake, and tc ..
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.