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