在以下程序中混合使用 int 和 double 会产生一个奇怪的结果:
int main()
{
double x = 9.90, y = 10.0, z, w;
int ip, fp;
z = y - x;
ip = (int) z;
w = (z - ip)*100;
printf( "\nx = %f\ty = %f\tz = %f\tw = %f\n",
x, y, z, w );
printf("\nip = %d", ip); /* So far, so good */
fp = (int) w; /* Why does fp get 9 and not 10??? */
printf("\nfp = %d\n", fp);
return 0;
}
结果:
x = 9.900000 y = 10.000000 z = 0.100000 w = 10.000000
ip = 0
fp = 9 (Should be 10!)
结果与使用 MingGW (WinXP) e Clang (MacOSX) 相同。谁能解释这种奇怪的行为?