2001-12-06 Dietmar Maurer <dietmar@ximian.com>
[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 using System.Globalization;
13
14 namespace System {
15         
16         public struct Double : IComparable, IFormattable { //, IConvertible {
17                 public const double Epsilon = 4.9406564584124650e-324;
18                 public const double MaxValue =  1.7976931348623157e308;
19                 public const double MinValue = -1.7976931348623157e308;
20                 public const double NaN = 0.0d / 0.0d;
21                 public const double NegativeInfinity = -1.0d / 0.0d;
22                 public const double PositiveInfinity = 1.0d / 0.0d;
23                 
24                 // VES needs to know about value.  public is workaround
25                 // so source will compile
26                 public double value;
27
28                 public int CompareTo (object v)
29                 {
30                         if (v == null)
31                                 return 1;
32                         
33                         if (!(v is System.Double))
34                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Double"));
35
36                         return (int) (value - ((double) v));
37                 }
38
39                 public override bool Equals (object o)
40                 {
41                         if (!(o is System.Double))
42                                 return false;
43
44                         return ((double) o) == value;
45                 }
46
47                 public override int GetHashCode ()
48                 {
49                         return (int) value;
50                 }
51
52                 public static bool IsInfinity (double d)
53                 {
54                         return (d == PositiveInfinity || d == NegativeInfinity);
55                 }
56
57                 public static bool IsNaN (double d)
58                 {
59                         return (d != d);
60                 }
61
62                 public static bool IsNegativeInfinity (double d)
63                 {
64                         return (d < 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
65                 }
66
67                 public static bool IsPositiveInfinity (double d)
68                 {
69                         return (d > 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
70                 }
71
72                 public static double Parse (string s)
73                 {
74                         return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
75                 }
76
77                 public static double Parse (string s, IFormatProvider fp)
78                 {
79                         return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
80                 }
81
82                 public static double Parse (string s, NumberStyles style) 
83                 {
84                         return Parse (s, style, null);
85                 }
86
87                 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
88                 {
89                         if (s == null) throw new ArgumentNullException();
90                         if (style > NumberStyles.Any)
91                         {
92                                 throw new ArgumentException();
93                         }
94                         NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
95                         if (format == null) throw new Exception("How did this happen?");
96                         if (s == format.NaNSymbol) return Double.NaN;
97                         if (s == format.PositiveInfinitySymbol) return Double.PositiveInfinity;
98                         if (s == format.NegativeInfinitySymbol) return Double.NegativeInfinity;
99                         string[] sl;
100                         long integral = 0;
101                         long fraction = 0;
102                         long exponent = 1;
103                         double retval = 0;
104                         if ((style & NumberStyles.AllowLeadingWhite) != 0)
105                         {
106                                 s.TrimStart(null);
107                         }
108                         if ((style & NumberStyles.AllowTrailingWhite) != 0)
109                         {
110                                 s.TrimEnd(null);
111                         }
112                         sl = s.Split(new Char[] {'e', 'E'}, 2);
113                         if (sl.Length > 1)
114                         {
115                                 if ((style & NumberStyles.AllowExponent) == 0)
116                                 {
117                                         throw new FormatException();
118                                 }
119                                 exponent = long.Parse(sl[1], NumberStyles.AllowLeadingSign, format);
120                         }
121                         s = sl[0];
122                         sl = s.Split(format.NumberDecimalSeparator.ToCharArray(), 2);
123                         if (sl.Length > 1)
124                         {
125                                 if ((style & NumberStyles.AllowDecimalPoint) == 0)
126                                 {
127                                         throw new FormatException();
128                                 }
129                                 fraction = long.Parse(sl[1], NumberStyles.None, format);
130                         }
131                         NumberStyles tempstyle = NumberStyles.None;
132                         if ((style & NumberStyles.AllowLeadingSign) != 0){
133                                 tempstyle = NumberStyles.AllowLeadingSign;
134                         }
135                         integral = long.Parse(sl[0], tempstyle, format);
136                         retval = fraction;
137                         while (retval >1) retval /= 10;
138                         if (integral < 0){
139                                 retval -= integral;
140                                 retval = -retval;
141                         }
142                         else retval += integral;
143                         if (exponent != 1) retval *= Math.Pow(10, exponent);
144                         return retval;
145                 }
146
147                 public override string ToString ()
148                 {
149                         return ToString (null, null);
150                 }
151
152                 public string ToString (IFormatProvider fp)
153                 {
154                         return ToString (null, fp);
155                 }
156
157                 public string ToString (string format)
158                 {
159                         return ToString (format, null);
160                 }
161
162                 public string ToString (string format, IFormatProvider fp)
163                 {
164                         throw new NotImplementedException ();
165                 }
166
167                 // =========== IConvertible Methods =========== //
168
169                 public TypeCode GetTypeCode ()
170                 {
171                         return TypeCode.Double;
172                 }
173         }
174 }