NaN comparison rule in C/C++

前端 未结 2 437
再見小時候
再見小時候 2021-01-04 06:09

Doing some optimziation on a piece of code, the correctness of the code depending on how the compiler handle NaNs.

I read the IEEE-754 rules on NaN, which states:

2条回答
  •  青春惊慌失措
    2021-01-04 07:03

    C/C++ does not require specific floating-point representation and does not require that any comparison against NaN is false.

    In C++ you can check if all floating-point types fulfill IEEE 754 using std::numeric_limits::is_iec559:

    static constexpr bool is_iec559;

    56 True if and only if the type adheres to IEC 559 standard.217

    57 Meaningful for all floating point types.


    217) International Electrotechnical Commission standard 559 is the same as IEEE 754.

    For other floating-point representations comparison against NaN may or may not behave the same way.

    In fact, even representing NaN itself is not required. See std::numeric_limits::has_quiet_NaN, std::numeric_limits::has_signaling_NaN.

提交回复
热议问题