- 2021-09-03 21:45
*views 1*- C In depth analysis of language
- C language

<> 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 ）

Technology

- Java431 articles
- Python263 articles
- Vue127 articles
- Linux122 articles
- MySQL104 articles
- javascript78 articles
- SpringBoot72 articles
- C++69 articles
- more...

Daily Recommendation

©2019-2020 Toolsou All rights reserved,

java Comparing attribute values between two objects utilize Python handle Excel data ——xlrd,xlwt library Bidirectional linked list Why? Python Not a future oriented programming language ?Python【 Assignment statement 】 Special lecture , You can't just a=b ah ! Suggest mastering ! utilize Python handle Excel data ——pandas library see SQL-SERVER Data volume and occupied space of database and each table PID The algorithm finally figured out the principle , It was so simple web Two front-end practical games （ Attached source code ） Beginners learn Python Be sure to know what his basic algorithms are ? What is the function ?