为什么使用英特尔C ++编译器的NaN-NaN == 0.0? [英] Why does NaN - NaN == 0.0 with the Intel C++ Compiler?
问题描述
众所周知,NaN是以算术方式传播的,但是我找不到任何示范,所以我写了一个小测试:
It is well-known that NaNs propagate in arithmetic, but I couldn't find any demonstrations, so I wrote a small test:
#include <limits>
#include <cstdio>
int main(int argc, char* argv[]) {
float qNaN = std::numeric_limits<float>::quiet_NaN();
float neg = -qNaN;
float sub1 = 6.0f - qNaN;
float sub2 = qNaN - 6.0f;
float sub3 = qNaN - qNaN;
float add1 = 6.0f + qNaN;
float add2 = qNaN + qNaN;
float div1 = 6.0f / qNaN;
float div2 = qNaN / 6.0f;
float div3 = qNaN / qNaN;
float mul1 = 6.0f * qNaN;
float mul2 = qNaN * qNaN;
printf(
"neg: %f\nsub: %f %f %f\nadd: %f %f\ndiv: %f %f %f\nmul: %f %f\n",
neg, sub1,sub2,sub3, add1,add2, div1,div2,div3, mul1,mul2
);
return 0;
}
该示例(在此处运行)基本上产生了我所期望的(否定有点怪异,但是很有道理):
The example (running live here) produces basically what I would expect (the negative is a little weird, but it kind of makes sense):
neg: -nan
sub: nan nan nan
add: nan nan
div: nan nan nan
mul: nan nan
MSVC 2015产生类似的结果.但是,英特尔C ++ 15会产生:
MSVC 2015 produces something similar. However, Intel C++ 15 produces:
neg: -nan(ind)
sub: nan nan 0.000000
add: nan nan
div: nan nan nan
mul: nan nan
具体是qNaN - qNaN == 0.0
.
这...不对,对吗?有关标准(ISO C,ISO C ++,IEEE 754)对此有何评论?为什么编译器之间的行为有所不同?
This... can't be right, right? What do the relevant standards (ISO C, ISO C++, IEEE 754) say about this, and why is there a difference in behavior between the compilers?
推荐答案
Intel C ++编译器中的默认浮点处理是/fp:fast
,它不安全地处理NaN
(这也会导致NaN == NaN
被/fp:strict
或/fp:precise
,看看是否有帮助.
The default floating point handling in Intel C++ compiler is /fp:fast
, which handles NaN
's unsafely (which also results in NaN == NaN
being true
for example). Try specifying /fp:strict
or /fp:precise
and see if that helps.
这篇关于为什么使用英特尔C ++编译器的NaN-NaN == 0.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!