所以如果反编译.net源,你可以找到这段代码
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SecuritySafeCritical]
[__DynamicallyInvokable]
public static unsafe bool IsNaN(double d)
{
return (ulong) (*(long*) &d & long.MaxValue) > 9218868437227405312UL;
}
所以根据 IEEE754NaN != NaN
所以问题很简单:为什么它看起来不像
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SecuritySafeCritical]
[__DynamicallyInvokable]
public static unsafe bool IsNaN(double d)
{
return d != d;
}
我的朋友告诉我,实施==
可能是return !IsNan(this) && this.InnerEquals(other);
但是 NaN 的 afaik 实现在处理器本身的硬件层上是硬编码的。我们不应该NaN
单独处理案件。
还有一个问题。为什么这么傻?
bool b1 = (double.NaN == double.NaN); // false
bool b2 = double.NaN.Equals(double.NaN); //true
我知道一个实现
[__DynamicallyInvokable]
public override bool Equals(object obj)
{
if (!(obj is double))
return false;
double d = (double) obj;
if (d == this)
return true;
if (double.IsNaN(d))
return double.IsNaN(this);
else
return false;
}
但不知道为什么