#include "stdafx.h"
#include <string.h>
#include <math.h>

float IEEE_754(int num);
#if 0
int main(int argc, char* argv[])
{
float f;
f = IEEE_754(0x00280000);
printf("%f\r\n",f);
f = IEEE_754(0x43BE1833);
printf("%f\r\n",f);
f = IEEE_754(0x3FC00000);
printf("%f\r\n",f);
f = IEEE_754(0x41D00000);
printf("%f\r\n",f);
f = IEEE_754(0xBFC00000);
printf("%f\r\n",f);

getchar();
return 0;
}

//太小的数会当成0
float IEEE_754(int num)
{
float f;
int S,P,M;//符号位,指数,尾数
S = num >> 31;
P = (num & 0x7FFFFFFF) >> 23;
M = (num & 0x007FFFFF);
f = pow(2,-23);
if(P == 0){
f = pow(2,-126) * M*pow(2,-23);//*M;
}else if(P != 0xFF){
f = pow(2,P-127) * (1+M*pow(2,-23));//固定偏移127
}
if(S){//符号位
f = -f;
}
if(P==0 && M==0){
f = 0;
}
return f;
}

#else


#include <iostream>
using namespace std;

int main(){
unsigned int hex=0xBFC00000;//0x41360000;
float* fp=(float*)&hex;//编译器内部浮点数使用的就是IEEE754标准
cout<<"x="<<*fp<<endl;

hex=0x00280000;
fp=(float*)&hex;
cout<<"x="<<*fp<<endl;
hex=0x43BE1833;
fp=(float*)&hex;
cout<<"x="<<*fp<<endl;
hex=0x3FC00000;
fp=(float*)&hex;
cout<<"x="<<*fp<<endl;
hex=0x41D00000;
fp=(float*)&hex;
cout<<"x="<<*fp<<endl;
hex=0xBFC00000;
fp=(float*)&hex;
cout<<"x="<<*fp<<endl;
return 0;
}

#endif