Come on, sit down

Previously in the previous article “the boss said: who wants to use double to define the amount of goods, just pack up their own things and go” has been deeply talked about:

When defining variables with double/float values for things like order transactions, currency calculations, and commodity amounts, you can run into all sorts of weird problems, as detailed in that article.

At the time I wrote that article, I thought everyone knew how to convert from decimal to binary, so I didn’t give you the exact process. As a result, after the article was sent out, a number of friends in the private letter said that in the article those decimal to binary examples how to convert out, the best to explain in detail.

Well, here we are!

By the way, it seems that the course “Principles of Planning” or “Computer Systems” needs to be reinvented in a small way (funny).

But honestly, CRUD, copy and paste, interface adjustment, writing business code for a long time, the computer foundation is really a lot of forgotten… I feel the same way!


How are floating point numbers represented in computers?

For those of you who have taken principles of Computer Composition or similar courses, you may know that floating-point numbers are stored in a computer in accordance with IEEE 754 floating-point counting standards, which can be expressed as:

Adopt mantissa + order code encoding way, more popular point said, is similar to the mathematical textbook on the scientific counting method of expression: significant number + index bit!

Thus, given the three dimensions of the symbol (S), the step part (E), and the mantissa part (M), the representation of a floating-point number is completely established, and the computer representation of float and double floating-point numbers looks something like this:

1. Symbol Part (S)

0-1 – negative

2. Step code part (E) (Index part) :

  • forfloatType floating point number, exponential part8Digit, which can be positive or negative, so the range of exponents that can be represented is- 127 ~ 128
  • fordoubleType floating point number, exponential part11Digit, which can be positive or negative, so the range of exponents that can be represented is- 1023 ~ 1024

3. Mantissa (M) :

The precision of a floating point number is determined by the number of mantissa digits:

  • forfloatType floating point, mantissa part23Bit, in decimal^ 2 = 8388608, so the decimal accuracy is only6 ~ 7A;
  • fordoubleType floating point, mantissa part52Bit, in decimal52 ^ 2 = 4503599627370496, so the decimal accuracy is only15 to 16position

As a result, floating-point numbers may lose accuracy when handed to the computer for storage!! Therefore, you need to be very careful when using it. If you really have a bug in this piece, it is very difficult to locate the problem, so preventive work should be done.


Base conversion calculation case

The above is said to be the content of the IEEE standard, which belongs to the theoretical specification. So how do you convert a decimal into binary? We have to explain it with real examples.

Let’s start with a simple example

For example, how to convert the decimal number 0.875 to binary?

Can be taken in several strides:

1, with the decimal point as the boundary, split

2. Integer partial conversion

Integer to binary I think we should be familiar with: mod by 2 can be used. The integer part of 0.875 is 0 and no operation is required.

3. Fractional conversion

The conversion of the decimal part is different from that of the integer part. The “multiply by two” method is used. The illustration shows that:

4. Merge results

Integer part + decimal part, resulting in a binary result of 0.111.

Therefore, according to the computer counting method of mantissa + order code described in the previous section, the result can be expressed as:

So the corresponding result can be:

  • The sign bit:0
  • Step code (E) partIf the:floatFor example, should be127 plus minus 1 is 126, so the binary representation is:01111110
  • Mantissa part (M)If the:floatFor example, should be23Is, so after completing the tail is11000000000000000000000.

So the final total is (in 32-bit precision float) :

00111111011000000000000000000000

Let’s do a more complicated example

For example, how to convert decimal 6.36 to binary?

I don’t want to write when I can use diagrams, so I can explain it with a picture:

Integer part + decimal part, so the resulting binary result is 110.01011100… .

In accordance with the computer counting method of mantissa + order code described in the previous section, it can be expressed as:

So the corresponding result can be:

  • Sign bit: 0
  • Step code (E) partIf the:floatFor example, should be127 plus 2 is 129, so the binary representation is:10000001
  • Mantissa part (M):1001011100..., in fact, it does not cycle indefinitely, but iffloatType precision to intercept23Bit, can be expressed as10010111000010100011111

So the final total is (in 32-bit precision float) :

01000000110010111000010100011111

So like this infinite mantissa situation, with computer storage interception is inevitable, there will be a certain loss of accuracy! So this is essentially why data of types like float or double are risky to use, and therefore must be combined with practical business considerations.


Artifact blessing

If you are not sure about the above calculation results, or want to check if the manual conversion results are correct, there are also direct binary conversion sites, typically such as Binaryconvert

Do not want to manual conversion, directly to the above input, conversion can get the results, and can be exchanged, the use of very convenient.


aftertaste

Sometimes it is really interesting to review the basics. For example, when writing this article, although it is very basic, the process of expressing it is quite interesting, especially the process of drawing and presenting it. I hope I can share my encouragement with my friends.