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:
The macro NAN
from math.h
is quiet NAN:
ISO/IEC 9899:2011(E)(添加了重点):
ISO/IEC 9899:2011 (E) (emphasis added):
宏
NAN
.它扩展为一个float类型的常量表达式,表示一个安静的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异常的产生.例子:
Quiet NaNs usually do not lead to raising of FP exceptions. Examples:
- ISO/IEC 9899:2011(E)(添加了重点):
5.2.4.2.2浮动类型的特征< float.h>
5.2.4.2.2 Characteristics of floating types <float.h>
3 安静的NaN 几乎通过每个算术运算传播,而不会引发浮点异常;信号NaN作为算术操作数出现时通常会引发浮点异常.
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.
- IEEE 754-2008(添加了重点):
5.11比较谓词的详细信息明确考虑了安静NaN 操作数的可能性的程序可以使用表5.3中的无序安静谓词,而则不使用表示这样的无效操作异常.
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.
但是:
-
llrintf()
既不是算术运算,也不是表5.3中的无序安静谓词.因此,5.2.4.2.2.3
和5.11
不适用. -
7.12.9.5例如,lrint和llrint函数
并没有说明如果输入是安静的NaN,是否会引发FP异常.
llrintf()
is neither arithmetic operation, nor unordered-quiet predicates in Table 5.3. Hence,5.2.4.2.2.3
and5.11
are not applicable.7.12.9.5 The lrint and llrint functions
(for example) says nothing about whether FP exceptions are raised or not in case if input is quiet NaN.
初步结论:由于通常的做法,安静的NaN不会导致提出FP例外".可以得出结论,如果输入是安静的NaN,则 lrint
和 llrint
函数应该 not 导致FP异常.
Preliminary conclusion: Because of the general practice "quiet NaN does not lead to raising FP exceptions" it can be concluded that the lrint
and llrint
functions should not lead to raising FP exceptions if input is quiet NaN.
实践:
代码(t125.c):
Code (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
问题:如果输入是安静的NaN,有人可以澄清C标准库函数(例如 llrintf
)引发FP异常的情况/行为吗?
Question: can someone clarify the situation / behavior around raising of FP exceptions for C standard library functions (ex. llrintf
) if input is quiet NaN?
推荐答案
OP印象中,术语 conversion 仅表示隐式转换和显式转换(广播操作)(请参见 C11 6.3转换
).表示OP印象中的术语 function 不是 conversion .
The OP was under impression that term conversion implies only implicit conversion and explicit conversion (cast operation) (see C11 6.3 Conversions
). Meaning that OP was under impression that term function is not a conversion.
但是, C11 F.3运算符和函数
(添加了重点)明确指出:
However, C11 F.3 Operators and functions
(emphasis added) explicitly states that:
中的 lrint
和llrint
功能提供IEC 60559 转化.
The
lrint
andllrint
functions in<math.h>
provide the IEC 60559 conversions.
因此,是的, C11 F.4浮点数到整数转换
(加了强调)回答了以下问题:
Hence, yes, C11 F.4 Floating to integer conversion
(emphasis added) answers the question:
F.4浮点到整数转换
F.4 Floating to integer conversion
1 ... ...否则,如果浮点值是无穷大或 NaN 或如果浮点值的整数部分超出了整数类型的范围,则无效"'浮点异常被引发,并且结果值未指定....
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. ...
这篇关于QNAN传递给C标准库函数(例如llrintf):不清楚是否引发FP异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!