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);
112 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
114 if (s == null) throw new ArgumentNullException();
115 if (style > NumberStyles.Any)
117 throw new ArgumentException();
119 NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
120 if (format == null) throw new Exception("How did this happen?");
121 if (s == format.NaNSymbol) return Double.NaN;
122 if (s == format.PositiveInfinitySymbol) return Double.PositiveInfinity;
123 if (s == format.NegativeInfinitySymbol) return Double.NegativeInfinity;
129 if ((style & NumberStyles.AllowLeadingWhite) != 0)
133 if ((style & NumberStyles.AllowTrailingWhite) != 0)
137 sl = s.Split(new Char[] {'e', 'E'}, 2);
140 if ((style & NumberStyles.AllowExponent) == 0)
142 throw new FormatException();
144 exponent = long.Parse(sl[1], NumberStyles.AllowLeadingSign, format);
147 sl = s.Split(format.NumberDecimalSeparator.ToCharArray(), 2);
150 if ((style & NumberStyles.AllowDecimalPoint) == 0)
152 throw new FormatException();
154 fraction = long.Parse(sl[1], NumberStyles.None, format);
156 NumberStyles tempstyle = NumberStyles.None;
157 if ((style & NumberStyles.AllowLeadingSign) != 0){
158 tempstyle = NumberStyles.AllowLeadingSign;
161 if (sl[0].Length > 0)
162 integral = long.Parse(sl[0], tempstyle, format);
168 // FIXME: what about the zeros between the decimal point
169 // and the first non-zero digit?
170 while (retval >1) retval /= 10;
175 else retval += integral;
176 if (exponent != 1) retval *= Math.Pow(10, exponent);
180 public override string ToString ()
182 return ToString (null, null);
185 public string ToString (IFormatProvider fp)
187 return ToString (null, fp);
190 public string ToString (string format)
192 return ToString (format, null);
196 public string ToString (string format, IFormatProvider fp)
198 // FIXME: Need to pass format and provider info to this call too.
199 return ToStringImpl(value);
202 [MethodImplAttribute(MethodImplOptions.InternalCall)]
203 private static extern string ToStringImpl (double value);
205 // =========== IConvertible Methods =========== //
207 public TypeCode GetTypeCode ()
209 return TypeCode.Double;
212 public object ToType (Type conversionType, IFormatProvider provider)
214 return System.Convert.ToType(value, conversionType, provider);
217 public bool ToBoolean (IFormatProvider provider)
219 return System.Convert.ToBoolean(value);
222 public byte ToByte (IFormatProvider provider)
224 return System.Convert.ToByte(value);
227 public char ToChar (IFormatProvider provider)
229 throw new InvalidCastException();
232 [CLSCompliant(false)]
233 public DateTime ToDateTime (IFormatProvider provider)
235 throw new InvalidCastException();
238 public decimal ToDecimal (IFormatProvider provider)
240 return System.Convert.ToDecimal(value);
243 public double ToDouble (IFormatProvider provider)
245 return System.Convert.ToDouble(value);
248 public short ToInt16 (IFormatProvider provider)
250 return System.Convert.ToInt16(value);
253 public int ToInt32 (IFormatProvider provider)
255 return System.Convert.ToInt32(value);
258 public long ToInt64 (IFormatProvider provider)
260 return System.Convert.ToInt64(value);
263 [CLSCompliant(false)]
264 public sbyte ToSByte (IFormatProvider provider)
266 return System.Convert.ToSByte(value);
269 public float ToSingle (IFormatProvider provider)
271 return System.Convert.ToSingle(value);
274 string IConvertible.ToString (IFormatProvider provider)
276 return ToString(provider);
279 [CLSCompliant(false)]
280 public ushort ToUInt16 (IFormatProvider provider)
282 return System.Convert.ToUInt16(value);
285 [CLSCompliant(false)]
286 public uint ToUInt32 (IFormatProvider provider)
288 return System.Convert.ToUInt32(value);
291 [CLSCompliant(false)]
292 public ulong ToUInt64 (IFormatProvider provider)
294 return System.Convert.ToUInt64(value);