“x!= x”是测试NaN的便携方式吗? [英] Is `x!=x` a portable way to test for NaN?

查看:171
本文介绍了“x!= x”是测试NaN的便携方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中,如果使用 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 x NaN

<$ c> p>如果 X 是一个 Nan ,则表达式 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 if x is a NaN.

The expression x = x is false if X is a Nan.

F.3 Operators and functions

The isnan macro in <math.h> provides the isnan function recommended in the Appendix to IEC 60559.

这篇关于“x!= x”是测试NaN的便携方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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