将数字与NaN进行比较会得到什么结果? [英] What is the result of comparing a number with NaN?
问题描述
例如考虑
bool fun (double a, double b) {
return a < b;
}
如果任何参数为NaN,fun
将返回什么?这是未定义的/实现定义的行为吗?
What will fun
return if any of the arguments are NaN? Is this undefined / implementation defined behavior?
其他关系运算符和相等运算符会发生什么?
What happens with the other relational operators and the equality operators?
推荐答案
C ++标准仅说:
[expr.rel]/5 如果两个操作数(转换后)都是算术或枚举类型,则如果指定的关系为true,则每个运算符都应产生
true
,而false
如果它是假的.
[expr.rel]/5 If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield
true
if the specified relationship is true andfalse
if it is false.
因此,基本上,如果a
小于b
,则a < b
为真.
So basically, a < b
is true if a
is less than b
.
但是,该实现可以通过numeric_limits::is_iec559
声明符合浮点运算法则的IEC 559 aka IEEE 754标准.然后由5.7节和表4中的该标准控制,该标准要求除!=
之外的所有比较都涉及NaN
报告false
. !=
涉及NaN
个报告true
However, the implementation may claim conformance to IEC 559 aka IEEE 754 standard for floating point arithmetic, via numeric_limits::is_iec559
. Then it is governed by that standard in section 5.7 and table 4, which requires that all comparisons but !=
involving NaN
report false
. !=
involving NaN
reports true
这篇关于将数字与NaN进行比较会得到什么结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!