[Algorithmic] how floating-point numbers are stored in memory

Source: Internet
Author: User

Float variable takes 32bit, which is 4 bytes of memory space

Let's first look at the three components of the floating point binary representation.

The three main ingredients are:

    • Sign (1bit): Indicates whether the floating-point number is positive or negative. 0 for positive, 1 for negative
    • Exponent (8bits): Exponential section. Similar to the n in the m*10^n of science and technology law, except that it is based on 2 instead of 10. It is important to note that this section is based on 2^7-1, which is 127, or 01111111 for 2^0, and the conversion needs to be adjusted according to the 127 offset.
    • Mantissa (23bits): Base section. The actual representation of the floating-point number's specific value.

Let's look at a practical example to explain the conversion process.
Step 1 to rewrite the whole number of parts
Take the value 5.2 for example. First, regardless of the exponential part, we first simply rewrite the decimal number into binary.
The integer part is simple, 5.101.

Step 2 rewriting of decimal parts
The decimal part is the equivalent of breaking into a 2^-1 until 2^-n. For example:
0.2 = 0.125+0.0625+0.007825+0.00390625 is 2^-3+2^-4+2^-7+2^-8 .... 00110011001100110011.

Or a more stupid way to interpret the decimal to the binary decimal rewrite conversion, usually the decimal 0.5 also (that is, the score 1/2), equivalent to the binary 0.1 (equivalent to the fractional 1/2),

We can multiply the decimal fractional part by 2, take the integer part as the binary one, and the remaining decimals to multiply by 2 until there are no remaining decimals.

For example 0.2 can be converted to:

0.2 x 2 = 0.4 0

0.4 x 2 = 0.8 0

0.8 x 2 = 1.6 1

0.6 x 2 = 1.2 1

0.2 x 2 = 0.4 0

0.4 x 2 = 0.8 0

0.8 x 2 = 1.6 1

.......

That is:. 0011001 ..... (It is an infinite loop of binary numbers, understand why the decimal decimals converted into binary decimals when the loss of precision is the case?)

Step 3 Normalization
Now we have such a string of binary 101.00110011001100110011. Then we have to normalize it, also called normalize. In fact, the principle is very simple is to ensure that only a bit before the decimal point. So we got the following expression: 1.0100110011001100110011 * 2^2. So far we've done the rewrite, and the next step is to populate the three components with the bit.

Step 4 Fill
Index section (Exponent): Previously said to be adjusted with 127 as an offset. So 2 of the 2-time side, the exponential portion is offset into 2+127 that is 129, expressed as 10000001 filled in.
Integer part (mantissa): In addition to simple filling, the place that needs special explanation is that the integer part 1 in 1.010011 is abandoned at the time of filling. Because the normalized value of the whole part is always 1. Then you may have doubts, is it not 1.010011 and 0.010011 after omitting the integer part? Not really, if you look closely at the latter: you will find that he is not a normalized binary and can be rewritten as 1.0011 * 2^-2. So omitting a bit before the decimal point does not cause any confusion between the two floating-point numbers.
The results of the specific filling are shown in

Exercise: If you want to test whether you fully understand this section, you can write a floating point to try to convert. The floating-point binary conversion tool allows you to verify the answer.

[Algorithmic] how floating-point numbers are stored in memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.