Is `x!=x` a portable way to test for NaN?

后端 未结 2 780
野性不改
野性不改 2021-02-13 13:09

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

2条回答
  •  不要未来只要你来
    2021-02-13 13:54

    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 provides the isnan function recommended in the Appendix to IEC 60559.

提交回复
热议问题