Bug report: Can't use NAN in a const expression

Community Edition user here. Not sure where to report this…

I’m writing a script to detect the bit representation of floating point numbers for different compilers, operating systems, and CPU architectures, and am trying to use it with the PGI compiler. One of the things my script does is compile a NAN float as a const expression. Unfortunately, the PGI compiler doesn’t support this. When I try to compile the following:

#include <math.h>

static const float my_nan = NAN;

int main(void)
{
  return 0;
}

I get the following error:

PGC-S-0074-Non-constant expression in initializer (main.c: 3)
PGC/power Linux 19.10-0: compilation completed with severe errors

This is due to how NAN is defined in math.h:

/* On GCC 8, the definition of NAN is something that pgcc doesn't handle very
 * well.  Change the definition to something else. */
#undef NAN
static union { unsigned char __c[4]; float __d; } __pgi_nan_union
    __attribute_used__ = { { 0, 0, 0xc0, 0x7f } };
#define NAN    (__pgi_nan_union.__d)
#endif

Hi Kyle,

Apologies for the late response.

Yes, that code was added awhile ago to address some incompatibilities between our old C compiler and GNU. We’re in the process of switching to a C++ based front-end for C where this work-around wont be needed any longer so will be removed from the math.h we ship in an upcoming release.

While I don’t know what other issues this may cause, one possible work around is to comment out this code from the PGI 19.10 include/math.h file and then compile with the new C compiler (pgcc18).

With this commented out from math.h:

% pgcc test.c
PGC-W-0132-Floating pt. invalid oprnd. Check constants and constant expressions (test.c: 3)
PGC-W-0086-Division by zero (test.c: 3)
PGC/x86-64 Linux 19.10-0: compilation completed with warnings
% pgcc18 test.c
"test.c", line 3: warning: variable "my_nan" was declared but never referenced
  static const float my_nan = NAN;

-Mat