<> Zero rounding , Negative infinite rounding , Positive infinite rounding , Rounding
If you assign a floating-point number to an integer , Only integer digits are saved ：
This rounding method is zero rounding ,C This is the default language
C There is also a corresponding zero rounding function in the language ：
Similarly, another function is negative infinity rounding :
Its rounding scheme is to round to negative infinity ：
Rounding with floor , Of course, there are functions rounded to positive infinity ：
It is rounded to positive infinity ：
last , There is also a function of rounding ：
<> Take mold / Surplus
Modular concept ： If a and d Are two natural numbers ,d Nonzero , It can be proved that there are two unique integers q and r, satisfy a = q*d + r And 0 ≤ r < d. among ,q
Called quotient ,r It is called remainder . therefore q=a/d,r=a%d.
But what if you modulo a negative number ?
stay C In language ：
stay Python in ：
It can be seen that the results of these two kinds of modeling are different .
a=-10,d=3; -10=(-3)*3+(-1) q=-3,r=-1; C language -10=(-4)*3+2 q=-4,r=2; Python
As can be seen from the above example , Specific remainder r Size of , The essence depends on the quotient q of . The quotient depends on the rounding rule when dividing .
Like the one above -10/3=-3.3333, If the quotient is rounded to zero , The result is -3, The remainder is -1, And if the quotient is rounded to negative infinity , The result is -4, The remainder is 2.
These two rounding methods , Is the difference between module and remainder ：
Take remainder or mold , We should calculate the quotient , Then we can get the remainder .
Surplus ： As far as possible , Round to zero .
Take mold ： As far as possible , Round to negative infinity
There is no difference between the two methods when the quotient is positive , Because the result of zero rounding and negative infinite rounding is the same . But if the result of the quotient is negative , Then the results of remainder and modulus are different .
therefore , Two data involved in the remainder , If the same symbol , Modulo is equivalent to remainder , Because quotient is positive , If different symbols , Then the module and remainder are different .
To sum up ,C In language % The essence is to take surplus ,Python Medium % The essence is to take the mold .
And through the above analysis , We can draw a conclusion ：
Specific remainder r Size of , The essence depends on the quotient q of . And business , It depends on the time of division , Rounding rule .
Remainder and mold taking ： The balance shall be taken as far as possible , Round to zero . Take the mold and let the quotient as much as possible , Round to negative infinity .
Two data involved in the remainder , If the same symbol , Modulo is equivalent to remainder .
If the two data symbols participating in the remainder are different , stay C In language ( Or other methods 0 Rounding language such as ：C++,Java), Remainder sign , Same as divisor .（ Because the direction 0 Rounding ）