2010-01-20 Zoltan Varga <vargaz@gmail.com>
[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 using System.Runtime.ConstrainedExecution;
35
36 namespace System
37 {
38         [Serializable]
39         [System.Runtime.InteropServices.ComVisible (true)]
40         public struct Single : IComparable, IFormattable, IConvertible, IComparable <float>, IEquatable <float>
41         {
42                 public const float Epsilon = 1.4e-45f;
43                 public const float MaxValue =  3.40282346638528859e38f;
44                 public const float MinValue = -3.40282346638528859e38f;
45                 public const float NaN = 0.0f / 0.0f;
46                 public const float PositiveInfinity =  1.0f / 0.0f;
47                 public const float NegativeInfinity = -1.0f / 0.0f;
48
49                 internal float m_value;
50
51                 public int CompareTo (object value)
52                 {
53                         if (value == null)
54                                 return 1;
55
56                         if (!(value is System.Single))
57                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Single."));
58
59                         float fv = (float)value;
60
61                         if (IsPositiveInfinity (m_value) && IsPositiveInfinity (fv))
62                                 return 0;
63
64                         if (IsNegativeInfinity (m_value) && IsNegativeInfinity (fv))
65                                 return 0;
66
67                         if (IsNaN (fv))
68                                 if (IsNaN (m_value))
69                                         return 0;
70                                 else
71                                         return 1;
72
73                         if (IsNaN (m_value))
74                                 if (IsNaN (fv))
75                                         return 0;
76                                 else
77                                         return -1;
78
79                         if (this.m_value == fv)
80                                 return 0;
81                         else if (this.m_value > fv)
82                                 return 1;
83                         else
84                                 return -1;
85                 }
86
87                 public override bool Equals (object obj)
88                 {
89                         if (!(obj is System.Single))
90                                 return false;
91
92                         float value = (float) obj;
93
94                         if (IsNaN (value))
95                                 return IsNaN (m_value);
96
97                         return (value == m_value);
98                 }
99
100                 public int CompareTo (float value)
101                 {
102                         if (IsPositiveInfinity (m_value) && IsPositiveInfinity (value))
103                                 return 0;
104
105                         if (IsNegativeInfinity (m_value) && IsNegativeInfinity (value))
106                                 return 0;
107
108                         if (IsNaN (value))
109                                 if (IsNaN (m_value))
110                                         return 0;
111                                 else
112                                         return 1;
113
114                         if (IsNaN (m_value))
115                                 if (IsNaN (value))
116                                         return 0;
117                                 else
118                                         return -1;
119
120                         if (this.m_value == value)
121                                 return 0;
122                         else if (this.m_value > value)
123                                 return 1;
124                         else
125                                 return -1;
126                 }
127
128                 public bool Equals (float obj)
129                 {
130                         if (IsNaN (obj))
131                                 return IsNaN (m_value);
132
133                         return obj == m_value;
134                 }
135
136                 public unsafe override int GetHashCode ()
137                 {
138                         float f = m_value;
139                         return *((int*)&f);
140                 }
141
142                 public static bool IsInfinity (float f)
143                 {
144                         return (f == PositiveInfinity || f == NegativeInfinity);
145                 }
146
147                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
148                 public static bool IsNaN (float f)
149                 {
150 #pragma warning disable 1718
151                         return (f != f);
152 #pragma warning restore
153                 }
154
155                 public static bool IsNegativeInfinity (float f)
156                 {
157                         return (f < 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
158                 }
159
160                 public static bool IsPositiveInfinity (float f)
161                 {
162                         return (f > 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
163                 }
164
165                 public static float Parse (string s)
166                 {
167                         double parsed_value = Double.Parse (
168                                 s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
169                         if (parsed_value > (double) float.MaxValue)
170                                 throw new OverflowException();
171
172                         return (float) parsed_value;
173                 }
174
175                 public static float Parse (string s, IFormatProvider provider)
176                 {
177                         double parsed_value = Double.Parse (
178                                 s, (NumberStyles.Float | NumberStyles.AllowThousands), provider);
179                         if (parsed_value > (double) float.MaxValue)
180                                 throw new OverflowException();
181
182                         return (float) parsed_value;
183                 }
184                 
185                 public static float Parse (string s, NumberStyles style)
186                 {
187                         double parsed_value = Double.Parse (s, style, null);
188                         if (parsed_value > (double) float.MaxValue)
189                                 throw new OverflowException();
190
191                         return (float) parsed_value;
192                 }
193
194                 public static float Parse (string s, NumberStyles style, IFormatProvider provider) 
195                 {
196                         double parsed_value = Double.Parse (s, style, provider);
197                         if (parsed_value > (double) float.MaxValue)
198                                 throw new OverflowException();
199
200                         return (float) parsed_value;
201                 }
202                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out float result)
203                 {
204                         double parsed_value;
205                         Exception exc;
206                         if (!Double.Parse (s, style, provider, true, out parsed_value, out exc)) {
207                                 result = 0;
208                                 return false;
209                         } else if (parsed_value > (double) float.MaxValue) {
210                                 result = 0;
211                                 return false;
212                         }
213                         result = (float) parsed_value;
214                         return true;
215                 }
216
217                 public static bool TryParse (string s, out float result)
218                 {
219                         return TryParse (s, NumberStyles.Any, null, out result);
220                 }
221
222                 public override string ToString ()
223                 {
224                         return NumberFormatter.NumberToString (m_value, null);
225                 }
226
227                 public string ToString (IFormatProvider provider)
228                 {
229                         return NumberFormatter.NumberToString (m_value, provider);
230                 }
231
232                 public string ToString (string format)
233                 {
234                         return ToString (format, null);
235                 }
236
237                 public string ToString (string format, IFormatProvider provider)
238                 {
239                         return NumberFormatter.NumberToString (format, m_value, provider);
240                 }
241
242                 // ============= IConvertible Methods ============ //
243                 public TypeCode GetTypeCode ()
244                 {
245                         return TypeCode.Single;
246                 }
247
248                 bool IConvertible.ToBoolean (IFormatProvider provider)
249                 {
250                         return System.Convert.ToBoolean (m_value);
251                 }
252
253                 byte IConvertible.ToByte (IFormatProvider provider)
254                 {
255                         return System.Convert.ToByte (m_value);
256                 }
257
258                 char IConvertible.ToChar (IFormatProvider provider)
259                 {
260                         return System.Convert.ToChar (m_value);
261                 }
262
263                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
264                 {
265                         return System.Convert.ToDateTime (m_value);
266                 }
267
268                 decimal IConvertible.ToDecimal (IFormatProvider provider)
269                 {
270                         return System.Convert.ToDecimal (m_value);
271                 }
272
273                 double IConvertible.ToDouble (IFormatProvider provider)
274                 {
275                         return System.Convert.ToDouble (m_value);
276                 }
277
278                 short IConvertible.ToInt16 (IFormatProvider provider)
279                 {
280                         return System.Convert.ToInt16 (m_value);
281                 }
282
283                 int IConvertible.ToInt32 (IFormatProvider provider)
284                 {
285                         return System.Convert.ToInt32 (m_value);
286                 }
287
288                 long IConvertible.ToInt64 (IFormatProvider provider)
289                 {
290                         return System.Convert.ToInt64 (m_value);
291                 }
292
293                 sbyte IConvertible.ToSByte (IFormatProvider provider)
294                 {
295                         return System.Convert.ToSByte (m_value);
296                 }
297
298                 float IConvertible.ToSingle (IFormatProvider provider)
299                 {
300                         return System.Convert.ToSingle (m_value);
301                 }
302
303                 object IConvertible.ToType (Type targetType, IFormatProvider provider)
304                 {
305                         if (targetType == null)
306                                 throw new ArgumentNullException ("targetType");
307                         return System.Convert.ToType (m_value, targetType, provider, false);
308                 }
309
310                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
311                 {
312                         return System.Convert.ToUInt16 (m_value);
313                 }
314
315                 uint IConvertible.ToUInt32 (IFormatProvider provider)
316                 {
317                         return System.Convert.ToUInt32 (m_value);
318                 }
319
320                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
321                 {
322                         return System.Convert.ToUInt64 (m_value);
323                 }
324         }
325 }