浮点型变量在计算机内存中占用4字节(Byte,32-bit。遵循IEEE-754格式标准。
一个浮点数由2部分组成:底数m 指数e
                          ±mantissa × 2exponent
(注意,公式中的mantissa exponent使用二进制表示)
底数部分 使用2进制数来表示此浮点数的实际值。
指数部分 占用8-bit的二进制数,可表示数值范围为0float几个字节多少位255。 但是指数应可正可负,所以IEEE规定,此处算出的次方须减去127才是真正的指数。所以float的指数可从 -126128.
底数部分实际是占用24-bit的一个值,由于其最高位始终为 1 ,所以最高位省去不存储,在存储中只有23-bit
到目前为止, 底数部分 23 加上指数部分 8 使用了31位。那么前面说过,float是占用4个字节即32-bit,那么还有一位是干嘛用的呢?  还有一位,其实就是4字节中的最高位,用来指示浮点数的正负,当最高位是1时,为负数,最高位是0时,为正数。
  浮点数据就是按下表的格式存储在4个字节中:
      Address+0    Address+1    Address+2    Address+3
Contents    SEEE EEEE    EMMM MMMM    MMMM MMMM    MMMM MMMM      S: 表示浮
点数正负,1为负数,0为正数
      E: 指数加上127后的值的二进制数
      M: 24-bit的底数(只存储23-bit
主意:这里有个特例,浮点数 0时,指数和底数都为0,但此前的公式不成立。因为20次方为1,所以,0是个特例。当然,这个特例也不用认为去干扰,编译器会自动去识别。


      通过上面的格式,我们下面举例看下-12.5在计算机中存储的具体数据:
    Address+0    Address+1    Address+2    Address+3
Contents        0xC1                        0x48                          0x00                      0x00    接下来我们验证下上面的数据表示的到底是不是-12.5,从而也看下它的转换过程。
由于浮点数不是以直接格式存储,他有几部分组成,所以要转换浮点数,首先要把各部分的值分离出来。
    Address+0    Address+1    Address+2    Address+3
格式    SEEEEEEE    EMMMMMMM    MMMMMMMM    MMMMMMMM
二进制    11000001    01001000    00000000    00000000
16进制    C1                          48                            00                            00
      可见:
      S: 1,是个负数。
      E: 10000010  转为10进制为130130-127=3,即实际指数部分为3.
      M: 10010000000000000000000 这里,在底数左边省略存储了一个1,使用 实际底数表示为 1.10010000000000000000000
      到此,我们吧三个部分的值都拎出来了,现在,我们通过指数部分E的值来调整底数部分M的值。调整方法为:如果指数E为负数,底数的小数点向左移,如果指数E为正数,底数的小数点向右移。小数点移动的位数由指数E的绝对值决定。
      这里,E为正3,使用向右移3为即得:
      1100.10000000000000000000
至次,这个结果就是12.5的二进制浮点数,将他换算成10进制数就看到12.5了,如何转换,看下面:
小数点左边的1100 表示为 (1 × 23) + (1 × 22) + (0 × 21) + (0 × 20), 其结果为 12
小数点右边的 .100 表示为 (1 × 2-1) + (0 × 2-2) + (0 × 2-3) + ... ,其结果为.5
以上二值的和为12.5 由于S 1,使用为负数,即-12.5
所以,16进制 0XC1480000 是浮点数 -12.5

上面是如何将计算机存储中的二进制数如何转换成实际浮点数,下面看下如何将一浮点数装换成计算机存储格式中的二进制数。
举例将17.625换算成 float型。
先,将17.625换算成二进制位:10001.101  ( 0.625 = 0.5+0.125, 0.5 1/2, 0.125 1/8 如果 不会将小数部分转换成二进制,请参考其他书籍。) 再将 10001.101 向右移,直到小数点前只剩一位 成了 1.0001101 x 24次方 (因为右移了4位)。此时 我们的底数M和指数E就出来了:
底数部分M,因为小数点前必为1,所以IEEE规定只记录小数点后的就好,所以此处底数为  0001101
指数部分E,实际为4,但须加上127,固为131,即二进制数 10000011
符号部分S,由于是正数,所以S0.
综上所述,17.625 float 存储格式就是:
0 10000011 00011010000000000000000
转换成16进制:0x41 8D 00 00
所以,一看,还是占用了4个字节。


下面,我做了个有趣的实验,就是由用户输入一个浮点数,程序将这个浮点数在计算机中存储的二进制直接输出,来看看我们上面所将的那些是否正确。
有兴趣同学可以copyVC6.0中去试试~



#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 a;
    uchar c_save[4];
    uchar i;
    void *f;
    f = &a;
    cout<<"请输入一个浮点数:";
    cin>>a;
    cout<<endl;
    for(i=0;i<4;i++)
    {
    c_save[i] =  *((uchar*)f+i);
    }
    cout<<"此浮点数在计算机内存中储存格式如下:"<<endl;
    for(i=4;i!=0;i--)
    binary_print(c_save[i-1]);
    cout<<endl;
}
//*************************************************************************************
#include <iostream.h>

int main()
{
    float a=2; /*这里分别让a等于2-24610.752.50.10 */
    char *p=(char*)&a;
    p+=3; /*将地址由高到低*/
    int line=0,temp=0;
    while(temp<4)
    {
        for(int i=7;i>=0;i--)
        {
            if(*p&(1<<i)) cout<<'1'; /*作与运算,如果和1与为1,说明该二进制是1,否则为0,其中左移运算让1不断往高位移动*/
            else cout<<'0';
            line++;
            if(line%4==0) cout<<' ';
        }
        temp++;
        p--;
    }
    cout<<endl;
    return 0;
}
 
 
测试结果及结果说明,给出MSDN Library上的解释(英文原版,原汁原味):
The format, then, for the various sizes is as follows:
Format
BYTE 1
BYTE 2
BYTE 3
BYTE 4
...
BYTE n
real*4
XXXX XXXX
XMMM MMMM
MMMM MMMM
MMMM MMMM
real*8
SXXX XXXX
XXXX MMMM
MMMM MMMM
MMMM MMMM
...
MMMM MMMM
real*10
SXXX XXXX
XXXX XXXX
1MMM MMMM
MMMM MMMM
...
MMMM MMMM
注:研究的是4字节浮点数,即float型数。
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 except 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 except 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.