Code (t125.c):
#include <fenv.h>
#include <stdint.h>
#include <stdio.h>
#if _MSC_VER
#pragma fenv_access (on)
#else
#pragma STDC FENV_ACCESS ON
#endif
void show_fe_exceptions(void)
{
printf("exceptions raised: ");
if (fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO");
if (fetestexcept(FE_INEXACT)) printf(" FE_INEXACT");
if (fetestexcept(FE_INVALID)) printf(" FE_INVALID");
if (fetestexcept(FE_OVERFLOW)) printf(" FE_OVERFLOW");
if (fetestexcept(FE_UNDERFLOW)) printf(" FE_UNDERFLOW");
if (fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
printf("\n");
}
typedef union { uint32_t u; float f; } ufloat;
int main(void)
{
_Bool b;
ufloat uqnan;
volatile float f;
uqnan.u = 0x7fc00000;
f = uqnan.f;
b = f == f;
show_fe_exceptions();
return b ? 1 : 0;
}
Invocations:
$ gcc t125.c -Wall -Wextra -pedantic -std=c17 && ./a.exe
t125.c:7: warning: ignoring ‘#pragma STDC FENV_ACCESS’ [-Wunknown-pragmas]
7 | #pragma STDC FENV_ACCESS ON
|
exceptions raised: none
$ clang t125.c -Wall -Wextra -pedantic -std=c17 && ./a.exe
t125.c:7:14: warning: pragma STDC FENV_ACCESS ON is not supported, ignoring pragma [-Wunknown-pragmas]
#pragma STDC FENV_ACCESS ON
^
1 warning generated.
exceptions raised: none
$ cl t125.c /fp:strict /std:c17 && t125
exceptions raised: none
Question: why QNAN == QNAN
does not lead to raising FE_INVALID
exception?
UPD. Reason of the question: (false, see below) assumption that <any_NAN> == <any_NAN>
leads to raising FE_INVALID
exception.
UPD2. Changed code: from f = *(float*)&qnan
to f = uqnan.f
(type punning via union). This is to avoid violation of the aliasing rules of the C standard.