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 AllowSign, Digits, Decimal, ExponentSign, Exponent, ConsumeWhiteSpace
115 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
117 if (s == null) throw new ArgumentNullException();
118 if (style > NumberStyles.Any)
120 throw new ArgumentException();
122 NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
123 if (format == null) throw new Exception("How did this happen?");
124 if (s == format.NaNSymbol) return Double.NaN;
125 if (s == format.PositiveInfinitySymbol) return Double.PositiveInfinity;
126 if (s == format.NegativeInfinitySymbol) return Double.NegativeInfinity;
129 // validate and prepare string for C
132 byte [] b = new byte [len + 1];
137 if ((style & NumberStyles.AllowLeadingWhite) != 0){
138 while (sidx < len && Char.IsWhiteSpace (c = s [sidx]))
142 throw new FormatException();
145 bool allow_trailing_white = ((style & NumberStyles.AllowTrailingWhite) != 0);
150 State state = State.AllowSign;
155 string decimal_separator = null;
156 int decimal_separator_len = 0;
157 if ((style & NumberStyles.AllowDecimalPoint) != 0){
158 decimal_separator = format.NumberDecimalSeparator;
159 decimal_separator_len = decimal_separator.Length;
161 string positive = format.PositiveSign;
162 string negative = format.NegativeSign;
164 for (; sidx < len; sidx++){
168 case State.AllowSign:
169 if ((style & NumberStyles.AllowLeadingSign) != 0){
170 if (c == positive [0] &&
171 s.Substring (sidx, positive.Length) == positive){
172 state = State.Digits;
173 sidx += positive.Length-1;
177 if (c == negative [0] &&
178 s.Substring (sidx, negative.Length) == negative){
179 state = State.Digits;
180 b [didx++] = (byte) '-';
181 sidx += negative.Length-1;
185 state = State.Digits;
186 goto case State.Digits;
189 if (Char.IsDigit (c)){
190 b [didx++] = (byte) c;
193 if (c == 'e' || c == 'E')
194 goto case State.Decimal;
196 if (decimal_separator != null &&
197 decimal_separator [0] == c){
198 if (s.Substring (sidx, decimal_separator_len) ==
200 b [didx++] = (byte) '.';
201 sidx += decimal_separator_len-1;
202 state = State.Decimal;
207 if (Char.IsWhiteSpace (c))
208 goto case State.ConsumeWhiteSpace;
210 throw new FormatException ("Unknown char: " + c);
213 if (Char.IsDigit (c)){
214 b [didx++] = (byte) c;
218 if (c == 'e' || c == 'E'){
219 if ((style & NumberStyles.AllowExponent) == 0)
220 throw new FormatException ("Unknown char: " + c);
221 b [didx++] = (byte) c;
222 state = State.ExponentSign;
226 if (Char.IsWhiteSpace (c))
227 goto case State.ConsumeWhiteSpace;
228 throw new FormatException ("Unknown char: " + c);
230 case State.ExponentSign:
231 if (Char.IsDigit (c)){
232 state = State.Exponent;
233 goto case State.Exponent;
236 if (c == positive [0] &&
237 s.Substring (sidx, positive.Length) == positive){
238 state = State.Digits;
239 sidx += positive.Length-1;
243 if (c == negative [0] &&
244 s.Substring (sidx, negative.Length) == negative){
245 state = State.Digits;
246 b [didx++] = (byte) '-';
247 sidx += negative.Length-1;
251 if (Char.IsWhiteSpace (c))
252 goto case State.ConsumeWhiteSpace;
254 throw new FormatException ("Unknown char: " + c);
257 if (Char.IsDigit (c)){
258 b [didx++] = (byte) c;
262 if (Char.IsWhiteSpace (c))
263 goto case State.ConsumeWhiteSpace;
264 throw new FormatException ("Unknown char: " + c);
266 case State.ConsumeWhiteSpace:
267 if (allow_trailing_white && Char.IsWhiteSpace (c))
269 throw new FormatException ("Unknown char");
275 fixed (byte *p = &b [0]){
276 return ParseImpl (p);
281 [MethodImplAttribute(MethodImplOptions.InternalCall)]
282 unsafe private static extern double ParseImpl (byte *byte_ptr);
284 public override string ToString ()
286 return ToString (null, null);
289 public string ToString (IFormatProvider fp)
291 return ToString (null, fp);
294 public string ToString (string format)
296 return ToString (format, null);
300 public string ToString (string format, IFormatProvider fp)
302 // FIXME: Need to pass format and provider info to this call too.
303 return ToStringImpl(value);
306 [MethodImplAttribute(MethodImplOptions.InternalCall)]
307 private static extern string ToStringImpl (double value);
309 // =========== IConvertible Methods =========== //
311 public TypeCode GetTypeCode ()
313 return TypeCode.Double;
316 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
318 return System.Convert.ToType(value, conversionType, provider);
321 bool IConvertible.ToBoolean (IFormatProvider provider)
323 return System.Convert.ToBoolean(value);
326 byte IConvertible.ToByte (IFormatProvider provider)
328 return System.Convert.ToByte(value);
331 char IConvertible.ToChar (IFormatProvider provider)
333 throw new InvalidCastException();
336 [CLSCompliant(false)]
337 DateTime IConvertible.ToDateTime (IFormatProvider provider)
339 throw new InvalidCastException();
342 decimal IConvertible.ToDecimal (IFormatProvider provider)
344 return System.Convert.ToDecimal(value);
347 double IConvertible.ToDouble (IFormatProvider provider)
349 return System.Convert.ToDouble(value);
352 short IConvertible.ToInt16 (IFormatProvider provider)
354 return System.Convert.ToInt16(value);
357 int IConvertible.ToInt32 (IFormatProvider provider)
359 return System.Convert.ToInt32(value);
362 long IConvertible.ToInt64 (IFormatProvider provider)
364 return System.Convert.ToInt64(value);
367 [CLSCompliant(false)]
368 sbyte IConvertible.ToSByte (IFormatProvider provider)
370 return System.Convert.ToSByte(value);
373 float IConvertible.ToSingle (IFormatProvider provider)
375 return System.Convert.ToSingle(value);
379 string IConvertible.ToString (IFormatProvider provider)
381 return ToString(provider);
385 [CLSCompliant(false)]
386 ushort IConvertible.ToUInt16 (IFormatProvider provider)
388 return System.Convert.ToUInt16(value);
391 [CLSCompliant(false)]
392 uint IConvertible.ToUInt32 (IFormatProvider provider)
394 return System.Convert.ToUInt32(value);
397 [CLSCompliant(false)]
398 ulong IConvertible.ToUInt64 (IFormatProvider provider)
400 return System.Convert.ToUInt64(value);