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
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Globalization;
36 using System.Runtime.CompilerServices;
41 public struct Double : IComparable, IFormattable, IConvertible {
42 public const double Epsilon = 4.9406564584124650e-324;
43 public const double MaxValue = 1.7976931348623157e308;
44 public const double MinValue = -1.7976931348623157e308;
45 public const double NaN = 0.0d / 0.0d;
46 public const double NegativeInfinity = -1.0d / 0.0d;
47 public const double PositiveInfinity = 1.0d / 0.0d;
49 internal double m_value;
51 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52 extern internal static void AssertEndianity (out double value);
54 public int CompareTo (object v)
59 if (!(v is System.Double))
60 throw new ArgumentException (Locale.GetText ("Value is not a System.Double"));
62 double dv = (double)v;
64 if (IsPositiveInfinity(m_value) && IsPositiveInfinity(dv))
67 if (IsNegativeInfinity(m_value) && IsNegativeInfinity(dv))
82 if (m_value > dv) return 1;
83 else if (m_value < dv) return -1;
87 public override bool Equals (object o)
89 if (!(o is System.Double))
92 if (IsNaN ((double)o)) {
99 return ((double) o) == m_value;
102 public override int GetHashCode ()
104 return (int) m_value;
107 public static bool IsInfinity (double d)
109 return (d == PositiveInfinity || d == NegativeInfinity);
112 public static bool IsNaN (double d)
117 public static bool IsNegativeInfinity (double d)
119 return (d < 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
122 public static bool IsPositiveInfinity (double d)
124 return (d > 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
127 public static double Parse (string s)
129 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
132 public static double Parse (string s, IFormatProvider fp)
134 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
137 public static double Parse (string s, NumberStyles style)
139 return Parse (s, style, null);
142 // We're intentionally using constants here to avoid some bigger headaches in mcs.
143 // This struct must be compiled before System.Enum so we can't use enums here.
144 private const int State_AllowSign = 1;
145 private const int State_Digits = 2;
146 private const int State_Decimal = 3;
147 private const int State_ExponentSign = 4;
148 private const int State_Exponent = 5;
149 private const int State_ConsumeWhiteSpace = 6;
151 [MonoTODO("check if digits are group in correct numbers between the group separators")]
152 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
154 if (s == null) throw new ArgumentNullException();
155 if (style > NumberStyles.Any)
157 throw new ArgumentException();
159 NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
160 if (format == null) throw new Exception("How did this happen?");
161 if (s == format.NaNSymbol) return Double.NaN;
162 if (s == format.PositiveInfinitySymbol) return Double.PositiveInfinity;
163 if (s == format.NegativeInfinitySymbol) return Double.NegativeInfinity;
166 // validate and prepare string for C
169 byte [] b = new byte [len + 1];
174 if ((style & NumberStyles.AllowLeadingWhite) != 0){
175 while (sidx < len && Char.IsWhiteSpace (c = s [sidx]))
179 throw new FormatException();
182 bool allow_trailing_white = ((style & NumberStyles.AllowTrailingWhite) != 0);
187 int state = State_AllowSign;
192 string decimal_separator = null;
193 string group_separator = null;
194 int decimal_separator_len = 0;
195 int group_separator_len = 0;
196 if ((style & NumberStyles.AllowDecimalPoint) != 0){
197 decimal_separator = format.NumberDecimalSeparator;
198 decimal_separator_len = decimal_separator.Length;
200 if ((style & NumberStyles.AllowThousands) != 0){
201 group_separator = format.NumberGroupSeparator;
202 group_separator_len = group_separator.Length;
204 string positive = format.PositiveSign;
205 string negative = format.NegativeSign;
207 for (; sidx < len; sidx++){
211 case State_AllowSign:
212 if ((style & NumberStyles.AllowLeadingSign) != 0){
213 if (c == positive [0] &&
214 s.Substring (sidx, positive.Length) == positive){
215 state = State_Digits;
216 sidx += positive.Length-1;
220 if (c == negative [0] &&
221 s.Substring (sidx, negative.Length) == negative){
222 state = State_Digits;
223 b [didx++] = (byte) '-';
224 sidx += negative.Length-1;
228 state = State_Digits;
229 goto case State_Digits;
232 if (Char.IsDigit (c)){
233 b [didx++] = (byte) c;
236 if (c == 'e' || c == 'E')
237 goto case State_Decimal;
239 if (decimal_separator != null &&
240 decimal_separator [0] == c){
241 if (s.Substring (sidx, decimal_separator_len) ==
243 b [didx++] = (byte) '.';
244 sidx += decimal_separator_len-1;
245 state = State_Decimal;
249 if (group_separator != null &&
250 group_separator [0] == c){
251 if (s.Substring (sidx, group_separator_len) ==
253 sidx += group_separator_len-1;
254 state = State_Digits;
259 if (Char.IsWhiteSpace (c))
260 goto case State_ConsumeWhiteSpace;
262 throw new FormatException ("Unknown char: " + c);
265 if (Char.IsDigit (c)){
266 b [didx++] = (byte) c;
270 if (c == 'e' || c == 'E'){
271 if ((style & NumberStyles.AllowExponent) == 0)
272 throw new FormatException ("Unknown char: " + c);
273 b [didx++] = (byte) c;
274 state = State_ExponentSign;
278 if (Char.IsWhiteSpace (c))
279 goto case State_ConsumeWhiteSpace;
280 throw new FormatException ("Unknown char: " + c);
282 case State_ExponentSign:
283 if (Char.IsDigit (c)){
284 state = State_Exponent;
285 goto case State_Exponent;
288 if (c == positive [0] &&
289 s.Substring (sidx, positive.Length) == positive){
290 state = State_Digits;
291 sidx += positive.Length-1;
295 if (c == negative [0] &&
296 s.Substring (sidx, negative.Length) == negative){
297 state = State_Digits;
298 b [didx++] = (byte) '-';
299 sidx += negative.Length-1;
303 if (Char.IsWhiteSpace (c))
304 goto case State_ConsumeWhiteSpace;
306 throw new FormatException ("Unknown char: " + c);
309 if (Char.IsDigit (c)){
310 b [didx++] = (byte) c;
314 if (Char.IsWhiteSpace (c))
315 goto case State_ConsumeWhiteSpace;
316 throw new FormatException ("Unknown char: " + c);
318 case State_ConsumeWhiteSpace:
319 if (allow_trailing_white && Char.IsWhiteSpace (c))
321 throw new FormatException ("Unknown char");
327 fixed (byte *p = &b [0]){
328 double retVal = ParseImpl (p);
329 if (IsPositiveInfinity(retVal) || IsNegativeInfinity(retVal))
330 throw new OverflowException();
337 [MethodImplAttribute(MethodImplOptions.InternalCall)]
338 unsafe private static extern double ParseImpl (byte *byte_ptr);
340 public static bool TryParse (string s,
342 IFormatProvider provider,
346 result = Parse (s, style, provider);
354 public override string ToString ()
356 return ToString (null, null);
359 public string ToString (IFormatProvider fp)
361 return ToString (null, fp);
364 public string ToString (string format)
366 return ToString (format, null);
369 public string ToString (string format, IFormatProvider fp)
371 if (fp is CultureInfo)
372 return DoubleFormatter.NumberToString(format,
373 ((CultureInfo)fp).NumberFormat, m_value);
375 return DoubleFormatter.NumberToString(format,
376 (NumberFormatInfo)fp, m_value);
379 // =========== IConvertible Methods =========== //
381 public TypeCode GetTypeCode ()
383 return TypeCode.Double;
386 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
388 return System.Convert.ToType(m_value, conversionType, provider);
391 bool IConvertible.ToBoolean (IFormatProvider provider)
393 return System.Convert.ToBoolean(m_value);
396 byte IConvertible.ToByte (IFormatProvider provider)
398 return System.Convert.ToByte(m_value);
401 char IConvertible.ToChar (IFormatProvider provider)
403 throw new InvalidCastException();
406 DateTime IConvertible.ToDateTime (IFormatProvider provider)
408 throw new InvalidCastException();
411 decimal IConvertible.ToDecimal (IFormatProvider provider)
413 return System.Convert.ToDecimal(m_value);
416 double IConvertible.ToDouble (IFormatProvider provider)
418 return System.Convert.ToDouble(m_value);
421 short IConvertible.ToInt16 (IFormatProvider provider)
423 return System.Convert.ToInt16(m_value);
426 int IConvertible.ToInt32 (IFormatProvider provider)
428 return System.Convert.ToInt32(m_value);
431 long IConvertible.ToInt64 (IFormatProvider provider)
433 return System.Convert.ToInt64(m_value);
436 sbyte IConvertible.ToSByte (IFormatProvider provider)
438 return System.Convert.ToSByte(m_value);
441 float IConvertible.ToSingle (IFormatProvider provider)
443 return System.Convert.ToSingle(m_value);
447 string IConvertible.ToString (IFormatProvider provider)
449 return ToString(provider);
453 ushort IConvertible.ToUInt16 (IFormatProvider provider)
455 return System.Convert.ToUInt16(m_value);
458 uint IConvertible.ToUInt32 (IFormatProvider provider)
460 return System.Convert.ToUInt32(m_value);
463 ulong IConvertible.ToUInt64 (IFormatProvider provider)
465 return System.Convert.ToUInt64(m_value);