The Problem of Numerical Overflow in C Language (Part 2)

Question: What is the correspondence between binary decimals and decimal decimals?

    

[Note] A binary decimal must correspond to a decimal decimal, but a decimal decimal does not necessarily have a binary decimal corresponding to it. That is to say, there is not a one-to-one correspondence between a binary decimal and a decimal decimal: two adjacent binary numbers decimal corresponding decimal is not continuous, but there is a certain interval. we 2^-23and 2^-22two binary fraction, for example, located 2^-23and 2^-22two binary decimal value between decimal no corresponding binary fraction, Only one of these two binary decimals can be used as an approximate replacement (the mantissa is 23 digits): This means that when decimal decimals are converted to binary decimals, they are not all accurate. Because the decimal decimals corresponding to binary decimals are discrete, so There will be a situation where multiple approximately equal decimal decimals are represented by the same binary decimal. Therefore, we say that floating-point numbers are not real numbers in the true sense, they are only approximate expressions within a certain range.

[Note] Due to the limitation of the representation precision of floating-point numbers, we cannot directly compare the equality of two floating-point numbers (only the approximate equality of two floating-point numbers can be compared).

[Note] Not all real numbers can be accurately expressed with finite mantissas.

    When we assign a decimal decimal number to a single-precision real variable, we have to go through a process of converting from a decimal decimal number to a binary decimal number (the information is stored in binary form in the memory), because not every decimal decimal number has the same The corresponding binary decimal, so there may be loss of data accuracy during the conversion process. When the binary decimal is output in decimal form, since each binary decimal has a corresponding decimal decimal, the binary decimal is converted to a decimal decimal During the process, there will be no loss of data accuracy. But at the beginning of the process of converting decimal decimals to binary decimals, there may have been loss of data accuracy, so the final output decimal fraction may not be the same as the initial value Exactly the same. 

Question: Since floating-point data has a wider range of representation, why not directly replace integers with floating-point data?

    The problem of data accuracy is an important reason why floating-point types cannot replace integer types. 

Case analysis of accuracy loss 1:

Case analysis of accuracy loss 2:

    Modifying the variables a and b of the above program to the long type can solve the problem of data precision loss.

    You can also modify the variables a and b to double type, which can also solve the problem of data accuracy loss. But doing so will consume a lot of memory space. In addition, the operation speed of integer data is much faster than floating-point data. 

[Note] The float type can only guarantee 7 significant digits (after 7 digits are inaccurate), and the double type can only guarantee 16 significant digits (after 16 digits are inaccurate).

[Note] If a variable is represented by an integer type, it is sufficient. At this time, avoid using floating-point types (especially when adding and subtracting a large number and a small number). 

 

 

Guess you like

Origin blog.csdn.net/weixin_42048463/article/details/115052821