Storage Format of float (single precision) in memory in VC6.0

Source: Internet
Author: User

Copy From http://hi.baidu.com/chumingyu/blog/item/ff7b583829604d3c96ddd8b6.html

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 S: indicates positive and negative floating point numbers, 1 is negative, 0 is positive
E: the number of binary values after the exponent plus the value 127
M: base number of 24-bit (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 we will 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, and the actual base number is expressed as 1.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:
1100 on the left of the decimal point is (1 × 23) + (1 × 22) + (0 × 21) + (0 × 20), and 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 a binary bit: 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 to a binary part, 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 is M. Because it must be 1 before the decimal point, IEEE requires that only the decimal point be recorded. Therefore, the base number is 0001101.
Exponential part E, which is actually 4, but must be added to 127, fixed to 131, that is, the binary number 10000011
Symbol part S. Because it is a positive number, S is 0.
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.

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;
}
//************************************** **************************************** *******

# Include <iostream. h>

Int main ()
{
Float a = 2;/* Here Let a be equal to 2,-2, 2, 6, 1, 0.75, 2.5, 0.1, 0 */
Char * p = (char *) &;
P + = 3;/* change the address from high to low */
Int line = 0, temp = 0;
While (temp <4)
{
For (int I = 7; I> = 0; I --)
{
If (* p & (1 <I) cout <'1';/* is used for and computation. if the sum of 1 and 1 is 1, the binary value is 1, otherwise, it is 0, and the left shift operation keeps 1 moving to the high position */
Else cout <'0 ';
Line ++;
If (line % 4 = 0) cout <'';
}
Temp ++;

P --;
}
Cout <endl;
Return 0;
}

 

 

The test results and results are described as follows ):

The format, then, for the various sizes is as follows:

Format BYTE 1 BYTE 2 BYTE 3 BYTE 4 ... BYTE n
Real * 4 XXXX XMMM MMMM MMMM MMMM    
Real * 8 SXXX XXXX XXXX MMMM MMMM MMMM ... MMMM
Real * 10 Sxxx xxxx Xxxx 1mmm mmmm Mmmm ... Mmmm

Note: The four-byte floating point number, that is, the float type.

S represents the sign bit, the X's are the exponent bits, and the M's are the mantissa bits. note that the leftmost bit is assumed in real * 4 and real * 8 formats, but is present as "1" in BYTE 3 of the real * 10 format.

To shift the binary point properly, you first unbias the exponent and then move the binary point to the right or left the appropriate number of bits.

Examples

The following are some examples in real * 4 format:

  • In the following example, the sign bit is zero, and the stored exponent is 128, or 100 0000 0 in binary, which is 127 plus 1. the stored mantissa is (1 .) 000 0000... 0000 0000, which has an implied leading 1 and binary point, so the actual mantissa is
    One.

    SXXX XXXX XMMM MMMM ... MMMM MMMM2   =  1  * 2**1  = 0100 0000 0000 0000 ... 0000 0000 = 4000 0000
  • Same as + 2 blocks t that the sign bit is set. This is true for all IEEE format floating-point numbers.
    -2  = -1  * 2**1  = 1100 0000 0000 0000 ... 0000 0000 = C000 0000
  • Same mantissa, exponent increases by one (biased value is 129, or 100 0000 1 in binary.
    4  =  1  * 2**2  = 0100 0000 1000 0000 ... 0000 0000 = 4080 0000
  • Same exponent, mantissa is larger by half-it's (1 .) 100 0000... 0000 0000, which, since this is a binary fraction, is 1 1/2 (the values of the fractional digits are 1/2, 1/4, 1/8, and so forth ).
    6  = 1.5 * 2**2  = 0100 0000 1100 0000 ... 0000 0000 = 40C0 0000
  • Same exponent as other powers of two, mantissa is one less than two at 127, or 011 1111 1 in binary.
    1  = 1   * 2**0  = 0011 1111 1000 0000 ... 0000 0000 = 3F80 0000
  • The biased exponent is 126,011 1111 0 in binary, and the mantissa is (1.) 100 0000... 0000 0000, which is 1 1/2.
    .75 = 1.5 * 2**-1 = 0011 1111 0100 0000 ... 0000 0000 = 3F40 0000
  • Exactly the same as two counter t that the bit that represents 1/4 is set in the mantissa.
    2.5 = 1.25 * 2**1 = 0100 0000 0010 0000 ... 0000 0000 = 4020 0000
  • 1/10 is a repeating fraction in binary. the mantissa is just shy of 1.6, and the biased exponent says that 1.6 is to be divided by 16 (it is 011 1101 1 in binary, which is 123 in decimal ). the true exponent is 123-127 =-4, which means that the factor
    By which to multiply is 2 **-4 = 1/16. note that the stored mantissa is rounded up in the last bit-an attempt to represent the unrepresentable number as accurately as possible. (The reason that 1/10 and 1/100 are not exactly representable in binary is similar
    To the reason that 1/3 is not exactly representable in decimal .)

    0.1 = 1.6 * 2**-4 = 0011 1101 1100 1100 ... 1100 1101 = 3DCC CCCD
  • 0 = 1.0*2 **-128 = all zeros -- a special case.

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.