“x!= x”是测试NaN的便携方式吗? [英] Is `x!=x` a portable way to test for NaN?
问题描述
isnan(x)
,可以测试是否使用NaN。然而,许多在线的地方,包括例如这个 SO答案说,你可以简单地使用 x!=作为一种方法,在任何C规范中都是 x!= x
那保证测试x是否是NaN?我无法自己找到它,我希望我的代码能够与不同的编译器一起工作。 解决方案
请参考规范部分附件F :C标准的IEC 60559浮点运算:b
$ b
F.1简介
定义
__ STDC_IEC_559 __
的实现应符合本附录中的规范。
实现没有定义
__ STDC_IEC_559 __
不符合这些规范。
F.9.3关系运算符
表达式
x≠x $ c如果
x
是NaN
。
<$ c> p>如果X
是一个Nan $ c>,则表达式
x = x
$ C>
F.3运算符和函数
< math.h>
中的isnan
宏提供了isnan
IEC 60559附录中推荐的功能
In C you can test to see if a double if NaN using isnan(x)
. However many places online, including for example this SO answer say that you can simply use x!=x
instead.
Is x!=x
in any C specification as a method that is guaranteed to test if x is NaN? I can't find it myself and I would like my code to work with different compilers.
Please refer to the normative section Annex F: IEC 60559 floating-point arithmetic of the C standard:
F.1 Introduction
An implementation that defines
__STDC_IEC_559__
shall conform to the specifications in this annex.Implementations that do not define
__STDC_IEC_559__
are not required to conform to these specifications.
F.9.3 Relational operators
The expression
x ≠ x
is true ifx
is aNaN
.The expression
x = x
is false ifX
is aNan
.
F.3 Operators and functions
The
isnan
macro in<math.h>
provides theisnan
function recommended in the Appendix to IEC 60559.
这篇关于“x!= x”是测试NaN的便携方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!