I always thought that "==" and Equals() give same result for double values...But this is not true
Example
var a = Double.NaN;
Console.WriteLine(a == a);
Console.ReadKey();
Prints "False"
var a = Double.NaN;
Console.WriteLine(a.Equals(a));
Console.ReadKey();
Prints "True"!
So, why Equals() met开发者_如何学JAVAhod implementation for floating point numbers does not follow floating point numbers specification, which says that if number is NaN - it is not equal to anything, even to itself?
P.S I merged my questions and will delete another about NaN's as soon as possible
P.P.S Equals realization (.NET 4, thanks to Ani)
public bool Equals(double obj)
{
return ((obj == this) || (IsNaN(obj) && IsNaN(this)));
}
精彩评论