cvisual-c++c99

Recent MSVC versions don't treat NAN as constant, workaround?


Recent MSVC versions don't seem to treat NAN as a constant anymore. The new definition appears to be (__ucrt_int_to_float(0x7FC00000)). The old one was (-(float)(((float)(1e+300 * 1e+300)) * 0.0F)).

This causes code like the following to fail to compile with error C2099: initializer is not a constant:

#include <math.h>

double x[] = { 1.0, NAN };

Unfortunately, I don't have direct access to MSVC and I am dealing with this through GitHub CI.

Questions:

Apparently, MSVC also doesn't accept 0.0 / 0.0. The code double f() { return 0.0 / 0.0; } fails with error C2124: divide or mod by zero


Solution

  • This seems to be a bug in Windows SDK version 10.0.26100.0, which defines NAN as a call to __ucrt_int_to_float() instead of as a constant. It is being tracked here.

    As a workaround one can define _UCRT_NOISY_NAN to enable the legacy definition of the NAN macro, which can be seen here.