5 // Miguel de Icaza (miguel@ximian.com)
6 // Bob Smith (bob@thestuff.net)
8 // (C) Ximian, Inc. http://www.ximian.com
9 // (C) Bob Smith. http://www.thestuff.net
12 using System.Globalization;
16 public struct Double : IComparable, IFormattable { //, IConvertible {
17 public const double Epsilon = 4.9406564584124650e-324;
18 public const double MaxValue = 1.7976931348623157e308;
19 public const double MinValue = -1.7976931348623157e308;
20 public const double NaN = 0.0d / 0.0d;
21 public const double NegativeInfinity = -1.0d / 0.0d;
22 public const double PositiveInfinity = 1.0d / 0.0d;
24 // VES needs to know about value. public is workaround
25 // so source will compile
28 public int CompareTo (object v)
33 if (!(v is System.Double))
34 throw new ArgumentException (Locale.GetText ("Value is not a System.Double"));
36 return (int) (value - ((double) v));
39 public override bool Equals (object o)
41 if (!(o is System.Double))
44 return ((double) o) == value;
47 public override int GetHashCode ()
52 public static bool IsInfinity (double d)
54 return (d == PositiveInfinity || d == NegativeInfinity);
57 public static bool IsNaN (double d)
62 public static bool IsNegativeInfinity (double d)
64 return (d < 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
67 public static bool IsPositiveInfinity (double d)
69 return (d > 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
72 public static double Parse (string s)
74 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
77 public static double Parse (string s, IFormatProvider fp)
79 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
82 public static double Parse (string s, NumberStyles style)
84 return Parse (s, style, null);
87 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
89 if (s == null) throw new ArgumentNullException();
90 if (style > NumberStyles.Any)
92 throw new ArgumentException();
94 NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
95 if (format == null) throw new Exception("How did this happen?");
96 if (s == format.NaNSymbol) return Double.NaN;
97 if (s == format.PositiveInfinitySymbol) return Double.PositiveInfinity;
98 if (s == format.NegativeInfinitySymbol) return Double.NegativeInfinity;
104 if ((style & NumberStyles.AllowLeadingWhite) != 0)
108 if ((style & NumberStyles.AllowTrailingWhite) != 0)
112 sl = s.Split(new Char[] {'e', 'E'}, 2);
115 if ((style & NumberStyles.AllowExponent) == 0)
117 throw new FormatException();
119 exponent = long.Parse(sl[1], NumberStyles.AllowLeadingSign, format);
122 sl = s.Split(format.NumberDecimalSeparator.ToCharArray(), 2);
125 if ((style & NumberStyles.AllowDecimalPoint) == 0)
127 throw new FormatException();
129 fraction = long.Parse(sl[1], NumberStyles.None, format);
131 NumberStyles tempstyle = NumberStyles.None;
132 if ((style & NumberStyles.AllowLeadingSign) != 0){
133 tempstyle = NumberStyles.AllowLeadingSign;
135 integral = long.Parse(sl[0], tempstyle, format);
137 while (retval >1) retval /= 10;
142 else retval += integral;
143 if (exponent != 1) retval *= Math.Pow(10, exponent);
147 public override string ToString ()
149 return ToString (null, null);
152 public string ToString (IFormatProvider fp)
154 return ToString (null, fp);
157 public string ToString (string format)
159 return ToString (format, null);
162 public string ToString (string format, IFormatProvider fp)
164 throw new NotImplementedException ();
167 // =========== IConvertible Methods =========== //
169 public TypeCode GetTypeCode ()
171 return TypeCode.Double;