将int和const int转换为float时的结果不同

有人能够解释为什么int和const int在转换为float并在浮点数学中使用时给出不同的结果? 例如,参见这段代码:

int _tmain(int argc, _TCHAR* argv[]) { int x = 1000; const int y = 1000; float fx = (float) x; float fy = (float) y; printf("(int = 1000) * 0.3f = %4.10f \n", 0.3f*x); printf("(const int = 1000) * 0.3f = %4.10f \n", 0.3f*y); printf("(float)(int = 1000) * 0.3f = %4.10f \n", 0.3f*fx); printf("(float)(const int = 1000) * 0.3f = %4.10f \n", 0.3f*fy); return 0; } 

结果是:

 (int = 1000) * 0.3f = 300.0000119209 (const int = 1000) * 0.3f = 300.0000000000 (float)(int = 1000) * 0.3f = 300.0000119209 (float)(const int = 1000) * 0.3f = 300.0000119209 

我的猜测是,在第一种情况下,0.3f *(int)被隐式地转换为float,而在第二种情况下,0.3f *(const int)被隐式转换为double。 这是正确的,如果是这样,为什么会发生这种情况? 另外,什么是“正确”的方法?

非常感谢

在生成代码之前,编译器可以执行两个常量的乘法运算。 其余的必须在运行时完成。