Why is double.NaN not equal to itself?

后端 未结 11 1266
心在旅途
心在旅途 2020-11-27 17:42

Can someone explain this to me? In C# double.NaN is not equal to double.NaN

bool huh = double.NaN == double.NaN; // huh = false
bool huh2 = double.NaN >=          


        
11条回答
  •  温柔的废话
    2020-11-27 18:16

    Actually, you already found the way to check if a IEEE-754 floating point number is NaN: it is the only floating point value (or range of values, because there are several NaNs) that evaluates to False if compared to itself, i.e. :

    bool isNaN(double v) {
        return v != v;
    }
    

    Under the hood, the Double.IsNaN method might actually do the same thing. You should still use it, because the behavior is quite surprising to anybody who does not know about the FP standard.

提交回复
热议问题