Floating Point variables occupy 4 bytes (in bytes) in the computer memory, that is, 32-bit. Complies with IEEE-754 format standards.
A floating point number consists of two parts: base number M and exponent e.
± Mantissa × 2 Exponent
(Note: mantissa and exponent in the formula are represented in binary format)
The base part uses a binary number to represent the actual value of the floating point number.
The index occupies 8-bit binary data, which indicates that the value range is 0-255. However, the index should be positive and negative, so IEEE stipulates that the power calculated here must be less than 127, which is the real index. So the float index can range from-126 to 128.
The base part actually occupies a value of 24-bit. Because the maximum bit is always 1, the maximum bit is not stored, and only 23-bit is stored.
So far, the base part 23 digits plus the index Part 8 digits use 31 digits. As mentioned above, float occupies 4 bytes, namely 32-bit. Why is there another one? Another digit is the highest bit in four bytes, which is used to indicate the positive and negative values of floating point numbers. When the highest bit is 1, it is a negative number, and the highest bit is 0, it is a positive number.
Floating point data is stored in four bytes in the following table format:
|
Address + 0 |
Address + 1 |
Address + 2 |
Address + 3 |
Contents |
Seee eeee |
Emmm mmmm |
Mmmm |
Mmmm |
S: indicates positive and negative floating point numbers. 1 indicates negative, and 0 indicates positive.
E: the number of binary values after the exponent plus the value 127
M: base number of 24-bit (only 23-bit)
Note: Here is a special case. When the floating point number is 0, the index and the base number are both 0, but the previous formula is not true. Because 0 of 2 is 1, 0 is a special case. Of course, this special case does not need to be considered as interference, and the compiler will automatically identify it.
Through the above format, we will take an example to see the specific data stored in the computer-12.5:
|
Address + 0 |
Address + 1 |
Address + 2 |
Address + 3 |
Contents |
0xc1 |
0x48 |
0x00 |
0x00 |
Next, let's verify whether the above data represents-12.5, and then let's take a look at its conversion process.
Since floating point numbers are not stored in a direct format, they are composed of several parts. To convert floating point numbers, you must first separate the values of each part.
|
Address + 0 |
Address + 1 |
Address + 2 |
Address + 3 |
Format |
Seeeeeee |
Emmmmmmm |
Mmmmmm |
Mmmmmm |
Binary |
11000001 |
01001000 |
00000000 |
00000000 |
Hexadecimal |
C1 |
48 |
00 |
00 |
Visible:
S: 1, which is a negative number.
E:10000010Convert to decimal system 130,130-127 = 3, that is, the actual index is 3.
M:10010000000000000000000. Here, a 1 is omitted on the left side of the base number and expressed1.10010000000000000000000
At this point, the values of the three parts have all come out. Now, we can adjust the m value of the base part by exponent part E. Adjusted to: if the index e is negative, the decimal point of the base number shifts to the left. If the index e is positive, the decimal point of the base number shifts to the right. The number of digits that move the decimal point is determined by the absolute value of E.
Here, e is 3, and the Right Shift 3 is used as the result:
1100.10000000000000000000
Until now, the result is the binary floating point number of 12.5. If you convert it into a 10-digit number, you will see 12.5. For how to convert it, see the following:
Left decimal point1100(1 × 23) + (1 × 22) + (0 × 21) + (0 × 20). The result is12.
Right decimal point. 100...(1X2-1) + (0x2-2) + (0x2-3) +..., the result is. 5.
Sum of the preceding two values12.5Because S is1Is negative, that is-12.5.
Therefore, hexadecimal0xc1480000Yesfloating point number-12.5.
The preceding figure shows how to convert the binary number in computer storage to the actual floating point number. The following shows how to replace a floating point number with the binary number in the computer storage format.
For example, convert 17.625 to float type.
First, convert 17.625 to binary:10001.101(0.625 = 0.5 + 0.125, 0.5 is 1/2, 0.125 is 1/8. If the decimal part is not converted into binary, see other books .) Add10001.101Shift to the right until only one digit before the decimal point reaches the 4 power of 1.0001101x2 (because 4 digits are shifted to the right ). In this case, the base number m and the index e come out:
The base number is M. Because it must be 1 before the decimal point, IEEE requires that only the decimal point be recorded. Therefore, the base number here is0001101.
Exponential part E, which is actually 4, but must be added to 127, fixed to 131, that is, binary number10000011
Symbol part s, because it is a positive number, so s is0.
To sum up, 17.625 of the float storage formats are:
0 10000011 00011010000000000000000
Convert to hexadecimal:0x41 8d 00 00
Therefore, it still occupies 4 bytes.
The following is the test code:
Void binary_print (char C)
{
For (INT I = 0; I <8; ++ I)
{
If (C <I) & 0x80)
Cout <'1 ';
Else
Cout <'0 ';
}
Cout <'';
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Float;
Char c_save [4];
Char I;
Void * F;
F = &;
Cout <"enter a floating point number :";
Cin>;
Cout <Endl;
For (I = 0; I <4; I ++)
{
C_save [I] = * (char *) F + I );
}
Cout <"the floating point number is stored in the computer memory in the following format:" <Endl;
For (I = 4; I! = 0; I --)
Binary_print (c_save [I-1]);
Cout <Endl;
}