Come on, sit down and talk
Previously, in the previous article “the boss said: who wants to use double to define the amount of goods, just pack up the things and go”, he had a heartbreaking chat:
Dealing with things likeOrder transaction、Currency calculation, andCommodity amountBe careful with floating point numbers（
double
/float
）To define variables, otherwise you may encounter all kinds of wonderful problems. Specific examples are shown in detail in that article.
At that time, when I wrote that article, I thought that everyone was interested inConversion from decimal to binaryIt has been known for a long time, so there is no specific conversion process. As a result, after the article was sent out, a small group of partners in the private letter fed back that it was better to explain in detail how the decimal to binary examples in the article were converted.
Well, here we are!
By the way, it seems that the course “principles of group planning” or “computer system” needs to be rebuilt on a small scale (funny).
But to be honest, crud, copy and paste, interface adjustment, and business code writing have been a long time, and many computer foundations have been forgotten… I also have a deep feeling!
How are floatingpoint numbers represented in a computer?
LearnedPrinciples of computer compositionOr something like thatComputer systemAll the students in these courses should know that the storage of floatingpoint numbers in computers followsIEEE 754 floating point counting standard, which can be expressed as:
useMantissa + order codeThe way of coding, more generally speaking, is similar to what we learned in mathematics textbooksScientific countingRepresentation:Significant number + index bit！
Therefore, just give:Symbol (s)、Order code part、Mantissa partThe information of these three dimensions and the representation of a floatingpoint number are completely determined, sofloat
anddouble
The storage structure of these two types of floatingpoint numbers in the computer is shown as follows:
1. Symbol part
0
Positive1
Negative
2. Order code part (E) (exponential part)：
 about
float
Type B floating point number, index part8
Bit, considering that it can be positive or negative, so the index range that can be expressed is127 ~ 128
 about
double
Type B floating point number, index part11
Bit, considering that it can be positive or negative, so the index range that can be expressed is1023 ~ 1024
3. Mantissa part：
The precision of floatingpoint numbers is determined by the number of mantissa:
 about
float
Type floating point number, mantissa part23
Bit, conversion to decimal is2^23=8388608
, so decimal precision is only6 ~ 7
Bit;  about
double
Type floating point number, mantissa part52
Bit, conversion to decimal is2^52 = 4503599627370496
, so decimal precision is only15 ~ 16
position
So, when floatingpoint numbers are handed over to the computer for storage, there may beLoss of accuracyQuestion!!! Therefore, you need to be very careful when using. If there is a bug in this part, it is very difficult to locate the problem, so you need to do a good job in prevention.
Calculation case of base conversion
What is mentioned above is the content stipulated in IEEE standard, which belongs to the theoretical stipulation. How to convert a decimal to binary? We have to takePractical examplesTo explain.
Let’s start with a simple example
For example:Decimal0.875
How to convert it to binary?
There are several big steps:
1. Divide by decimal point
2. Integer part conversion
I think you should be familiar with integer to binary conversionDivision by 2Just. And here0.875
The integer part is 0, no operation is required.
3. Decimal part conversion
The conversion of decimal part is different from that of integer part“Multiply 2 to round”, as shown below:
4. Consolidated results
Integer part + decimal part
, the final binary result is0.111
。
So the result is as described in the previous sectionMantissa + order codeThe computer counting method of can be expressed as:
Therefore, we can get:

Sign bit：
0

Order code (E) part: if
float
For example, should be127 +（1）= 126
, so the binary representation is:01111110

Mantissa part: if
float
For example, should be23
Position, so the tail is11000000000000000000000
。
Therefore, the final result is as follows32
Bit accuracyfloat
Means):
00111111011000000000000000000000
Another example of complexity
Another example is:Decimal6.36
How to convert it to binary?
I don’t want to write words if I can use diagrams, so I can explain it clearly with a single diagram:
Integer part + decimal part, so the final binary result is110.01011100...
。
As described in the previous sectionMantissa + order codeThe computer counting method of can be expressed as:
Therefore, we can get:
 Sign bit：0

Order code (E) part: if
float
For example, should be127 +（2）= 129
, so the binary representation is:10000001

Mantissa part：
1001011100...
In fact, it is endless, but iffloat
Type precision to intercept23
Bit, it can be expressed as10010111000010100011111
Therefore, the final result is as follows32
Bit accuracyfloat
Means):
01000000110010111000010100011111
So it is necessary to use computer to store and intercept the mantissa of infinite digits, and there must be someLoss of accuracy! Therefore, it also fundamentally explains why the risk of using float or double data should be considered in combination with actual business rationality.
Blessing of artifact
If you are not sure about the above calculation results, or want to check whether the results of manual conversion are correct, there are also directBinary conversion tool stationFor example, binary convert
Do not want to manually convert, directly to the above input, conversion can get the results, and can be interchanged base, very convenient to use.
aftertaste
Sometimes it’s really interesting to review the basics. For example, when writing this article, although it’s a very basic thing, the process of expression is quite interesting, especially the process of drawing and showing. I hope I can share with my friends.