Switch to compiler-tester
[mono.git] / mcs / class / corlib / System / Double.cs
1 //
2 // System.Double.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Bob Smith       (bob@thestuff.net)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // (C) Bob Smith.    http://www.thestuff.net
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
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:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
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.
33 //
34
35 using System.Globalization;
36 using System.Runtime.CompilerServices;
37
38 #if NET_2_0
39 using System.Runtime.ConstrainedExecution;
40 #endif
41
42 namespace System {
43         
44         [Serializable]
45         public struct Double : IComparable, IFormattable, IConvertible
46 #if NET_2_0
47                 , IComparable <double>, IEquatable <double>
48 #endif
49         {
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;
56                 
57                 internal double m_value;
58
59                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
60                 extern internal static void AssertEndianity (out double value);
61
62                 public int CompareTo (object v)
63                 {
64                         if (v == null)
65                                 return 1;
66                         
67                         if (!(v is System.Double))
68                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Double"));
69
70                         double dv = (double)v;
71
72                         if (IsPositiveInfinity(m_value) && IsPositiveInfinity(dv))
73                                 return 0;
74
75                         if (IsNegativeInfinity(m_value) && IsNegativeInfinity(dv))
76                                 return 0;
77
78                         if (IsNaN(dv))
79                                 if (IsNaN(m_value))
80                                         return 0;
81                                 else
82                                         return 1;
83
84                         if (IsNaN(m_value))
85                                 if (IsNaN(dv))
86                                         return 0;
87                                 else
88                                         return -1;
89
90                         if (m_value > dv) return 1;
91                         else if (m_value < dv) return -1;
92                         else return 0;
93                 }
94
95                 public override bool Equals (object o)
96                 {
97                         if (!(o is System.Double))
98                                 return false;
99
100                         if (IsNaN ((double)o)) {
101                                 if (IsNaN(m_value))
102                                         return true;
103                                 else
104                                         return false;
105                         }
106
107                         return ((double) o) == m_value;
108                 }
109
110 #if NET_2_0
111                 public int CompareTo (double value)
112                 {
113                         if (IsPositiveInfinity(m_value) && IsPositiveInfinity(value))
114                                 return 0;
115
116                         if (IsNegativeInfinity(m_value) && IsNegativeInfinity(value))
117                                 return 0;
118
119                         if (IsNaN(value))
120                                 if (IsNaN(m_value))
121                                         return 0;
122                                 else
123                                         return 1;
124
125                         if (IsNaN(m_value))
126                                 if (IsNaN(value))
127                                         return 0;
128                                 else
129                                         return -1;
130
131                         if (m_value > value) return 1;
132                         else if (m_value < value) return -1;
133                         else return 0;
134                 }
135
136                 public bool Equals (double value)
137                 {
138                         if (IsNaN (value)) {
139                                 if (IsNaN(m_value))
140                                         return true;
141                                 else
142                                         return false;
143                         }
144
145                         return value == m_value;
146                 }
147 #endif
148
149                 public override unsafe int GetHashCode ()
150                 {
151                         double d = m_value;
152                         return (*((long*)&d)).GetHashCode ();
153                 }
154
155                 public static bool IsInfinity (double d)
156                 {
157                         return (d == PositiveInfinity || d == NegativeInfinity);
158                 }
159
160 #if NET_2_0
161                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
162 #endif
163                 public static bool IsNaN (double d)
164                 {
165                         return (d != d);
166                 }
167
168                 public static bool IsNegativeInfinity (double d)
169                 {
170                         return (d < 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
171                 }
172
173                 public static bool IsPositiveInfinity (double d)
174                 {
175                         return (d > 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
176                 }
177
178                 public static double Parse (string s)
179                 {
180                         return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
181                 }
182
183                 public static double Parse (string s, IFormatProvider fp)
184                 {
185                         return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
186                 }
187
188                 public static double Parse (string s, NumberStyles style) 
189                 {
190                         return Parse (s, style, null);
191                 }
192
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;
201                 
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)
204                 {
205                         if (s == null) throw new ArgumentNullException();
206                         if (style > NumberStyles.Any)
207                         {
208                                 throw new ArgumentException();
209                         }
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;
215
216                         //
217                         // validate and prepare string for C
218                         //
219                         int len = s.Length;
220                         byte [] b = new byte [len + 1];
221                         int didx = 0;
222                         int sidx = 0;
223                         char c;
224                         
225                         if ((style & NumberStyles.AllowLeadingWhite) != 0){
226                                 while (sidx < len && Char.IsWhiteSpace (c = s [sidx]))
227                                        sidx++;
228
229                                 if (sidx == len)
230                                         throw new FormatException();
231                         }
232
233                         bool allow_trailing_white = ((style & NumberStyles.AllowTrailingWhite) != 0);
234
235                         //
236                         // Machine state
237                         //
238                         int state = State_AllowSign;
239
240                         //
241                         // Setup
242                         //
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;
250                         }
251                         if ((style & NumberStyles.AllowThousands) != 0){
252                                 group_separator = format.NumberGroupSeparator;
253                                 group_separator_len = group_separator.Length;
254                         }
255                         string positive = format.PositiveSign;
256                         string negative = format.NegativeSign;
257                         
258                         for (; sidx < len; sidx++){
259                                 c = s [sidx];
260
261                                 if (c == '\0') {
262                                         sidx = len;
263                                         continue;
264                                 }
265                                 switch (state){
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;
272                                                         continue;
273                                                 }
274
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;
280                                                         continue;
281                                                 }
282                                         }
283                                         state = State_Digits;
284                                         goto case State_Digits;
285                                         
286                                 case State_Digits:
287                                         if (Char.IsDigit (c)){
288                                                 b [didx++] = (byte) c;
289                                                 break;
290                                         }
291                                         if (c == 'e' || c == 'E')
292                                                 goto case State_Decimal;
293                                         
294                                         if (decimal_separator != null &&
295                                             decimal_separator [0] == c){
296                                                 if (s.Substring (sidx, decimal_separator_len) ==
297                                                     decimal_separator){
298                                                         b [didx++] = (byte) '.';
299                                                         sidx += decimal_separator_len-1;
300                                                         state = State_Decimal; 
301                                                         break;
302                                                 }
303                                         }
304                                         if (group_separator != null &&
305                                             group_separator [0] == c){
306                                                 if (s.Substring (sidx, group_separator_len) ==
307                                                     group_separator){
308                                                         sidx += group_separator_len-1;
309                                                         state = State_Digits; 
310                                                         break;
311                                                 }
312                                         }
313                                         
314                                         if (Char.IsWhiteSpace (c))
315                                                 goto case State_ConsumeWhiteSpace;
316
317                                         throw new FormatException ("Unknown char: " + c);
318
319                                 case State_Decimal:
320                                         if (Char.IsDigit (c)){
321                                                 b [didx++] = (byte) c;
322                                                 break;
323                                         }
324
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;
330                                                 break;
331                                         }
332                                         
333                                         if (Char.IsWhiteSpace (c))
334                                                 goto case State_ConsumeWhiteSpace;
335                                         throw new FormatException ("Unknown char: " + c);
336
337                                 case State_ExponentSign:
338                                         if (Char.IsDigit (c)){
339                                                 state = State_Exponent;
340                                                 goto case State_Exponent;
341                                         }
342
343                                         if (c == positive [0] &&
344                                             s.Substring (sidx, positive.Length) == positive){
345                                                 state = State_Digits;
346                                                 sidx += positive.Length-1;
347                                                 continue;
348                                         }
349
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;
355                                                 continue;
356                                         }
357
358                                         if (Char.IsWhiteSpace (c))
359                                                 goto case State_ConsumeWhiteSpace;
360                                         
361                                         throw new FormatException ("Unknown char: " + c);
362
363                                 case State_Exponent:
364                                         if (Char.IsDigit (c)){
365                                                 b [didx++] = (byte) c;
366                                                 break;
367                                         }
368                                         
369                                         if (Char.IsWhiteSpace (c))
370                                                 goto case State_ConsumeWhiteSpace;
371                                         throw new FormatException ("Unknown char: " + c);
372
373                                 case State_ConsumeWhiteSpace:
374                                         if (allow_trailing_white && Char.IsWhiteSpace (c))
375                                                 break;
376                                         throw new FormatException ("Unknown char");
377                                 }
378                         }
379
380                         b [didx] = 0;
381                         unsafe {
382                                 fixed (byte *p = &b [0]){
383                                         double retVal = ParseImpl (p);
384                                         if (IsPositiveInfinity(retVal) || IsNegativeInfinity(retVal))
385                                                 throw new OverflowException();
386
387                                         return retVal;
388                                 }
389                         }
390                 }
391
392                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
393                 unsafe private static extern double ParseImpl (byte *byte_ptr);
394                 
395                 public static bool TryParse (string s,
396                                              NumberStyles style,
397                                              IFormatProvider provider,
398                                              out double result)
399                 {
400                         try {
401                                 result = Parse (s, style, provider);
402                                 return true;
403                         } catch {
404                                 result = 0;
405                                 return false;
406                         }
407                 }
408
409                 public override string ToString ()
410                 {
411                         return ToString (null, null);
412                 }
413
414                 public string ToString (IFormatProvider fp)
415                 {
416                         return ToString (null, fp);
417                 }
418
419                 public string ToString (string format)
420                 {
421                         return ToString (format, null);
422                 }
423
424                 public string ToString (string format, IFormatProvider fp)
425                 {
426                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (fp);
427                         return NumberFormatter.NumberToString (format, m_value, nfi);
428                 }
429
430                 // =========== IConvertible Methods =========== //
431
432                 public TypeCode GetTypeCode ()
433                 {
434                         return TypeCode.Double;
435                 }
436
437                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
438                 {
439                         return System.Convert.ToType(m_value, conversionType, provider);
440                 }
441                 
442                 bool IConvertible.ToBoolean (IFormatProvider provider)
443                 {
444                         return System.Convert.ToBoolean(m_value);
445                 }
446                 
447                 byte IConvertible.ToByte (IFormatProvider provider)
448                 {
449                         return System.Convert.ToByte(m_value);
450                 }
451                 
452                 char IConvertible.ToChar (IFormatProvider provider)
453                 {
454                         throw new InvalidCastException();
455                 }
456                 
457                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
458                 {
459                         throw new InvalidCastException();
460                 }
461                 
462                 decimal IConvertible.ToDecimal (IFormatProvider provider)
463                 {
464                         return System.Convert.ToDecimal(m_value);
465                 }
466                 
467                 double IConvertible.ToDouble (IFormatProvider provider)
468                 {
469                         return System.Convert.ToDouble(m_value);
470                 }
471                 
472                 short IConvertible.ToInt16 (IFormatProvider provider)
473                 {
474                         return System.Convert.ToInt16(m_value);
475                 }
476                 
477                 int IConvertible.ToInt32 (IFormatProvider provider)
478                 {
479                         return System.Convert.ToInt32(m_value);
480                 }
481                 
482                 long IConvertible.ToInt64 (IFormatProvider provider)
483                 {
484                         return System.Convert.ToInt64(m_value);
485                 }
486                 
487                 sbyte IConvertible.ToSByte (IFormatProvider provider)
488                 {
489                         return System.Convert.ToSByte(m_value);
490                 }
491                 
492                 float IConvertible.ToSingle (IFormatProvider provider)
493                 {
494                         return System.Convert.ToSingle(m_value);
495                 }
496                 
497 /*
498                 string IConvertible.ToString (IFormatProvider provider)
499                 {
500                         return ToString(provider);
501                 }
502 */
503
504                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
505                 {
506                         return System.Convert.ToUInt16(m_value);
507                 }
508                 
509                 uint IConvertible.ToUInt32 (IFormatProvider provider)
510                 {
511                         return System.Convert.ToUInt32(m_value);
512                 }
513                 
514                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
515                 {
516                         return System.Convert.ToUInt64(m_value);
517                 }
518         }
519 }