C/C++ compare to Nan (different behaviors on different floating point model)

125 views Asked by At

here a little test code:

float zeroF = 0.f;
float naNF = 0.f / zeroF;
float minimumF = std::min(1.0f, naNF);
std::cout << "MinimumF " << minimumF << std::endl;

double zeroD = 0.0;
double naND = 0.0 / zeroD;
double minimumD = std::min(1.0, naND);
std::cout << "MinimumD " << minimumD << std::endl;

I executed the code on VS2013.

On precise model (/fp:precise) the outputs are always "1";

On fast model (/fp:fast) the outputs will be "Nan" (-1.#IND) if optimization is enabled (/O2) and "1" if optimization is disabled (/Od).

First, what should be the right output according to IEEE754 ? (I read the docs and googled different articles like: What is the rationale for all comparisons returning false for IEEE754 NaN values?, and it seems that the right output should by Nan and not 1 but maybe I am wrong.

Secondly, how the fast model optimization here changes so drastically the output?

0

There are 0 answers