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 that mantissa and exponent in the formula are represented in binary format)
Base
The binary number is used to represent the actual value of the floating point number.
Index
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 mm
S:
Indicates positive and negative floating point numbers. 1 indicates negative, and 0 indicates positive.
E:
Returns the binary number of the value after 127.
M:
24-bit base (only 23-bit)
Idea: 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: seeeeeeeee emmmmmmm Mmmmmmmm
Binary 11000001 01001000 00000000 00000000
Hexadecimal C1 48 00 00
Visible:
S: 1, which is a negative number.
E: Convert 10000010 to 10: 130,130-127 = 3, that is, the actual index is 3.
M: It is 10010000000000000000000.
Here, a 1 is omitted on the left of the base number,
The actual base number is represented as 1.10010000000000000000000 to this point. The values of the three parts of the table are all shown. Now, we can adjust the base number m value 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 value is shifted to the right: 1100.10000000000000000000 to the next time. The result is a binary floating point number of 12.5. If it is converted to a decimal number, 12.5 is displayed, for how to convert, see the following: the 1100 decimal point on the left represents (1 × 23) + (1 × 22) + (0 × 21) + (0 × 20 ), the result is 12. The right decimal point. 100... It is expressed as (1 × 2-1) + (0 × 2-2) + (0 × 2-3) +..., and the result is. 5. The sum of the preceding two values is 12.5. Because S is 1, the negative value is used, that is,-12.5. Therefore, the hexadecimal 0xc1480000 is a floating point-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 .)
Then shift 10001.101 to the right until only one digit is left before the decimal point to 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 M, because the decimal point must be 1, so IEEE requires that only the decimal point should be recorded, so the base number here is 0001101. Exponential part E, which is actually 4, but must be added with 127, fixed as 131, that is, the binary number 10000011 symbol part s, because it is a positive number, so s is 0. to sum up, the float storage format of 17.625 is: 0 10000011 00011010000000000000000 is converted to hexadecimal: 0x41 8d 00 00. Therefore, it still occupies 4 bytes.
Next, I did an interesting experiment, that is, the user enters a floating point number, and the program outputs the binary data stored in the computer directly, let's see if the ones we mentioned above are correct. If you are interested, you can copy it to vc6.0 and try it ~!
# Include <iostream. h>
# Define uchar unsigned char
Void binary_print (uchar C)
{
For (INT I = 0; I <8; ++ I)
{
If (C <I) & 0x80)
Cout <'1'; else cout <'0 ';
}
Cout <'';
}
Void main ()
{
Float;
Uchar c_save [4];
Uchar I;
Void * F;
F = &;
Cout <"enter a floating point number :";
Cin>;
Cout <Endl;
For (I = 0; I <4; I ++)
{
C_save [I] = * (uchar *) 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;
}