* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[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         {
40                 public const float Epsilon = 1.4e-45f;
41                 public const float MaxValue =  3.40282346638528859e38f;
42                 public const float MinValue = -3.40282346638528859e38f;
43                 public const float NaN = 0.0f / 0.0f;
44                 public const float PositiveInfinity =  1.0f / 0.0f;
45                 public const float NegativeInfinity = -1.0f / 0.0f;
46
47                 internal float m_value;
48
49                 public int CompareTo (object v)
50                 {
51                         if (v == null)
52                                 return 1;
53
54                         if (!(v is System.Single))
55                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Single."));
56
57                         float fv = (float)v;
58
59                         if (IsPositiveInfinity (m_value) && IsPositiveInfinity (fv))
60                                 return 0;
61
62                         if (IsNegativeInfinity (m_value) && IsNegativeInfinity (fv))
63                                 return 0;
64
65                         if (IsNaN (fv))
66                                 if (IsNaN (m_value))
67                                         return 0;
68                                 else
69                                         return 1;
70
71                         if (IsNaN (m_value))
72                                 if (IsNaN (fv))
73                                         return 0;
74                                 else
75                                         return -1;
76
77                         if (this.m_value == fv)
78                                 return 0;
79                         else if (this.m_value > fv)
80                                 return 1;
81                         else
82                                 return -1;
83                 }
84
85                 public override bool Equals (object o)
86                 {
87                         if (!(o is System.Single))
88                                 return false;
89
90                         if (IsNaN ((float) o)) {
91                                 return IsNaN (m_value);
92                         }
93
94                         return ((float) o) == m_value;
95                 }
96
97                 public unsafe override int GetHashCode ()
98                 {
99                         float f = m_value;
100                         return *((int*)&f);
101                 }
102
103                 public static bool IsInfinity (float f)
104                 {
105                         return (f == PositiveInfinity || f == NegativeInfinity);
106                 }
107
108                 public static bool IsNaN (float f)
109                 {
110                         return (f != f);
111                 }
112
113                 public static bool IsNegativeInfinity (float f)
114                 {
115                         return (f < 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
116                 }
117
118                 public static bool IsPositiveInfinity (float f)
119                 {
120                         return (f > 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
121                 }
122
123                 public static float Parse (string s)
124                 {
125                         double parsed_value = Double.Parse (
126                                 s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
127                         if (parsed_value > (double) float.MaxValue)
128                                 throw new OverflowException();
129
130                         return (float) parsed_value;
131                 }
132
133                 public static float Parse (string s, IFormatProvider provider)
134                 {
135                         double parsed_value = Double.Parse (
136                                 s, (NumberStyles.Float | NumberStyles.AllowThousands), provider);
137                         if (parsed_value > (double) float.MaxValue)
138                                 throw new OverflowException();
139
140                         return (float) parsed_value;
141                 }
142                 
143                 public static float Parse (string s, NumberStyles style)
144                 {
145                         double parsed_value = Double.Parse (s, style, null);
146                         if (parsed_value > (double) float.MaxValue)
147                                 throw new OverflowException();
148
149                         return (float) parsed_value;
150                 }
151
152                 public static float Parse (string s, NumberStyles style, IFormatProvider provider) 
153                 {
154                         double parsed_value = Double.Parse (s, style, provider);
155                         if (parsed_value > (double) float.MaxValue)
156                                 throw new OverflowException();
157
158                         return (float) parsed_value;
159                 }
160
161                 public override string ToString ()
162                 {
163                         return ToString (null, null);
164                 }
165
166                 public string ToString (IFormatProvider provider)
167                 {
168                         return ToString (null, provider);
169                 }
170
171                 public string ToString (string format)
172                 {
173                         return ToString (format, null);
174                 }
175
176                 public string ToString (string format, IFormatProvider provider)
177                 {
178                         if (provider is CultureInfo)
179                                 return SingleFormatter.NumberToString (format, ((CultureInfo) provider).NumberFormat, m_value);
180                         else
181                                 return SingleFormatter.NumberToString (format, (NumberFormatInfo) provider, m_value);
182                 }
183
184                 // ============= IConvertible Methods ============ //
185                 public TypeCode GetTypeCode ()
186                 {
187                         return TypeCode.Single;
188                 }
189
190                 bool IConvertible.ToBoolean (IFormatProvider provider)
191                 {
192                         return System.Convert.ToBoolean (m_value);
193                 }
194
195                 byte IConvertible.ToByte (IFormatProvider provider)
196                 {
197                         return System.Convert.ToByte (m_value);
198                 }
199
200                 char IConvertible.ToChar (IFormatProvider provider)
201                 {
202                         return System.Convert.ToChar (m_value);
203                 }
204
205                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
206                 {
207                         return System.Convert.ToDateTime (m_value);
208                 }
209
210                 decimal IConvertible.ToDecimal (IFormatProvider provider)
211                 {
212                         return System.Convert.ToDecimal (m_value);
213                 }
214
215                 double IConvertible.ToDouble (IFormatProvider provider)
216                 {
217                         return System.Convert.ToDouble (m_value);
218                 }
219
220                 short IConvertible.ToInt16 (IFormatProvider provider)
221                 {
222                         return System.Convert.ToInt16 (m_value);
223                 }
224
225                 int IConvertible.ToInt32 (IFormatProvider provider)
226                 {
227                         return System.Convert.ToInt32 (m_value);
228                 }
229
230                 long IConvertible.ToInt64 (IFormatProvider provider)
231                 {
232                         return System.Convert.ToInt64 (m_value);
233                 }
234
235                 sbyte IConvertible.ToSByte (IFormatProvider provider)
236                 {
237                         return System.Convert.ToSByte (m_value);
238                 }
239
240                 float IConvertible.ToSingle (IFormatProvider provider)
241                 {
242                         return System.Convert.ToSingle (m_value);
243                 }
244
245                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
246                 {
247                         return System.Convert.ToType (m_value, conversionType, provider);
248                 }
249
250                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
251                 {
252                         return System.Convert.ToUInt16 (m_value);
253                 }
254
255                 uint IConvertible.ToUInt32 (IFormatProvider provider)
256                 {
257                         return System.Convert.ToUInt32 (m_value);
258                 }
259
260                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
261                 {
262                         return System.Convert.ToUInt64 (m_value);
263                 }
264         }
265 }