【发布时间】:2015-02-03 15:01:47
【问题描述】:
我在我的程序中使用-Ofast 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
所以!= 和== 的结果是不可信的。这是否意味着我应该 == 和 != 只有当这两个值都不是 nan 时,否则我应该在之前使用 isnan 进行测试?
是否保证isnan 与-Ofast 一起正常工作?
== 和 != 与 -Ofast 的双精度如何正确工作?
有人可以提供-Ofast 添加的完整限制列表吗?
【问题讨论】:
-
你为什么首先使用-Ofast?你读过文档吗?它与 -O3 -ffast-math 相同,其中 -ffast-math 表示您不在乎 float/double 是否给出错误结果,只要它们速度快...