std::isnan(nan) and nan!=nan return false

Issue #34 resolved
Robert Welch created an issue

If I write the following code in c++:

#include <math.h>
float bad_number = sqrt(0)/0;
std::cout << "nan = " << bad_number << "\n";

The value of bad_number will be NaN and it will always print NaN. The output of the program will always be 'nan = nan'.

If I'm in a random c++ file on my hard drive, and I add these lines:

bool is_nan_homebrew = bad_number != bad_number;
bool is_nan_c11stdlib = std::isnan(bad_number);
bool c99_macro_nan = isnan(bad_number);

All these bools will end up true, because, well, they're nan.

Maybe I'm being dumb (I hope so): I tried the same code inside FFEA (inside rod_math_v9.cpp), and all three of those methods returned false, even though the program still printed 'nan = nan'.

No idea what caused it but this is a really malevolent bug. is_nan_homebrew should always be true, it's in the standard.

A workaround: use boost::math::isnan(x) from boost/math/special_functions/fpclassify.hpp. That seems to work as intended.

Comments (1)

  1. Robert Welch reporter

    The issue is because of --ffast-math being turned on in g++. Now, if you want my honest opinion, it's not worth the hypothetical performance gain, but I'm going to leave it on and just keep using boost for now.

  2. Log in to comment