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;
39 using System.Runtime.ConstrainedExecution;
45 public struct Double : IComparable, IFormattable, IConvertible
47 , IComparable <double>, IEquatable <double>
50 public const double Epsilon = 4.9406564584124650e-324;
51 public const double MaxValue = 1.7976931348623157e308;
52 public const double MinValue = -1.7976931348623157e308;
53 public const double NaN = 0.0d / 0.0d;
54 public const double NegativeInfinity = -1.0d / 0.0d;
55 public const double PositiveInfinity = 1.0d / 0.0d;
57 internal double m_value;
59 [MethodImplAttribute(MethodImplOptions.InternalCall)]
60 extern internal static void AssertEndianity (out double value);
62 public int CompareTo (object v)
67 if (!(v is System.Double))
68 throw new ArgumentException (Locale.GetText ("Value is not a System.Double"));
70 double dv = (double)v;
72 if (IsPositiveInfinity(m_value) && IsPositiveInfinity(dv))
75 if (IsNegativeInfinity(m_value) && IsNegativeInfinity(dv))
90 if (m_value > dv) return 1;
91 else if (m_value < dv) return -1;
95 public override bool Equals (object o)
97 if (!(o is System.Double))
100 if (IsNaN ((double)o)) {
107 return ((double) o) == m_value;
111 public int CompareTo (double value)
113 if (IsPositiveInfinity(m_value) && IsPositiveInfinity(value))
116 if (IsNegativeInfinity(m_value) && IsNegativeInfinity(value))
131 if (m_value > value) return 1;
132 else if (m_value < value) return -1;
136 public bool Equals (double value)
145 return value == m_value;
149 public override unsafe int GetHashCode ()
152 return (*((long*)&d)).GetHashCode ();
155 public static bool IsInfinity (double d)
157 return (d == PositiveInfinity || d == NegativeInfinity);
161 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
163 public static bool IsNaN (double d)
168 public static bool IsNegativeInfinity (double d)
170 return (d < 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
173 public static bool IsPositiveInfinity (double d)
175 return (d > 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
178 public static double Parse (string s)
180 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
183 public static double Parse (string s, IFormatProvider fp)
185 return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
188 public static double Parse (string s, NumberStyles style)
190 return Parse (s, style, null);
193 // We're intentionally using constants here to avoid some bigger headaches in mcs.
194 // This struct must be compiled before System.Enum so we can't use enums here.
195 private const int State_AllowSign = 1;
196 private const int State_Digits = 2;
197 private const int State_Decimal = 3;
198 private const int State_ExponentSign = 4;
199 private const int State_Exponent = 5;
200 private const int State_ConsumeWhiteSpace = 6;
202 [MonoTODO("check if digits are group in correct numbers between the group separators")]
203 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
205 if (s == null) throw new ArgumentNullException();
206 if (style > NumberStyles.Any)
208 throw new ArgumentException();
210 NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
211 if (format == null) throw new Exception("How did this happen?");
212 if (s == format.NaNSymbol) return Double.NaN;
213 if (s == format.PositiveInfinitySymbol) return Double.PositiveInfinity;
214 if (s == format.NegativeInfinitySymbol) return Double.NegativeInfinity;
217 // validate and prepare string for C
220 byte [] b = new byte [len + 1];
225 if ((style & NumberStyles.AllowLeadingWhite) != 0){
226 while (sidx < len && Char.IsWhiteSpace (c = s [sidx]))
230 throw new FormatException();
233 bool allow_trailing_white = ((style & NumberStyles.AllowTrailingWhite) != 0);
238 int state = State_AllowSign;
243 string decimal_separator = null;
244 string group_separator = null;
245 int decimal_separator_len = 0;
246 int group_separator_len = 0;
247 if ((style & NumberStyles.AllowDecimalPoint) != 0){
248 decimal_separator = format.NumberDecimalSeparator;
249 decimal_separator_len = decimal_separator.Length;
251 if ((style & NumberStyles.AllowThousands) != 0){
252 group_separator = format.NumberGroupSeparator;
253 group_separator_len = group_separator.Length;
255 string positive = format.PositiveSign;
256 string negative = format.NegativeSign;
258 for (; sidx < len; sidx++){
266 case State_AllowSign:
267 if ((style & NumberStyles.AllowLeadingSign) != 0){
268 if (c == positive [0] &&
269 s.Substring (sidx, positive.Length) == positive){
270 state = State_Digits;
271 sidx += positive.Length-1;
275 if (c == negative [0] &&
276 s.Substring (sidx, negative.Length) == negative){
277 state = State_Digits;
278 b [didx++] = (byte) '-';
279 sidx += negative.Length-1;
283 state = State_Digits;
284 goto case State_Digits;
287 if (Char.IsDigit (c)){
288 b [didx++] = (byte) c;
291 if (c == 'e' || c == 'E')
292 goto case State_Decimal;
294 if (decimal_separator != null &&
295 decimal_separator [0] == c){
296 if (s.Substring (sidx, decimal_separator_len) ==
298 b [didx++] = (byte) '.';
299 sidx += decimal_separator_len-1;
300 state = State_Decimal;
304 if (group_separator != null &&
305 group_separator [0] == c){
306 if (s.Substring (sidx, group_separator_len) ==
308 sidx += group_separator_len-1;
309 state = State_Digits;
314 if (Char.IsWhiteSpace (c))
315 goto case State_ConsumeWhiteSpace;
317 throw new FormatException ("Unknown char: " + c);
320 if (Char.IsDigit (c)){
321 b [didx++] = (byte) c;
325 if (c == 'e' || c == 'E'){
326 if ((style & NumberStyles.AllowExponent) == 0)
327 throw new FormatException ("Unknown char: " + c);
328 b [didx++] = (byte) c;
329 state = State_ExponentSign;
333 if (Char.IsWhiteSpace (c))
334 goto case State_ConsumeWhiteSpace;
335 throw new FormatException ("Unknown char: " + c);
337 case State_ExponentSign:
338 if (Char.IsDigit (c)){
339 state = State_Exponent;
340 goto case State_Exponent;
343 if (c == positive [0] &&
344 s.Substring (sidx, positive.Length) == positive){
345 state = State_Digits;
346 sidx += positive.Length-1;
350 if (c == negative [0] &&
351 s.Substring (sidx, negative.Length) == negative){
352 state = State_Digits;
353 b [didx++] = (byte) '-';
354 sidx += negative.Length-1;
358 if (Char.IsWhiteSpace (c))
359 goto case State_ConsumeWhiteSpace;
361 throw new FormatException ("Unknown char: " + c);
364 if (Char.IsDigit (c)){
365 b [didx++] = (byte) c;
369 if (Char.IsWhiteSpace (c))
370 goto case State_ConsumeWhiteSpace;
371 throw new FormatException ("Unknown char: " + c);
373 case State_ConsumeWhiteSpace:
374 if (allow_trailing_white && Char.IsWhiteSpace (c))
376 throw new FormatException ("Unknown char");
382 fixed (byte *p = &b [0]){
383 double retVal = ParseImpl (p);
384 if (IsPositiveInfinity(retVal) || IsNegativeInfinity(retVal))
385 throw new OverflowException();
392 [MethodImplAttribute(MethodImplOptions.InternalCall)]
393 unsafe private static extern double ParseImpl (byte *byte_ptr);
395 public static bool TryParse (string s,
397 IFormatProvider provider,
401 result = Parse (s, style, provider);
409 public override string ToString ()
411 return ToString (null, null);
414 public string ToString (IFormatProvider fp)
416 return ToString (null, fp);
419 public string ToString (string format)
421 return ToString (format, null);
424 public string ToString (string format, IFormatProvider fp)
426 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (fp);
427 return NumberFormatter.NumberToString (format, m_value, nfi);
430 // =========== IConvertible Methods =========== //
432 public TypeCode GetTypeCode ()
434 return TypeCode.Double;
437 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
439 return System.Convert.ToType(m_value, conversionType, provider);
442 bool IConvertible.ToBoolean (IFormatProvider provider)
444 return System.Convert.ToBoolean(m_value);
447 byte IConvertible.ToByte (IFormatProvider provider)
449 return System.Convert.ToByte(m_value);
452 char IConvertible.ToChar (IFormatProvider provider)
454 throw new InvalidCastException();
457 DateTime IConvertible.ToDateTime (IFormatProvider provider)
459 throw new InvalidCastException();
462 decimal IConvertible.ToDecimal (IFormatProvider provider)
464 return System.Convert.ToDecimal(m_value);
467 double IConvertible.ToDouble (IFormatProvider provider)
469 return System.Convert.ToDouble(m_value);
472 short IConvertible.ToInt16 (IFormatProvider provider)
474 return System.Convert.ToInt16(m_value);
477 int IConvertible.ToInt32 (IFormatProvider provider)
479 return System.Convert.ToInt32(m_value);
482 long IConvertible.ToInt64 (IFormatProvider provider)
484 return System.Convert.ToInt64(m_value);
487 sbyte IConvertible.ToSByte (IFormatProvider provider)
489 return System.Convert.ToSByte(m_value);
492 float IConvertible.ToSingle (IFormatProvider provider)
494 return System.Convert.ToSingle(m_value);
498 string IConvertible.ToString (IFormatProvider provider)
500 return ToString(provider);
504 ushort IConvertible.ToUInt16 (IFormatProvider provider)
506 return System.Convert.ToUInt16(m_value);
509 uint IConvertible.ToUInt32 (IFormatProvider provider)
511 return System.Convert.ToUInt32(m_value);
514 ulong IConvertible.ToUInt64 (IFormatProvider provider)
516 return System.Convert.ToUInt64(m_value);