Why does NaN - NaN == 0.0 with the Intel C++ Compiler?

前端 未结 3 725
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 18:22

It is well-known that NaNs propagate in arithmetic, but I couldn\'t find any demonstrations, so I wrote a small test:

#include 
#include 

        
3条回答
  •  無奈伤痛
    2021-01-29 19:04

    This . . . can't be right, right? My question: what do the relevant standards (ISO C, ISO C++, IEEE 754) say about this?

    Petr Abdulin already answered why the compiler gives a 0.0 answer.

    Here is what IEEE-754:2008 says:

    (6.2 Operations with NaNs) "[...] For an operation with quiet NaN inputs, other than maximum and minimum operations, if a floating-point result is to be delivered the result shall be a quiet NaN which should be one of the input NaNs."

    So the only valid result for the subtraction of two quiet NaN operand is a quiet NaN; any other result is not valid.

    The C Standard says:

    (C11, F.9.2 Expression transformations p1) "[...]

    x − x → 0. 0 "The expressions x − x and 0. 0 are not equivalent if x is a NaN or infinite"

    (where here NaN denotes a quiet NaN as per F.2.1p1 "This specification does not define the behavior of signaling NaNs. It generally uses the term NaN to denote quiet NaNs")

提交回复
热议问题