2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System / Single.cs
1 //
2 // System.Single.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Globalization;
34
35 namespace System
36 {
37         [Serializable]
38         public struct Single : IComparable, IFormattable, IConvertible
39 #if NET_2_0
40                 , IComparable <float>
41 #endif
42         {
43                 public const float Epsilon = 1.4e-45f;
44                 public const float MaxValue =  3.40282346638528859e38f;
45                 public const float MinValue = -3.40282346638528859e38f;
46                 public const float NaN = 0.0f / 0.0f;
47                 public const float PositiveInfinity =  1.0f / 0.0f;
48                 public const float NegativeInfinity = -1.0f / 0.0f;
49
50                 internal float m_value;
51
52                 public int CompareTo (object v)
53                 {
54                         if (v == null)
55                                 return 1;
56
57                         if (!(v is System.Single))
58                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Single."));
59
60                         float fv = (float)v;
61
62                         if (IsPositiveInfinity (m_value) && IsPositiveInfinity (fv))
63                                 return 0;
64
65                         if (IsNegativeInfinity (m_value) && IsNegativeInfinity (fv))
66                                 return 0;
67
68                         if (IsNaN (fv))
69                                 if (IsNaN (m_value))
70                                         return 0;
71                                 else
72                                         return 1;
73
74                         if (IsNaN (m_value))
75                                 if (IsNaN (fv))
76                                         return 0;
77                                 else
78                                         return -1;
79
80                         if (this.m_value == fv)
81                                 return 0;
82                         else if (this.m_value > fv)
83                                 return 1;
84                         else
85                                 return -1;
86                 }
87
88                 public override bool Equals (object o)
89                 {
90                         if (!(o is System.Single))
91                                 return false;
92
93                         if (IsNaN ((float) o)) {
94                                 return IsNaN (m_value);
95                         }
96
97                         return ((float) o) == m_value;
98                 }
99
100 #if NET_2_0
101                 public int CompareTo (float value)
102                 {
103                         if (IsPositiveInfinity (m_value) && IsPositiveInfinity (value))
104                                 return 0;
105
106                         if (IsNegativeInfinity (m_value) && IsNegativeInfinity (value))
107                                 return 0;
108
109                         if (IsNaN (value))
110                                 if (IsNaN (m_value))
111                                         return 0;
112                                 else
113                                         return 1;
114
115                         if (IsNaN (m_value))
116                                 if (IsNaN (value))
117                                         return 0;
118                                 else
119                                         return -1;
120
121                         if (this.m_value == value)
122                                 return 0;
123                         else if (this.m_value > value)
124                                 return 1;
125                         else
126                                 return -1;
127                 }
128
129                 public bool Equals (float value)
130                 {
131                         if (IsNaN (value))
132                                 return IsNaN (m_value);
133
134                         return value == m_value;
135                 }
136 #endif
137
138                 public unsafe override int GetHashCode ()
139                 {
140                         float f = m_value;
141                         return *((int*)&f);
142                 }
143
144                 public static bool IsInfinity (float f)
145                 {
146                         return (f == PositiveInfinity || f == NegativeInfinity);
147                 }
148
149                 public static bool IsNaN (float f)
150                 {
151                         return (f != f);
152                 }
153
154                 public static bool IsNegativeInfinity (float f)
155                 {
156                         return (f < 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
157                 }
158
159                 public static bool IsPositiveInfinity (float f)
160                 {
161                         return (f > 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
162                 }
163
164                 public static float Parse (string s)
165                 {
166                         double parsed_value = Double.Parse (
167                                 s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
168                         if (parsed_value > (double) float.MaxValue)
169                                 throw new OverflowException();
170
171                         return (float) parsed_value;
172                 }
173
174                 public static float Parse (string s, IFormatProvider provider)
175                 {
176                         double parsed_value = Double.Parse (
177                                 s, (NumberStyles.Float | NumberStyles.AllowThousands), provider);
178                         if (parsed_value > (double) float.MaxValue)
179                                 throw new OverflowException();
180
181                         return (float) parsed_value;
182                 }
183                 
184                 public static float Parse (string s, NumberStyles style)
185                 {
186                         double parsed_value = Double.Parse (s, style, null);
187                         if (parsed_value > (double) float.MaxValue)
188                                 throw new OverflowException();
189
190                         return (float) parsed_value;
191                 }
192
193                 public static float Parse (string s, NumberStyles style, IFormatProvider provider) 
194                 {
195                         double parsed_value = Double.Parse (s, style, provider);
196                         if (parsed_value > (double) float.MaxValue)
197                                 throw new OverflowException();
198
199                         return (float) parsed_value;
200                 }
201
202                 public override string ToString ()
203                 {
204                         return ToString (null, null);
205                 }
206
207                 public string ToString (IFormatProvider provider)
208                 {
209                         return ToString (null, provider);
210                 }
211
212                 public string ToString (string format)
213                 {
214                         return ToString (format, null);
215                 }
216
217                 public string ToString (string format, IFormatProvider provider)
218                 {
219                         if (provider is CultureInfo)
220                                 return SingleFormatter.NumberToString (format, ((CultureInfo) provider).NumberFormat, m_value);
221                         else
222                                 return SingleFormatter.NumberToString (format, (NumberFormatInfo) provider, m_value);
223                 }
224
225                 // ============= IConvertible Methods ============ //
226                 public TypeCode GetTypeCode ()
227                 {
228                         return TypeCode.Single;
229                 }
230
231                 bool IConvertible.ToBoolean (IFormatProvider provider)
232                 {
233                         return System.Convert.ToBoolean (m_value);
234                 }
235
236                 byte IConvertible.ToByte (IFormatProvider provider)
237                 {
238                         return System.Convert.ToByte (m_value);
239                 }
240
241                 char IConvertible.ToChar (IFormatProvider provider)
242                 {
243                         return System.Convert.ToChar (m_value);
244                 }
245
246                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
247                 {
248                         return System.Convert.ToDateTime (m_value);
249                 }
250
251                 decimal IConvertible.ToDecimal (IFormatProvider provider)
252                 {
253                         return System.Convert.ToDecimal (m_value);
254                 }
255
256                 double IConvertible.ToDouble (IFormatProvider provider)
257                 {
258                         return System.Convert.ToDouble (m_value);
259                 }
260
261                 short IConvertible.ToInt16 (IFormatProvider provider)
262                 {
263                         return System.Convert.ToInt16 (m_value);
264                 }
265
266                 int IConvertible.ToInt32 (IFormatProvider provider)
267                 {
268                         return System.Convert.ToInt32 (m_value);
269                 }
270
271                 long IConvertible.ToInt64 (IFormatProvider provider)
272                 {
273                         return System.Convert.ToInt64 (m_value);
274                 }
275
276                 sbyte IConvertible.ToSByte (IFormatProvider provider)
277                 {
278                         return System.Convert.ToSByte (m_value);
279                 }
280
281                 float IConvertible.ToSingle (IFormatProvider provider)
282                 {
283                         return System.Convert.ToSingle (m_value);
284                 }
285
286                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
287                 {
288                         return System.Convert.ToType (m_value, conversionType, provider);
289                 }
290
291                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
292                 {
293                         return System.Convert.ToUInt16 (m_value);
294                 }
295
296                 uint IConvertible.ToUInt32 (IFormatProvider provider)
297                 {
298                         return System.Convert.ToUInt32 (m_value);
299                 }
300
301                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
302                 {
303                         return System.Convert.ToUInt64 (m_value);
304                 }
305         }
306 }