Comparing Double.NaN with itself

后端 未结 4 1293
迷失自我
迷失自我 2021-02-06 21:58

I am stuck trying to find out why these two operations return different values:

  1. Double.NaN == Double.NaN returns false
  2. Do
4条回答
  •  梦如初夏
    2021-02-06 22:13

    Well, Oded's answer is great but I want to say something;

    When I decompile Double.Equals() method, it seems like this;

    public bool Equals(double obj)
    {
        return ((obj == this) || (IsNaN(obj) && IsNaN(this)));
    }
    

    So since we have this = Double.NaN and obj = Double.NaN

    (IsNaN(obj)) and (IsNaN(this)) returns `true`.
    

    So basicly it is could return ((obj == this) || true

    which is equvalent to

    return ((obj == this) is true.

提交回复
热议问题