C++

Asked • 08/30/19

Why does NaN - NaN == 0.0 with the Intel C++ Compiler?

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](http://ideone.com/FjphJj)) 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 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 Specifically, `qNaN - qNaN == 0.0`. 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?

1 Expert Answer

By:

Dylan B. answered • 11/10/20

Tutor
New to Wyzant

Computer science Tutor, Associates degree, Real world experience

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.