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;
17 public struct Double : IComparable, IFormattable, IConvertible {
18 public const double Epsilon = 4.9406564584124650e-324;
19 public const double MaxValue = 1.7976931348623157e308;
20 public const double MinValue = -1.7976931348623157e308;
21 public const double NaN = 0.0d / 0.0d;
22 public const double NegativeInfinity = -1.0d / 0.0d;
23 public const double PositiveInfinity = 1.0d / 0.0d;
25 // VES needs to know about value. public is workaround
26 // so source will compile
29 public int CompareTo (object v)
34 if (!(v is System.Double))
35 throw new ArgumentException (Locale.GetText ("Value is not a System.Double"));
37 if (IsPositiveInfinity(value) && IsPositiveInfinity((double) v)){
41 if (IsNegativeInfinity(value) && IsNegativeInfinity((double) v)){
45 if (IsNaN((double) v)) {
52 return (int) (value - ((double) v));
55 public override bool Equals (object o)
57 if (!(o is System.Double))
60 if (IsNaN ((double)o)) {
67 return ((double) o) == value;
70 public override int GetHashCode ()
75 public static bool IsInfinity (double d)
77 return (d == PositiveInfinity || d == NegativeInfinity);
80 public static bool IsNaN (double d)
85 public static bool IsNegativeInfinity (double d)
87 return (d < 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
90 public static bool IsPositiveInfinity (double d)
92 return (d > 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
95 public static double Parse (string s)
97 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
100 public static double Parse (string s, IFormatProvider fp)
102 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
105 public static double Parse (string s, NumberStyles style)
107 return Parse (s, style, null);
110 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
112 if (s == null) throw new ArgumentNullException();
113 if (style > NumberStyles.Any)
115 throw new ArgumentException();
117 NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
118 if (format == null) throw new Exception("How did this happen?");
119 if (s == format.NaNSymbol) return Double.NaN;
120 if (s == format.PositiveInfinitySymbol) return Double.PositiveInfinity;
121 if (s == format.NegativeInfinitySymbol) return Double.NegativeInfinity;
127 if ((style & NumberStyles.AllowLeadingWhite) != 0)
131 if ((style & NumberStyles.AllowTrailingWhite) != 0)
135 sl = s.Split(new Char[] {'e', 'E'}, 2);
138 if ((style & NumberStyles.AllowExponent) == 0)
140 throw new FormatException();
142 exponent = long.Parse(sl[1], NumberStyles.AllowLeadingSign, format);
145 sl = s.Split(format.NumberDecimalSeparator.ToCharArray(), 2);
148 if ((style & NumberStyles.AllowDecimalPoint) == 0)
150 throw new FormatException();
152 fraction = long.Parse(sl[1], NumberStyles.None, format);
154 NumberStyles tempstyle = NumberStyles.None;
155 if ((style & NumberStyles.AllowLeadingSign) != 0){
156 tempstyle = NumberStyles.AllowLeadingSign;
158 integral = long.Parse(sl[0], tempstyle, format);
160 while (retval >1) retval /= 10;
165 else retval += integral;
166 if (exponent != 1) retval *= Math.Pow(10, exponent);
170 public override string ToString ()
172 return ToString (null, null);
175 public string ToString (IFormatProvider fp)
177 return ToString (null, fp);
180 public string ToString (string format)
182 return ToString (format, null);
186 public string ToString (string format, IFormatProvider fp)
188 throw new NotImplementedException ();
191 // =========== IConvertible Methods =========== //
193 public TypeCode GetTypeCode ()
195 return TypeCode.Double;
198 public object ToType (Type conversionType, IFormatProvider provider)
200 return System.Convert.ToType(value, conversionType, provider);
203 public bool ToBoolean (IFormatProvider provider)
205 return System.Convert.ToBoolean(value);
208 public byte ToByte (IFormatProvider provider)
210 return System.Convert.ToByte(value);
213 public char ToChar (IFormatProvider provider)
215 throw new InvalidCastException();
218 [CLSCompliant(false)]
219 public DateTime ToDateTime (IFormatProvider provider)
221 throw new InvalidCastException();
224 public decimal ToDecimal (IFormatProvider provider)
226 return System.Convert.ToDecimal(value);
229 public double ToDouble (IFormatProvider provider)
231 return System.Convert.ToDouble(value);
234 public short ToInt16 (IFormatProvider provider)
236 return System.Convert.ToInt16(value);
239 public int ToInt32 (IFormatProvider provider)
241 return System.Convert.ToInt32(value);
244 public long ToInt64 (IFormatProvider provider)
246 return System.Convert.ToInt64(value);
249 [CLSCompliant(false)]
250 public sbyte ToSByte (IFormatProvider provider)
252 return System.Convert.ToSByte(value);
255 public float ToSingle (IFormatProvider provider)
257 return System.Convert.ToSingle(value);
260 string IConvertible.ToString (IFormatProvider provider)
262 return ToString(provider);
265 [CLSCompliant(false)]
266 public ushort ToUInt16 (IFormatProvider provider)
268 return System.Convert.ToUInt16(value);
271 [CLSCompliant(false)]
272 public uint ToUInt32 (IFormatProvider provider)
274 return System.Convert.ToUInt32(value);
277 [CLSCompliant(false)]
278 public ulong ToUInt64 (IFormatProvider provider)
280 return System.Convert.ToUInt64(value);