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;
13 using System.Runtime.CompilerServices;
18 public struct Double : IComparable, IFormattable, IConvertible {
19 public const double Epsilon = 4.9406564584124650e-324;
20 public const double MaxValue = 1.7976931348623157e308;
21 public const double MinValue = -1.7976931348623157e308;
22 public const double NaN = 0.0d / 0.0d;
23 public const double NegativeInfinity = -1.0d / 0.0d;
24 public const double PositiveInfinity = 1.0d / 0.0d;
26 // VES needs to know about value. public is workaround
27 // so source will compile
30 public int CompareTo (object v)
35 if (!(v is System.Double))
36 throw new ArgumentException (Locale.GetText ("Value is not a System.Double"));
38 if (IsPositiveInfinity(value) && IsPositiveInfinity((double) v)){
42 if (IsNegativeInfinity(value) && IsNegativeInfinity((double) v)){
46 if (IsNaN((double) v)) {
53 return (int) (value - ((double) v));
56 public override bool Equals (object o)
58 if (!(o is System.Double))
61 if (IsNaN ((double)o)) {
68 return ((double) o) == value;
71 public override int GetHashCode ()
76 public static bool IsInfinity (double d)
78 return (d == PositiveInfinity || d == NegativeInfinity);
81 public static bool IsNaN (double d)
86 public static bool IsNegativeInfinity (double d)
88 return (d < 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
91 public static bool IsPositiveInfinity (double d)
93 return (d > 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
96 public static double Parse (string s)
98 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
101 public static double Parse (string s, IFormatProvider fp)
103 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
106 public static double Parse (string s, NumberStyles style)
108 return Parse (s, style, null);
111 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
113 if (s == null) throw new ArgumentNullException();
114 if (style > NumberStyles.Any)
116 throw new ArgumentException();
118 NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
119 if (format == null) throw new Exception("How did this happen?");
120 if (s == format.NaNSymbol) return Double.NaN;
121 if (s == format.PositiveInfinitySymbol) return Double.PositiveInfinity;
122 if (s == format.NegativeInfinitySymbol) return Double.NegativeInfinity;
128 if ((style & NumberStyles.AllowLeadingWhite) != 0)
132 if ((style & NumberStyles.AllowTrailingWhite) != 0)
136 sl = s.Split(new Char[] {'e', 'E'}, 2);
139 if ((style & NumberStyles.AllowExponent) == 0)
141 throw new FormatException();
143 exponent = long.Parse(sl[1], NumberStyles.AllowLeadingSign, format);
146 sl = s.Split(format.NumberDecimalSeparator.ToCharArray(), 2);
149 if ((style & NumberStyles.AllowDecimalPoint) == 0)
151 throw new FormatException();
153 fraction = long.Parse(sl[1], NumberStyles.None, format);
155 NumberStyles tempstyle = NumberStyles.None;
156 if ((style & NumberStyles.AllowLeadingSign) != 0){
157 tempstyle = NumberStyles.AllowLeadingSign;
159 integral = long.Parse(sl[0], tempstyle, format);
161 while (retval >1) retval /= 10;
166 else retval += integral;
167 if (exponent != 1) retval *= Math.Pow(10, exponent);
171 public override string ToString ()
173 return ToString (null, null);
176 public string ToString (IFormatProvider fp)
178 return ToString (null, fp);
181 public string ToString (string format)
183 return ToString (format, null);
187 public string ToString (string format, IFormatProvider fp)
189 // FIXME: Need to pass format and provider info to this call too.
190 return ToStringImpl(value);
193 [MethodImplAttribute(MethodImplOptions.InternalCall)]
194 private static extern string ToStringImpl (double value);
196 // =========== IConvertible Methods =========== //
198 public TypeCode GetTypeCode ()
200 return TypeCode.Double;
203 public object ToType (Type conversionType, IFormatProvider provider)
205 return System.Convert.ToType(value, conversionType, provider);
208 public bool ToBoolean (IFormatProvider provider)
210 return System.Convert.ToBoolean(value);
213 public byte ToByte (IFormatProvider provider)
215 return System.Convert.ToByte(value);
218 public char ToChar (IFormatProvider provider)
220 throw new InvalidCastException();
223 [CLSCompliant(false)]
224 public DateTime ToDateTime (IFormatProvider provider)
226 throw new InvalidCastException();
229 public decimal ToDecimal (IFormatProvider provider)
231 return System.Convert.ToDecimal(value);
234 public double ToDouble (IFormatProvider provider)
236 return System.Convert.ToDouble(value);
239 public short ToInt16 (IFormatProvider provider)
241 return System.Convert.ToInt16(value);
244 public int ToInt32 (IFormatProvider provider)
246 return System.Convert.ToInt32(value);
249 public long ToInt64 (IFormatProvider provider)
251 return System.Convert.ToInt64(value);
254 [CLSCompliant(false)]
255 public sbyte ToSByte (IFormatProvider provider)
257 return System.Convert.ToSByte(value);
260 public float ToSingle (IFormatProvider provider)
262 return System.Convert.ToSingle(value);
265 string IConvertible.ToString (IFormatProvider provider)
267 return ToString(provider);
270 [CLSCompliant(false)]
271 public ushort ToUInt16 (IFormatProvider provider)
273 return System.Convert.ToUInt16(value);
276 [CLSCompliant(false)]
277 public uint ToUInt32 (IFormatProvider provider)
279 return System.Convert.ToUInt32(value);
282 [CLSCompliant(false)]
283 public ulong ToUInt64 (IFormatProvider provider)
285 return System.Convert.ToUInt64(value);