How to store float data in memory

Source: Internet
Author: User

Floating-point variables occupy 4 bytes (byte) in computer memory, which is 32-bit. Follow the IEEE-754 format standard.
A floating-point number consists of 2 parts: base m and exponent E.
±mantissax2exponent
(Note that the mantissa and exponent in the formula use binary notation)
The base section uses 2 decimal numbers to represent the actual value of this floating-point number.
The exponential portion occupies a binary number of 8-bit, which can represent a range of 0-255. However, the index should be positive, so the IEEE stipulates that the sub-square to be calculated here is the real index minus 127. So the float index can be from 126 to 128.
The base part is actually a value that occupies 24-bit, because its highest bit is the e bit, so the highest bit is omitted from storage, only 23-bit in storage.
So far, the base section 23 bits plus the exponential portion 8 bits have used 31 bits.   So, as I said earlier, float is a 4-byte 32-bit, so what else does one do? There is, in fact, the highest bit in 4 bytes, which indicates the positive or negative of the floating-point number, when the highest bit is 1 o'clock, is negative, and the highest bit is 0 o'clock, which is a positive number.
Floating-point data is stored in 4 bytes in the format of the following table:
Address+0 address+1 address+2 address+3
Contents seee eeee emmm MMMM MMMM MMMM MMMM MMMM S: Indicates floating-point number plus or minus, 1 is negative, 0 is positive
E: Binary number of values after index plus 127
Base of M:24-bit (store 23-bit only)
Idea: Here is a special case, the floating point is 0 o'clock, the index and base are 0, but the previous formula is not established. Because 2 of 0 is 1, so 0 is a special case. Of course, this special case does not have to be solved manually, the compiler will automatically identify.


In the format above, we take a look at the following 4.5 specific data stored on the computer:
Address+0 address+1 address+2 address+3
Contents 0x40 0x90 0x00 0x00 Next we verify the above The data is represented in the end is not 4.5, so also see its conversion process.
Since floating-point numbers are not stored in direct format, he has several parts, so to convert floating-point numbers, the first thing to do is to separate the values of each part.
Address+0 address+1 address+2 address+3
Format seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
Binary 01000000 10010000 00000000 00000000
16 Binary 40 90 00 00
Visible:
S: 0, is a positive number.
E: The conversion of 10000001 to 10 is 129,129-127=2, that is, the actual exponent portion is 2.
M: For 00100000000000000000000. Here, the left side of the base is omitted to store a 1, with the actual base represented as 1.00100000000000000000000
So here we go. The values of the three parts are carried out, and now we adjust the value of the base part m by the value of the exponent part E. The adjustment method is: If the exponent e is negative, the decimal point of the base shifts to the left, and if the exponent e is positive, the base point shifts to the right. The number of digits moved by the decimal point is determined by the absolute value of index E.
Here, E is positive 2, and using the right Shift 2 is available:
100.100000000000000000000
To the next, the result is 4.5 of the binary floating-point number, he converted to 10 binary to see 4.5, how to convert, see below:
The 100 to the left of the decimal point is represented as (1x22) + (0x21) + (0x20), and the result is 4.
To the right of the decimal point. 100 ... represented as (1x2-1) + (0x2-2) + (0x2-3) + ... with the result of. 5.
The sum of the above two values is 4.5, because S is 0, the use is positive, that is, 4.5.
Therefore, the 16 binary 0x40900000 is a floating-point number of 4.5.

The above is how to convert the binary numbers in the computer store into actual floating-point numbers, see below how to replace a floating-point number into a computer storage format of binary numbers.
For example, 17.625 is converted to float type.
First, convert 17.625 to bits: 10001.101 (0.625 = 0.5+0.125, 0.5 is 1/2, 0.125 is 1/8 if the fractional part is not converted to binary, refer to other books. Then move 10001.101 to the right until the decimal point is only one of the 4 1.0001101 x 2 (because the right shift is 4 bits). At this point our base m and index e are out:
Base section M, since the decimal point must be 1, so the IEEE rules only after the decimal point is good, so here the base is 0001101.
Index part E, actually 4, but must be added 127, solid 131, that is, the binary number 10000011
The symbol part s, because it is a positive number, so S is 0.
In summary, the 17.625 float storage format is:
0 10000011 00011010000000000000000
Convert to 16:0x41 8D 00 00
So, one look, it still takes up 4 bytes.

http://blog.csdn.net/flyingleo1981/article/details/52935431

How to store float data 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.