added missing [Serializable] attribute
[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         [Serializable]
17         public struct Double : IComparable, IFormattable, IConvertible {
18                 public const double Epsilon = 4.9406564584124650e-324;
19                 public const double MaxValue =  1.7976931348623157e308;
20                 public const double MinValue = -1.7976931348623157e308;
21                 public const double NaN = 0.0d / 0.0d;
22                 public const double NegativeInfinity = -1.0d / 0.0d;
23                 public const double PositiveInfinity = 1.0d / 0.0d;
24                 
25                 // VES needs to know about value.  public is workaround
26                 // so source will compile
27                 public double value;
28
29                 public int CompareTo (object v)
30                 {
31                         if (v == null)
32                                 return 1;
33                         
34                         if (!(v is System.Double))
35                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Double"));
36
37                         if (IsPositiveInfinity(value) && IsPositiveInfinity((double) v)){
38                                 return 0;
39                         }
40
41                         if (IsNegativeInfinity(value) && IsNegativeInfinity((double) v)){
42                                 return 0;
43                         }
44
45                         if (IsNaN((double) v)) {
46                                 if (IsNaN(value))
47                                         return 0;
48                                 else
49                                         return 1;
50                         }
51
52                         return (int) (value - ((double) v));
53                 }
54
55                 public override bool Equals (object o)
56                 {
57                         if (!(o is System.Double))
58                                 return false;
59
60                         if (IsNaN ((double)o)) {
61                                 if (IsNaN(value))
62                                         return true;
63                                 else
64                                         return false;
65                         }
66
67                         return ((double) o) == value;
68                 }
69
70                 public override int GetHashCode ()
71                 {
72                         return (int) value;
73                 }
74
75                 public static bool IsInfinity (double d)
76                 {
77                         return (d == PositiveInfinity || d == NegativeInfinity);
78                 }
79
80                 public static bool IsNaN (double d)
81                 {
82                         return (d != d);
83                 }
84
85                 public static bool IsNegativeInfinity (double d)
86                 {
87                         return (d < 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
88                 }
89
90                 public static bool IsPositiveInfinity (double d)
91                 {
92                         return (d > 0.0d && (d == NegativeInfinity || d == PositiveInfinity));
93                 }
94
95                 public static double Parse (string s)
96                 {
97                         return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
98                 }
99
100                 public static double Parse (string s, IFormatProvider fp)
101                 {
102                         return Parse (s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
103                 }
104
105                 public static double Parse (string s, NumberStyles style) 
106                 {
107                         return Parse (s, style, null);
108                 }
109
110                 public static double Parse (string s, NumberStyles style, IFormatProvider provider)
111                 {
112                         if (s == null) throw new ArgumentNullException();
113                         if (style > NumberStyles.Any)
114                         {
115                                 throw new ArgumentException();
116                         }
117                         NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
118                         if (format == null) throw new Exception("How did this happen?");
119                         if (s == format.NaNSymbol) return Double.NaN;
120                         if (s == format.PositiveInfinitySymbol) return Double.PositiveInfinity;
121                         if (s == format.NegativeInfinitySymbol) return Double.NegativeInfinity;
122                         string[] sl;
123                         long integral = 0;
124                         long fraction = 0;
125                         long exponent = 1;
126                         double retval = 0;
127                         if ((style & NumberStyles.AllowLeadingWhite) != 0)
128                         {
129                                 s.TrimStart(null);
130                         }
131                         if ((style & NumberStyles.AllowTrailingWhite) != 0)
132                         {
133                                 s.TrimEnd(null);
134                         }
135                         sl = s.Split(new Char[] {'e', 'E'}, 2);
136                         if (sl.Length > 1)
137                         {
138                                 if ((style & NumberStyles.AllowExponent) == 0)
139                                 {
140                                         throw new FormatException();
141                                 }
142                                 exponent = long.Parse(sl[1], NumberStyles.AllowLeadingSign, format);
143                         }
144                         s = sl[0];
145                         sl = s.Split(format.NumberDecimalSeparator.ToCharArray(), 2);
146                         if (sl.Length > 1)
147                         {
148                                 if ((style & NumberStyles.AllowDecimalPoint) == 0)
149                                 {
150                                         throw new FormatException();
151                                 }
152                                 fraction = long.Parse(sl[1], NumberStyles.None, format);
153                         }
154                         NumberStyles tempstyle = NumberStyles.None;
155                         if ((style & NumberStyles.AllowLeadingSign) != 0){
156                                 tempstyle = NumberStyles.AllowLeadingSign;
157                         }
158                         integral = long.Parse(sl[0], tempstyle, format);
159                         retval = fraction;
160                         while (retval >1) retval /= 10;
161                         if (integral < 0){
162                                 retval -= integral;
163                                 retval = -retval;
164                         }
165                         else retval += integral;
166                         if (exponent != 1) retval *= Math.Pow(10, exponent);
167                         return retval;
168                 }
169
170                 public override string ToString ()
171                 {
172                         return ToString (null, null);
173                 }
174
175                 public string ToString (IFormatProvider fp)
176                 {
177                         return ToString (null, fp);
178                 }
179
180                 public string ToString (string format)
181                 {
182                         return ToString (format, null);
183                 }
184
185                 [MonoTODO]
186                 public string ToString (string format, IFormatProvider fp)
187                 {
188                         throw new NotImplementedException ();
189                 }
190
191                 // =========== IConvertible Methods =========== //
192
193                 public TypeCode GetTypeCode ()
194                 {
195                         return TypeCode.Double;
196                 }
197
198                 public object ToType (Type conversionType, IFormatProvider provider)
199                 {
200                         return System.Convert.ToType(value, conversionType, provider);
201                 }
202                 
203                 public bool ToBoolean (IFormatProvider provider)
204                 {
205                         return System.Convert.ToBoolean(value);
206                 }
207                 
208                 public byte ToByte (IFormatProvider provider)
209                 {
210                         return System.Convert.ToByte(value);
211                 }
212                 
213                 public char ToChar (IFormatProvider provider)
214                 {
215                         throw new InvalidCastException();
216                 }
217                 
218                 [CLSCompliant(false)]
219                 public DateTime ToDateTime (IFormatProvider provider)
220                 {
221                         throw new InvalidCastException();
222                 }
223                 
224                 public decimal ToDecimal (IFormatProvider provider)
225                 {
226                         return System.Convert.ToDecimal(value);
227                 }
228                 
229                 public double ToDouble (IFormatProvider provider)
230                 {
231                         return System.Convert.ToDouble(value);
232                 }
233                 
234                 public short ToInt16 (IFormatProvider provider)
235                 {
236                         return System.Convert.ToInt16(value);
237                 }
238                 
239                 public int ToInt32 (IFormatProvider provider)
240                 {
241                         return System.Convert.ToInt32(value);
242                 }
243                 
244                 public long ToInt64 (IFormatProvider provider)
245                 {
246                         return System.Convert.ToInt64(value);
247                 }
248                 
249                 [CLSCompliant(false)] 
250                 public sbyte ToSByte (IFormatProvider provider)
251                 {
252                         return System.Convert.ToSByte(value);
253                 }
254                 
255                 public float ToSingle (IFormatProvider provider)
256                 {
257                         return System.Convert.ToSingle(value);
258                 }
259                 
260                 string IConvertible.ToString (IFormatProvider provider)
261                 {
262                         return ToString(provider);
263                 }
264
265                 [CLSCompliant(false)]
266                 public ushort ToUInt16 (IFormatProvider provider)
267                 {
268                         return System.Convert.ToUInt16(value);
269                 }
270                 
271                 [CLSCompliant(false)]
272                 public uint ToUInt32 (IFormatProvider provider)
273                 {
274                         return System.Convert.ToUInt32(value);
275                 }
276                 
277                 [CLSCompliant(false)]
278                 public ulong ToUInt64 (IFormatProvider provider)
279                 {
280                         return System.Convert.ToUInt64(value);
281                 }
282         }
283 }