Time：2020-5-21

# 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 transactionCurrency 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 floating-point numbers represented in a computer?

LearnedPrinciples of computer compositionOr something like thatComputer systemAll the students in these courses should know that the storage of floating-point 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 partMantissa partThe information of these three dimensions and the representation of a floating-point number are completely determined, so`float`and`double`The storage structure of these two types of floating-point numbers in the computer is shown as follows:

1. Symbol part

`0`-Positive`1`-Negative

2. Order code part (E) (exponential part)

• about`float`Type B floating point number, index part`8`Bit, considering that it can be positive or negative, so the index range that can be expressed is`-127 ~ 128`
• about`double`Type B floating point number, index part`11`Bit, considering that it can be positive or negative, so the index range that can be expressed is`-1023 ~ 1024`

3. Mantissa part

The precision of floating-point numbers is determined by the number of mantissa:

• about`float`Type floating point number, mantissa part`23`Bit, conversion to decimal is`2^23=8388608`, so decimal precision is only`6 ~ 7`Bit;
• about`double`Type floating point number, mantissa part`52`Bit, conversion to decimal is`2^52 = 4503599627370496`, so decimal precision is only`15 ~ 16`position

So, when floating-point 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.

For example:Decimal`0.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 here`0.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 is`0.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 be`127 +（-1）= 126`, so the binary representation is:`01111110`
• Mantissa part: if`float`For example, should be`23`Position, so the tail is`11000000000000000000000`

Therefore, the final result is as follows`32`Bit accuracy`float`Means):

`00111111011000000000000000000000`

## Another example of complexity

Another example is:Decimal`6.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 is`110.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 be`127 +（2）= 129`, so the binary representation is:`10000001`
• Mantissa part`1001011100...`In fact, it is endless, but if`float`Type precision to intercept`23`Bit, it can be expressed as`10010111000010100011111`

Therefore, the final result is as follows`32`Bit accuracy`float`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.

## Cloud git tutorial – help developers make better use of GIT

Introduction: cloud effect git tutorial – help developers make better use of GIT. Git, as a distributed version control tool, has become more and more popular in recent years. Many companies and developers begin to use git to replace SVN. This article focuses on a deeper skill improvement tutorial on cloud effect for developers using […]