double d1 = double.Nan;
double d2 = double.Nan;
Console.WriteLine(d1 == d2);
Console.WriteLine(d1.Equals(d2));上述代码第一行输出False
第二行输出True高手们有什么不同的解释?

解决方案 »

  1.   

    没有内存地址这种说法。
    d1 = 100;
    d2 = 100;
    的话,那么输出都是True
      

  2.   

    真的很奇怪,就算用
    Console.WriteLine(double.NaN == double.NaN);
    也会输出False
      

  3.   

    double数据类型本身的问题造成不同比较方法会有不同的结果吧,不器官,C++中好象提过这个类似的问题
      

  4.   

    可能是C#的约定吧,用其他double的字段(MaxValue等)没有出现这个问题,偏偏用NaN就出现问题了
      

  5.   

    说不大清楚,这是我找到的,你看看,可能有点用http://blog.joycode.com/sumtec/archive/2004/07/05/26678.aspx
      

  6.   

    找到了,来自MSDN的  备注
      此常数的值是零被零除所得到的结果。
      当运算结果未定义时返回此常数。
      使用 IsNaN 确定值是否为非数字。不可能通过将某个值与另一个等于 NaN 的值进行比较来确定该值是否不是数字。看来就是CLR的约定
      

  7.   

    继续,还是引用MSDN下面的代码示例演示如何使用 NaN: Visual Basic
    Dim zero As Double = 0' This condition will return false.
    If (0 / zero) = Double.NaN Then
        Console.WriteLine("0 / 0 can be tested with Double.NaN.")
    Else
        Console.WriteLine("0 / 0 cannot be tested with Double.NaN; use Double.IsNan() instead.")
    End If 
    C# 
    Double zero = 0;// This condition will return false.
    if ((0 / zero) == Double.NaN) 
    {
        Console.WriteLine("0 / 0 can be tested with Double.NaN.");

    else 
    {
        Console.WriteLine("0 / 0 cannot be tested with Double.NaN; use Double.IsNan() instead.");

    C++
    Double zero = 0;// This condition will return false.
    if ( (0 / zero) == Double::NaN )
    {
       Console::WriteLine( "0 / 0 can be tested with Double::NaN." );
    }
    else
    {
       Console::WriteLine( "0 / 0 cannot be tested with Double::NaN; use Double::IsNan() instead." );

    J#
    Double zero = new Double(0);// This condition will return false.
    if (0 / zero.doubleValue() == Double.NaN) {
        Console.WriteLine("0 / 0 can be tested with Double.NaN.");
    }
    else {
        Console.WriteLine(
            "0 / 0 cannot be tested with Double.NaN; " 
            + "use Double.IsNan() instead.");

    以上都会输出
    0 / 0 cannot be tested with Double.NaN; use Double.IsNan() instead.