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 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
207 if (!Parse (s, style, provider, false, out result, out exc))
213 [MonoTODO("check if digits are group in correct numbers between the group separators")]
214 internal static bool Parse (string s, NumberStyles style, IFormatProvider provider, bool tryParse, out double result, out Exception exc)
221 exc = new ArgumentNullException ("s");
225 if (style > NumberStyles.Any) {
227 exc = new ArgumentException();
231 NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
232 if (format == null) throw new Exception("How did this happen?");
234 if (s == format.NaNSymbol) {
238 if (s == format.PositiveInfinitySymbol) {
239 result = Double.PositiveInfinity;
242 if (s == format.NegativeInfinitySymbol) {
243 result = Double.NegativeInfinity;
248 // validate and prepare string for C
251 byte [] b = new byte [len + 1];
256 if ((style & NumberStyles.AllowLeadingWhite) != 0){
257 while (sidx < len && Char.IsWhiteSpace (c = s [sidx]))
262 exc = Int32.GetFormatException ();
267 bool allow_trailing_white = ((style & NumberStyles.AllowTrailingWhite) != 0);
272 int state = State_AllowSign;
277 string decimal_separator = null;
278 string group_separator = null;
279 int decimal_separator_len = 0;
280 int group_separator_len = 0;
281 if ((style & NumberStyles.AllowDecimalPoint) != 0){
282 decimal_separator = format.NumberDecimalSeparator;
283 decimal_separator_len = decimal_separator.Length;
285 if ((style & NumberStyles.AllowThousands) != 0){
286 group_separator = format.NumberGroupSeparator;
287 group_separator_len = group_separator.Length;
289 string positive = format.PositiveSign;
290 string negative = format.NegativeSign;
292 for (; sidx < len; sidx++){
300 case State_AllowSign:
301 if ((style & NumberStyles.AllowLeadingSign) != 0){
302 if (c == positive [0] &&
303 s.Substring (sidx, positive.Length) == positive){
304 state = State_Digits;
305 sidx += positive.Length-1;
309 if (c == negative [0] &&
310 s.Substring (sidx, negative.Length) == negative){
311 state = State_Digits;
312 b [didx++] = (byte) '-';
313 sidx += negative.Length-1;
317 state = State_Digits;
318 goto case State_Digits;
321 if (Char.IsDigit (c)){
322 b [didx++] = (byte) c;
325 if (c == 'e' || c == 'E')
326 goto case State_Decimal;
328 if (decimal_separator != null &&
329 decimal_separator [0] == c){
330 if (s.Substring (sidx, decimal_separator_len) ==
332 b [didx++] = (byte) '.';
333 sidx += decimal_separator_len-1;
334 state = State_Decimal;
338 if (group_separator != null &&
339 group_separator [0] == c){
340 if (s.Substring (sidx, group_separator_len) ==
342 sidx += group_separator_len-1;
343 state = State_Digits;
348 if (Char.IsWhiteSpace (c))
349 goto case State_ConsumeWhiteSpace;
352 exc = new FormatException ("Unknown char: " + c);
356 if (Char.IsDigit (c)){
357 b [didx++] = (byte) c;
361 if (c == 'e' || c == 'E'){
362 if ((style & NumberStyles.AllowExponent) == 0)
363 throw new FormatException ("Unknown char: " + c);
364 b [didx++] = (byte) c;
365 state = State_ExponentSign;
369 if (Char.IsWhiteSpace (c))
370 goto case State_ConsumeWhiteSpace;
373 exc = new FormatException ("Unknown char: " + c);
376 case State_ExponentSign:
377 if (Char.IsDigit (c)){
378 state = State_Exponent;
379 goto case State_Exponent;
382 if (c == positive [0] &&
383 s.Substring (sidx, positive.Length) == positive){
384 state = State_Digits;
385 sidx += positive.Length-1;
389 if (c == negative [0] &&
390 s.Substring (sidx, negative.Length) == negative){
391 state = State_Digits;
392 b [didx++] = (byte) '-';
393 sidx += negative.Length-1;
397 if (Char.IsWhiteSpace (c))
398 goto case State_ConsumeWhiteSpace;
401 exc = new FormatException ("Unknown char: " + c);
405 if (Char.IsDigit (c)){
406 b [didx++] = (byte) c;
410 if (Char.IsWhiteSpace (c))
411 goto case State_ConsumeWhiteSpace;
414 exc = new FormatException ("Unknown char: " + c);
417 case State_ConsumeWhiteSpace:
418 if (allow_trailing_white && Char.IsWhiteSpace (c))
422 exc = new FormatException ("Unknown char");
429 fixed (byte *p = &b [0]){
431 if (!ParseImpl (p, out retVal)) {
433 exc = Int32.GetFormatException ();
436 if (IsPositiveInfinity(retVal) || IsNegativeInfinity(retVal)) {
438 exc = new OverflowException ();
448 [MethodImplAttribute(MethodImplOptions.InternalCall)]
449 unsafe private static extern bool ParseImpl (byte *byte_ptr, out double value);
451 public static bool TryParse (string s,
453 IFormatProvider provider,
457 if (!Parse (s, style, provider, true, out result, out exc)) {
465 public override string ToString ()
467 return ToString (null, null);
470 public string ToString (IFormatProvider fp)
472 return ToString (null, fp);
475 public string ToString (string format)
477 return ToString (format, null);
480 public string ToString (string format, IFormatProvider fp)
482 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (fp);
483 return NumberFormatter.NumberToString (format, m_value, nfi);
486 // =========== IConvertible Methods =========== //
488 public TypeCode GetTypeCode ()
490 return TypeCode.Double;
493 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
495 return System.Convert.ToType(m_value, conversionType, provider);
498 bool IConvertible.ToBoolean (IFormatProvider provider)
500 return System.Convert.ToBoolean(m_value);
503 byte IConvertible.ToByte (IFormatProvider provider)
505 return System.Convert.ToByte(m_value);
508 char IConvertible.ToChar (IFormatProvider provider)
510 throw new InvalidCastException();
513 DateTime IConvertible.ToDateTime (IFormatProvider provider)
515 throw new InvalidCastException();
518 decimal IConvertible.ToDecimal (IFormatProvider provider)
520 return System.Convert.ToDecimal(m_value);
523 double IConvertible.ToDouble (IFormatProvider provider)
525 return System.Convert.ToDouble(m_value);
528 short IConvertible.ToInt16 (IFormatProvider provider)
530 return System.Convert.ToInt16(m_value);
533 int IConvertible.ToInt32 (IFormatProvider provider)
535 return System.Convert.ToInt32(m_value);
538 long IConvertible.ToInt64 (IFormatProvider provider)
540 return System.Convert.ToInt64(m_value);
543 sbyte IConvertible.ToSByte (IFormatProvider provider)
545 return System.Convert.ToSByte(m_value);
548 float IConvertible.ToSingle (IFormatProvider provider)
550 return System.Convert.ToSingle(m_value);
554 string IConvertible.ToString (IFormatProvider provider)
556 return ToString(provider);
560 ushort IConvertible.ToUInt16 (IFormatProvider provider)
562 return System.Convert.ToUInt16(m_value);
565 uint IConvertible.ToUInt32 (IFormatProvider provider)
567 return System.Convert.ToUInt32(m_value);
570 ulong IConvertible.ToUInt64 (IFormatProvider provider)
572 return System.Convert.ToUInt64(m_value);