QNAN 传递到 C 标准库函数(例如 llrintf):不清楚是否引发 FP 异常

QNAN passed into C standard library functions (ex. llrintf): not clear whether FP exceptions are raised or not

来自 math.h 的宏 NAN 安静 NAN:

ISO/IEC 9899:2011 (E)(重点添加):

The macro

NAN

is defined if and only if the implementation supports quiet NaNs for the float type. It expands to a constant expression of type float representing a quiet NaN.

安静的 NaN 通常不会导致引发 FP 异常。示例:

  1. 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.

  1. 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.

但是:

  1. llrintf() 既不是 算术运算 ,也不是 Table 5.3 中的 无序安静谓词。因此,5.2.4.2.2.35.11 不适用。
  2. 7.12.9.5 The lrint and llrint functions(例如)在输入为 quiet NaN 的情况下未提及是否引发 FP 异常。

初步结论:由于一般做法“安静的 NaN 不会导致引发 FP 异常”,可以得出结论 lrintllrint 函数应该 如果输入是安静的 NaN,则导致引发 FP 异常。

练习:

代码(t125.c):

#include <fenv.h>
#include <math.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");
}

int main(void)
{
    long long ll;

    ll = llrintf(NAN);
    show_fe_exceptions();
    printf("ll %lld\n", ll);
    return 0;
}

调用:

$ cl t125.c /std:c11 /fp:strict && t125
exceptions raised:  FE_INEXACT FE_INVALID FE_OVERFLOW
ll 0

$ clang t125.c -std=c11 -ffp-model=strict -Wall -Wextra -pedantic && ./a.exe
t125.c:6:9: warning: unknown pragma ignored [-Wunknown-pragmas]
#pragma fenv_access (on)
        ^
1 warning generated.
exceptions raised:  FE_INEXACT FE_INVALID FE_OVERFLOW
ll -9223372036854775808

$ gcc t125.c -std=c11 -Wall -Wextra -pedantic && ./a.exe
t125.c:8: warning: ignoring ‘#pragma STDC FENV_ACCESS’ [-Wunknown-pragmas]
    8 | #pragma STDC FENV_ACCESS ON
      |
exceptions raised:  FE_INVALID
ll -9223372036854775808

问题:如果输入是 quiet NaN,有人可以澄清 C 标准库函数(例如 llrintf)引发 FP 异常的情况/行为吗?

OP 的印象是术语 conversion 仅暗示 隐式转换 显式转换 (投射操作)(参见 C11 6.3 Conversions)。这意味着 OP 认为术语 function 不是 conversion

但是,C11 F.3 Operators and functions(强调)明确指出:

The lrint and llrint functions in <math.h> provide the IEC 60559 conversions.

因此,是的,C11 F.4 Floating to integer conversion(强调)回答了问题:

F.4 Floating to integer conversion

1    ... Otherwise, if the floating value is infinite or NaN or if the integral part of the floating value exceeds the range of the integer type, then the ‘‘invalid’’ floating-point exception is raised and the resulting value is unspecified. ...