为什么 QNAN == QNAN 不会导致引发 FE_INVALID 异常?

Why QNAN == QNAN does not lead to raising FE_INVALID exception?

代码(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;
}

调用:

$ 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

问题:为什么 QNAN == QNAN 不会引发 FE_INVALID 异常?

更新。问题原因:(错误,见下文)假设 <any_NAN> == <any_NAN> 导致引发 FE_INVALID 异常。

UPD2。更改代码:从 f = *(float*)&qnanf = uqnan.f(通过联合输入双关语)。这是为了避免违反 C 标准的别名规则。

ISO/IEC 9899:2011 (E)(强调):

5.2.4.2.2 Characteristics of floating types <float.h>

3    A quiet NaN propagates through almost every arithmetic operation without raising a floating-point exception; a signaling NaN generally raises a floating-point exception when occurring as an arithmetic operand.

另请参阅:What is the difference between quiet NaN and signaling NaN?

更新。是的,看来等式算不上这个目的的算术运算。然后这里引用 IEEE 754-2008(强调):

5.11 Details of comparison predicates Programs that explicitly take account of the possibility of quiet NaN operands may use the unordered-quiet predicates in Table 5.3 which do not signal such an invalid operation exception.

例如,谓词LT EQ 不应导致引发无效操作异常。但是,我们看到(在上面的评论中)对于 f <= fgcccl (msvc) 都会提高 FE_INVALID。这是错误/缺陷吗?虽然 none 其中定义 __STDC_IEC_559__ 定义为 1。但是,它们不是 必需的:

__STDC_IEC_559__ The integer constant 1, intended to indicate conformance to the specifications in annex F (IEC 60559 floating-point arithmetic).