I have been working for two years, but I still don’t know how to convert floating-point number to binary number?

Time:2020-5-21

I have been working for two years, but I still don't know how to convert floating-point number to binary number?


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.

I have been working for two years, but I still don't know how to convert floating-point number to binary number?

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:

I have been working for two years, but I still don't know how to convert floating-point number to binary number?

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, sofloatanddoubleThe storage structure of these two types of floating-point numbers in the computer is shown as follows:

I have been working for two years, but I still don't know how to convert floating-point number to binary number?

I have been working for two years, but I still don't know how to convert floating-point number to binary number?

1. Symbol part

0-Positive1-Negative

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

  • aboutfloatType B floating point number, index part8Bit, considering that it can be positive or negative, so the index range that can be expressed is-127 ~ 128
  • aboutdoubleType B floating point number, index part11Bit, 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:

  • aboutfloatType floating point number, mantissa part23Bit, conversion to decimal is2^23=8388608, so decimal precision is only6 ~ 7Bit;
  • aboutdoubleType floating point number, mantissa part52Bit, conversion to decimal is2^52 = 4503599627370496, so decimal precision is only15 ~ 16position

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.

Let’s start with a simple example

For example:Decimal0.875How 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.875The 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:

I have been working for two years, but I still don't know how to convert floating-point number to binary number?

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:

I have been working for two years, but I still don't know how to convert floating-point number to binary number?

Therefore, we can get:

  • Sign bit0
  • Order code (E) part: iffloatFor example, should be127 +(-1)= 126, so the binary representation is:01111110
  • Mantissa part: iffloatFor example, should be23Position, so the tail is11000000000000000000000

Therefore, the final result is as follows32Bit accuracyfloatMeans):

00111111011000000000000000000000

Another example of complexity

Another example is:Decimal6.36How 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:

I have been working for two years, but I still don't know how to convert floating-point number to binary number?

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:

I have been working for two years, but I still don't know how to convert floating-point number to binary number?

Therefore, we can get:

  • Sign bit:0
  • Order code (E) part: iffloatFor example, should be127 +(2)= 129, so the binary representation is:10000001
  • Mantissa part1001011100...In fact, it is endless, but iffloatType precision to intercept23Bit, it can be expressed as10010111000010100011111

Therefore, the final result is as follows32Bit accuracyfloatMeans):

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

I have been working for two years, but I still don't know how to convert floating-point number to binary number?

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.

Recommended Today

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 […]