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 if (IsPositiveInfinity(value) && IsPositiveInfinity((double) v)){
40 if (IsNegativeInfinity(value) && IsNegativeInfinity((double) v)){
44 if (IsNaN((double) v)) {
51 return (int) (value - ((double) v));
54 public override bool Equals (object o)
56 if (!(o is System.Double))
59 if (IsNaN ((double)o)) {
66 return ((double) o) == value;
69 public override int GetHashCode ()
74 public static bool IsInfinity (double d)
76 return (d == PositiveInfinity || d == NegativeInfinity);
79 public static bool IsNaN (double d)
84 public static bool IsNegativeInfinity (double d)
86 return (d < 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
89 public static bool IsPositiveInfinity (double d)
91 return (d > 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
94 public static double Parse (string s)
96 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
99 public static double Parse (string s, IFormatProvider fp)
101 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
104 public static double Parse (string s, NumberStyles style)
106 return Parse (s, style, null);
109 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
111 if (s == null) throw new ArgumentNullException();
112 if (style > NumberStyles.Any)
114 throw new ArgumentException();
116 NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
117 if (format == null) throw new Exception("How did this happen?");
118 if (s == format.NaNSymbol) return Double.NaN;
119 if (s == format.PositiveInfinitySymbol) return Double.PositiveInfinity;
120 if (s == format.NegativeInfinitySymbol) return Double.NegativeInfinity;
126 if ((style & NumberStyles.AllowLeadingWhite) != 0)
130 if ((style & NumberStyles.AllowTrailingWhite) != 0)
134 sl = s.Split(new Char[] {'e', 'E'}, 2);
137 if ((style & NumberStyles.AllowExponent) == 0)
139 throw new FormatException();
141 exponent = long.Parse(sl[1], NumberStyles.AllowLeadingSign, format);
144 sl = s.Split(format.NumberDecimalSeparator.ToCharArray(), 2);
147 if ((style & NumberStyles.AllowDecimalPoint) == 0)
149 throw new FormatException();
151 fraction = long.Parse(sl[1], NumberStyles.None, format);
153 NumberStyles tempstyle = NumberStyles.None;
154 if ((style & NumberStyles.AllowLeadingSign) != 0){
155 tempstyle = NumberStyles.AllowLeadingSign;
157 integral = long.Parse(sl[0], tempstyle, format);
159 while (retval >1) retval /= 10;
164 else retval += integral;
165 if (exponent != 1) retval *= Math.Pow(10, exponent);
169 public override string ToString ()
171 return ToString (null, null);
174 public string ToString (IFormatProvider fp)
176 return ToString (null, fp);
179 public string ToString (string format)
181 return ToString (format, null);
185 public string ToString (string format, IFormatProvider fp)
187 throw new NotImplementedException ();
190 // =========== IConvertible Methods =========== //
192 public TypeCode GetTypeCode ()
194 return TypeCode.Double;
197 public object ToType (Type conversionType, IFormatProvider provider)
199 return System.Convert.ToType(value, conversionType, provider);
202 public bool ToBoolean (IFormatProvider provider)
204 return System.Convert.ToBoolean(value);
207 public byte ToByte (IFormatProvider provider)
209 return System.Convert.ToByte(value);
212 public char ToChar (IFormatProvider provider)
214 throw new InvalidCastException();
217 [CLSCompliant(false)]
218 public DateTime ToDateTime (IFormatProvider provider)
220 throw new InvalidCastException();
223 public decimal ToDecimal (IFormatProvider provider)
225 return System.Convert.ToDecimal(value);
228 public double ToDouble (IFormatProvider provider)
230 return System.Convert.ToDouble(value);
233 public short ToInt16 (IFormatProvider provider)
235 return System.Convert.ToInt16(value);
238 public int ToInt32 (IFormatProvider provider)
240 return System.Convert.ToInt32(value);
243 public long ToInt64 (IFormatProvider provider)
245 return System.Convert.ToInt64(value);
248 [CLSCompliant(false)]
249 public sbyte ToSByte (IFormatProvider provider)
251 return System.Convert.ToSByte(value);
254 public float ToSingle (IFormatProvider provider)
256 return System.Convert.ToSingle(value);
259 string IConvertible.ToString (IFormatProvider provider)
261 return ToString(provider);
264 [CLSCompliant(false)]
265 public ushort ToUInt16 (IFormatProvider provider)
267 return System.Convert.ToUInt16(value);
270 [CLSCompliant(false)]
271 public uint ToUInt32 (IFormatProvider provider)
273 return System.Convert.ToUInt32(value);
276 [CLSCompliant(false)]
277 public ulong ToUInt64 (IFormatProvider provider)
279 return System.Convert.ToUInt64(value);