将数字与NaN进行比较会得到什么结果? [英] What is the result of comparing a number with NaN?

查看:665
本文介绍了将数字与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 and false 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆