C代码
#include <stdio.h>
union
{
float f;
unsigned int u;
} myun;
int main ( void )
{
float a,b,c;
a= 4501.490234;
b= 4501;
c=a-b;
myun.f=a; printf("0x%08X %f\n",myun.u,myun.f);
myun.f=b; printf("0x%08X %f\n",myun.u,myun.f);
myun.f=c; printf("0x%08X %f\n",myun.u,myun.f);
return(0);
}
结果是:
0x458CABEC 4501.490234
0x458CA800 4501.000000
0x3EFB0000 0.490234
我的PHP代码:
<?php
$a = 4501.490234;
$b = 4501;
$c = $a - $b;
echo bin2hex(pack("f", $a))."\n";
echo bin2hex(pack("f", $b))."\n";
echo bin2hex(pack("f", $c))."\n";
$r = unpack('ffloat',"\x00\x00\xFB\x3E");
echo $r['float']."\n";
结果:
ecab8c45
00a88c45
f3fffa3e
0.490234375
所以有两个问题
1.不同的减法结果:f3fffa3e和0x3EFB0000
2.即使相同的0x3EFB0000也有不同的表示形式:0.490234375和0.490234