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