首先,我知道将浮点值与==进行比较是不好的,因为你可以在尾数中关闭一些非常小的数量,但在我的例子中并非如此.我遇到的问题是基于2个因素的输出变化. 1)我传入的优化标志,以及2)如果我取消注释std :: cout线.
为什么代码GCC在-O2下产生不同的运行?
如果取消注释打印,为什么在-O2下编译的代码有效?
这是我正在测试的代码:
#include <iostream> const float ft_to_m = (float)0.3048; const float m_to_ft = (float)3.28083989501; float FeetToKilometers( float & Feet ) { float Kilometers; Kilometers = (ft_to_m * Feet) / 1000.; return Kilometers; } int main(void) { float feet = 20000.; float old_val = 0; float new_val = FeetToKilometers(feet ); float diff_val = 0; int *old_int = reinterpret_cast<int*>(&old_val); int *new_int = reinterpret_cast<int*>(&new_val); for (int i=0; i<2; i++) { new_val = FeetToKilometers(feet ); diff_val = old_val-new_val; //std::cout << "Random COUT that makes this work" << std::endl; if(old_val==new_val) { std::cout << "old_val==new_val" << std::endl; std::cout << std::hex << *old_int << "," << std::hex << *new_int << std::endl; std::cout << "diff_val = " << diff_val <<std::endl; } else { std::cout << "old_val!=new_val" <<std::endl; std::cout << std::hex << *old_int << "," << std::hex << *new_int << std::endl; std::cout << "diff_val = " << diff_val <<std::endl; old_val=FeetToKilometers(feet); } } return 0; }
当使用-O0,-O1和-O3(g -O test.cpp)在linux / cygwin上编译时,我得到以下输出:
$./a.exe
old_val!=new_val
0,40c3126f
diff_val = -6.096
old_val==new_val
40c3126f,40c3126f
diff_val = 0
该输出是正确的,您可以看到浮点数(new_val和old_val)的位是相同的.当我使用-O2标志(g -O2 test.cpp)编译时,我得到以下内容:
$./a.exe
old_val!=new_val
0,40c3126f
diff_val = -6.096
old_val!=new_val
40c3126f,40c3126f
diff_val = 1.19209e-07
我会认为这个输出错了.即使两个值相同,但减去它们和==检查表明它们是不同的.如果我然后取消注释std :: cout行,并使用-O2标志(g -O2 test.cpp)重建,我得到以下内容:
$./a.exe
Random COUT that makes this work
old_val!=new_val
0,40c3126f
diff_val = -6.096
Random COUT that makes this work
old_val==new_val
40c3126f,40c3126f
diff_val = 1.19209e-07
这在old_val == new_val中是正确的,即使减法仍然显示略有差异.
如果脚是2000而不是20000,此代码也可以在-O2下工作.
任何人都可以解释为什么编译的代码行为像这样?我想知道为什么2位相同的浮点值不能与==进行比较.
gcc版本3.4.4