It's a question that's likely to be asked in an interview , After online access to a variety of information 、 Articles, etc , There are descriptions of this problem . But the details and some key points need to be carefully studied and pondered to understand . In the process of figuring out this problem, we have experienced a lot of ideological struggles , So it's still necessary to record and share your insights .
First , Be clear about all the calculations in the program , Going to the bottom of the computer is binary Computing , and , Binary computation has no concept of subtraction , The subtraction will be calculated by adding a negative number .
The whole calculation process goes through the following steps ：
Back to the question itself , Let's start with how to put 0.1 Convert it to the underlying storage of the computer ,0.2 The idea of the transformation is clear . How to convert decimal to binary , Reference resources Novice tutorial that will do , No more details here .0.1 The conversion to binary is like this ：
Go back and you'll find , This process is an infinite loop state . Never mind that , Write it down first ：
1.1(0011)… * 2-4（ Move the decimal point to the right 4 position , The base of binary is 2）
OK, The next question is , The computer 64 How do bits store scientific notation ？
This icon shows 64 Bit binary different parts of the stored data label . First , The highest bit is the sign bit , So it can't participate in the task of storing data , The subsequent 11 position （ Index part ） Binary number used to store the index in scientific notation , remainder 52 position （ Mantissa part ） It is used to store the decimal point of mantissa in scientific notation 52 position . The sign bits and mantissa of these three parts are very clear , Sign bit 0 It means a positive number ,1 A negative number ; The mantissa part of science is just the mantissa part 52 position ; The determination of the index part is a little more complicated .
Memory gives 11 Bit binary gives exponent , therefore ,11 If bit binary is converted to decimal , The range of data that can be stored is ：[0, 211], namely
[0, 2048]. But there's a problem , The index can also be negative ！ How do negative numbers mean ？ Don't 11 The bit has to give up another bit to represent the sign bit ？ Not at all , In this part of the processing ,IEEE754 The standard index is 0 The cardinal number of is 1023（ With 1023 As the dividing line between positive and negative numbers ）, It's equivalent to being able to store
[-1023, 1024] The number of this range . therefore , With 0.1 Take the index converted to scientific notation as an example , Index -4 Will be converted into
1023 - 4 = 1019, And then convert it to binary ：
1019 It's converted to binary ：1111111011. therefore ,0.1 Before the final binary 12 Position as ：
Next , The mantissa is reserved for 52 position , What about infinite loops ？ It's time to Rounding operation , follow 0 House 1 The principle of entry （ It is similar to the meaning of rounding in decimal system ）, This is also the key point of this issue , Rounding makes the data lose some precision .
You may wonder , A simple calculation can lead to the deviation of the calculation results , So is the result of a slightly more complex operation reliable ？ Of course it's reliable . firstly , It's rare to see an infinite loop in such a transformation , Even if there is , The double precision storage provided by the computer can also ensure that the data operation is within the allowable error range ; second , The bottom layer of the computer has a processing mechanism for precision error calculation , Calculation will not be allowed to go further and further on the road of deviation .
0.1 The final binary form is like this ：
0 01111111011 1001100110011001100110011001100110011001100110011010
0.2 The binary storage of is ：
0 01111111100 1001100110011001100110011001100110011001100110011010
The next step is to do the addition , Binary addition is not difficult , It's like the decimal system . But it's not easy , Before we will 0.1 and 0.2 When it comes to scientific notation , We found that 0.1 My index is -4,0.2 My index is -3. If you want to express the results of their operations in a scientific way , We have to unify the exponents and then extract the common factor for calculation . Here's a question Operations on orders , To minimize the loss of accuracy , We need to follow the small order to the large order （ That is, to convert a smaller index into a larger one ） Principles . In this case , We need to unify the index into -3. therefore ,0.1 After the operation of the order of the binary , That's true ：
0 01111111100 (0.)1100110011001100110011001100110011001100110011001101.
The mantissa needs to be shifted one bit to the right , Move the excess right to round . The integer part omitted by default 1 It's moved to the decimal part , So the integral part becomes 0.
Next, when you do the addition , To complete the integer part omitted before the mantissa , Because when there is carry , The integral part can change , This is also to facilitate the subsequent adjustment of the calculation results .
The result of adding mantissa is ：
There are two problems with this result ：
So adjust the results , First, change the result to “1.” At the beginning , The decimal point is shifted one place to the left , become ：
meanwhile , Add the index to 1： become ：
Last , Still based on 0 House 1 The principle of entry , Put the mantissa part beyond 52 The part outside the bit is rounded , The result is ：
therefore , The complete result is ：
0 11111111101 (1.)0011001100110011001100110011001100110011001100110100
Converting to decimal is ：
thus , All the steps and detailed analysis of this problem have come to an end .