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

前端 未结 3 728
被撕碎了的回忆
被撕碎了的回忆 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 18:57

    Since I see an answer impugning the standards compliance of Intel's compiler, and no one else has mentioned this, I will point out that both GCC and Clang have a mode in which they do something quite similar. Their default behavior is IEEE-compliant —

    $ g++ -O2 test.cc && ./a.out 
    neg: -nan
    sub: nan nan nan
    add: nan nan
    div: nan nan nan
    mul: nan nan
    
    $ clang++ -O2 test.cc && ./a.out 
    neg: -nan
    sub: -nan nan nan
    add: nan nan
    div: nan nan nan
    mul: nan nan
    

    — but if you ask for speed at the expense of correctness, you get what you ask for —

    $ g++ -O2 -ffast-math test.cc && ./a.out 
    neg: -nan
    sub: nan nan 0.000000
    add: nan nan
    div: nan nan 1.000000
    mul: nan nan
    
    $ clang++ -O2 -ffast-math test.cc && ./a.out 
    neg: -nan
    sub: -nan nan 0.000000
    add: nan nan
    div: nan nan nan
    mul: nan nan
    

    I think it is entirely fair to criticize ICC's choice of default, but I would not read the entire Unix wars back into that decision.

提交回复
热议问题