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 (decimal_separator != null &&
194 decimal_separator [0] == c){
195 if (s.Substring (sidx, decimal_separator_len) ==
197 b [didx++] = (byte) '.';
198 sidx += decimal_separator_len-1;
199 state = State.Decimal;
204 if (Char.IsWhiteSpace (c))
205 goto case State.ConsumeWhiteSpace;
207 throw new FormatException ("Unknown char: " + c);
210 if (Char.IsDigit (c)){
211 b [didx++] = (byte) c;
215 if (c == 'e' || c == 'E'){
216 if ((style & NumberStyles.AllowExponent) == 0)
217 throw new FormatException ("Unknown char: " + c);
218 b [didx++] = (byte) c;
219 state = State.ExponentSign;
223 if (Char.IsWhiteSpace (c))
224 goto case State.ConsumeWhiteSpace;
225 throw new FormatException ("Unknown char: " + c);
227 case State.ExponentSign:
228 if (Char.IsDigit (c)){
229 state = State.Exponent;
230 goto case State.Exponent;
233 if (c == positive [0] &&
234 s.Substring (sidx, positive.Length) == positive){
235 state = State.Digits;
236 sidx += positive.Length-1;
240 if (c == negative [0] &&
241 s.Substring (sidx, negative.Length) == negative){
242 state = State.Digits;
243 b [didx++] = (byte) '-';
244 sidx += negative.Length-1;
248 if (Char.IsWhiteSpace (c))
249 goto case State.ConsumeWhiteSpace;
251 throw new FormatException ("Unknown char: " + c);
254 if (Char.IsDigit (c)){
255 b [didx++] = (byte) c;
259 if (Char.IsWhiteSpace (c))
260 goto case State.ConsumeWhiteSpace;
261 throw new FormatException ("Unknown char: " + c);
263 case State.ConsumeWhiteSpace:
264 if (allow_trailing_white && Char.IsWhiteSpace (c))
266 throw new FormatException ("Unknown char");
272 fixed (byte *p = &b [0]){
273 return ParseImpl (p);
278 [MethodImplAttribute(MethodImplOptions.InternalCall)]
279 unsafe private static extern double ParseImpl (byte *byte_ptr);
281 public override string ToString ()
283 return ToString (null, null);
286 public string ToString (IFormatProvider fp)
288 return ToString (null, fp);
291 public string ToString (string format)
293 return ToString (format, null);
297 public string ToString (string format, IFormatProvider fp)
299 // FIXME: Need to pass format and provider info to this call too.
300 return ToStringImpl(value);
303 [MethodImplAttribute(MethodImplOptions.InternalCall)]
304 private static extern string ToStringImpl (double value);
306 // =========== IConvertible Methods =========== //
308 public TypeCode GetTypeCode ()
310 return TypeCode.Double;
313 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
315 return System.Convert.ToType(value, conversionType, provider);
318 bool IConvertible.ToBoolean (IFormatProvider provider)
320 return System.Convert.ToBoolean(value);
323 byte IConvertible.ToByte (IFormatProvider provider)
325 return System.Convert.ToByte(value);
328 char IConvertible.ToChar (IFormatProvider provider)
330 throw new InvalidCastException();
333 [CLSCompliant(false)]
334 DateTime IConvertible.ToDateTime (IFormatProvider provider)
336 throw new InvalidCastException();
339 decimal IConvertible.ToDecimal (IFormatProvider provider)
341 return System.Convert.ToDecimal(value);
344 double IConvertible.ToDouble (IFormatProvider provider)
346 return System.Convert.ToDouble(value);
349 short IConvertible.ToInt16 (IFormatProvider provider)
351 return System.Convert.ToInt16(value);
354 int IConvertible.ToInt32 (IFormatProvider provider)
356 return System.Convert.ToInt32(value);
359 long IConvertible.ToInt64 (IFormatProvider provider)
361 return System.Convert.ToInt64(value);
364 [CLSCompliant(false)]
365 sbyte IConvertible.ToSByte (IFormatProvider provider)
367 return System.Convert.ToSByte(value);
370 float IConvertible.ToSingle (IFormatProvider provider)
372 return System.Convert.ToSingle(value);
376 string IConvertible.ToString (IFormatProvider provider)
378 return ToString(provider);
382 [CLSCompliant(false)]
383 ushort IConvertible.ToUInt16 (IFormatProvider provider)
385 return System.Convert.ToUInt16(value);
388 [CLSCompliant(false)]
389 uint IConvertible.ToUInt32 (IFormatProvider provider)
391 return System.Convert.ToUInt32(value);
394 [CLSCompliant(false)]
395 ulong IConvertible.ToUInt64 (IFormatProvider provider)
397 return System.Convert.ToUInt64(value);