## NaN(非数字), PositiveInfinity(无穷大), NegativeInfinity(无穷小)

   1: using System;
   2: using System.Collections.Generic;
   3:
   4: namespace Roger.Testing
   5: {
   6:     public class Testing
   7:     {
   8:         public static void Main()
   9:         {
  10:             double a = 3.1415926;
  11:             double b = 3.1415926;
  12:
  13:             Console.WriteLine("<- Comparing Values ->");
  14:             Console.WriteLine("Equals: {0}", a.Equals(b));
  15:             Console.WriteLine("==: {0}", a == b);
  16:
  17:             Console.WriteLine("<- Comparing NaNs ->");
  18:             double c = double.NaN;
  19:             double d = double.NaN;
  20:             Console.WriteLine("Equals: {0}", c.Equals(d));
  21:             Console.WriteLine("==: {0}", c == d);
  22:
  23:             Console.WriteLine("<- Comparing PositiveInfinity ->");
  24:             double e = double.PositiveInfinity;
  25:             double f = double.PositiveInfinity;
  26:             Console.WriteLine("Equals: {0}", e.Equals(f));
  27:             Console.WriteLine("==: {0}", e == f);
  28:
  29:             Console.Read();
  30:         }
  31:     }
  32: }

Although two floating point NaNs are defined by IEC 60559:1989 to always compare as unequal, the System.Object.Equals requires that overrides must satisfy the requirements for an equivalence contract for operator.  Therefore, System.Double.Equals and System.Single.Equals return True when comparing two NaNs, while the equality operator returns False in that case, as required by the IEC standard.

1. a == a // true

2. if a == b then b == a

3. if a == b and b == c then a == c

   1:
   2:         // True if obj is another Double with the same value as the current instance.  This is
   3:         // a method of object equality, that only returns true if obj is also a double.
   4:         public override bool Equals(Object obj) {
   5:             if (!(obj is Double)) {
   6:                 return false;
   7:             }
   8:             double temp = ((Double)obj).m_value;
   9:             // This code below is written this way for performance reasons i.e the != and == check is intentional.
  10:             if (temp == m_value) {
  11:                 return true;
  12:             }
  13:             return IsNaN(temp) && IsNaN(m_value);
  14:         }

   1: public static bool IsNaN(double d)
   2:         {
   3:             //Jit will generate inlineable code with this
   4: // warning CS1718: comparison to same variable
   5: #pragma warning disable 1718
   6:             if (d != d)
   7:             {
   8:                 return true;
   9:             }
  10:             else
  11:             {
  12:                 return false;
  13:             }
  14: #pragma warning restore 1718
  15:         }

   1: public const double NegativeInfinity = (double)-1.0 / (double)(0.0);
   2:         public const double PositiveInfinity = (double)1.0 / (double)(0.0);
   3:         public const double NaN = (double)0.0 / (double)0.0;

From WiKI

1. if exponent is 0 and fraction is 0, the number is ±0 (depending on the sign bit)
2. if exponent = 2e − 1 and fraction is 0, the number is ±infinity (again depending on the sign bit), and
3. if exponent = 2e − 1 and fraction is not 0, the number being represented is not a number (NaN).

X rem 0 = NaN  // X 除以 0 = NaN
0 *  +infinity = 0 * -infinity = NaN
(X / 0) = +infinity, if X > 0
NaN, if X = 0
infinity, if X < 0
NaN op X = X op NaN = NaN for all operations
(+infinity) + (+infinity) =  (+infinity)
X / (+infinity) = 0
X mod (-infinity) = -X
(+infinity) - (+infinity) =  NaN

Have Fun, :)

posted on 2008-08-09 14:53  xwang  阅读(...)  评论(...编辑  收藏

• 随笔 - 92
• 文章 - 0
• 评论 - 252
• 引用 - 22