我在我的程序中使用-Ofast
gcc选项导致延迟需求。我写了简单的测试程序:gcc - 快速 - 限制的完整列表
#include <iostream>
#include <math.h>
static double quiet_NaN = std::numeric_limits<double>::quiet_NaN();
int main()
{
double newValue = 130000;
double curValue = quiet_NaN;
printf("newValue = %f\n", newValue);
printf("curValue = %f\n", curValue);
printf("isnan(newValue) = %d\n", isnan(newValue));
printf("isnan(curValue) = %d\n", isnan(curValue));
printf("newValue == curValue %d\n", (newValue == curValue));
printf("newValue != curValue %d\n", (newValue != curValue));
}
我试图用默认的标志,并与-Ofast运行:
$ g++ TestPointer.cpp
$./a.out
newValue = 130000.000000
curValue = nan
isnan(newValue) = 0
isnan(curValue) = 1
newValue == curValue 0
newValue != curValue 1
$ g++ -Ofast TestPointer.cpp
$ ./a.out
newValue = 130000.000000
curValue = nan
isnan(newValue) = 0
isnan(curValue) = 1
newValue == curValue 1
newValue != curValue 0
所以!=
和==
结果不能被信任。这是否意味着只有当这两个值都不是南时,我才应该==
和!=
,否则我应该先用isnan
进行测试?
确保isnan
与-Ofast
正常工作? ==
和!=
如何正确工作与-Ofast
双? 有人可以提供由-Ofast
添加的完整限制列表吗?
为什么你首先使用-Ofast?你读过文档了吗?它与-O3 -ffast-math相同,其中-ffast-math表示你不关心float/double只要速度很快就会给出错误结果...... – 2014-12-06 09:02:49