2003-04-22 Nick Drochak <ndrochak@gol.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 using System.Globalization;
11 using System.Runtime.CompilerServices;
12
13 namespace System {
14         
15         [Serializable]
16         public struct Single : IComparable, IFormattable, IConvertible {
17                 public const float Epsilon = 1.4e-45f;
18                 public const float MaxValue =  3.40282346638528859e38f;
19                 public const float MinValue = -3.40282346638528859e38f;
20                 public const float NaN = 0.0f / 0.0f;
21                 public const float PositiveInfinity =  1.0f / 0.0f;
22                 public const float NegativeInfinity = -1.0f / 0.0f;
23                         
24                 internal float value;
25                         
26                 public int CompareTo (object v)
27                 {
28                         if (v == null)
29                                 return 1;
30
31                         if (!(v is System.Single))
32                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Single"));
33
34                         float fv = (float)v;
35
36                         if (this.value == fv) return 0;
37                         else if (this.value > fv) return 1;
38                         else return -1;
39                 }
40
41                 public override bool Equals (object o)
42                 {
43                         if (!(o is System.Single))
44                                 return false;
45
46                         return ((float) o) == value;
47                 }
48
49                 public override int GetHashCode ()
50                 {
51                         return (int) value;
52                 }
53
54                 public static bool IsInfinity (float f)
55                 {
56                         return (f == PositiveInfinity || f == NegativeInfinity);
57                 }
58
59                 public static bool IsNaN (float f)
60                 {
61                         return (f != f);
62                 }
63
64                 public static bool IsNegativeInfinity (float f)
65                 {
66                         return (f < 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
67                 }
68
69                 public static bool IsPositiveInfinity (float f)
70                 {
71                         return (f > 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
72                 }
73
74                 public static float Parse (string s)
75                 {
76                         double parsed_value = Double.Parse (
77                                 s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
78                         if (parsed_value > (double) float.MaxValue)
79                                 throw new OverflowException();
80                         
81                         return (float) parsed_value;
82                 }
83
84                 public static float Parse (string s, IFormatProvider fp)
85                 {
86                         double parsed_value = Double.Parse (
87                                 s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
88                         if (parsed_value > (double) float.MaxValue)
89                                 throw new OverflowException();
90                         
91                         return (float) parsed_value;
92                 }
93                 
94                 public static float Parse (string s, NumberStyles style)
95                 {
96                         double parsed_value = Double.Parse (s, style, null);
97                         if (parsed_value > (double) float.MaxValue)
98                                 throw new OverflowException();
99                         
100                         return (float) parsed_value;
101                 }
102
103                 public static float Parse (string s, NumberStyles style, IFormatProvider fp) 
104                 {
105                         double parsed_value = Double.Parse (s, style, fp);
106                         if (parsed_value > (double) float.MaxValue)
107                                 throw new OverflowException();
108                         
109                         return (float) parsed_value;
110                 }
111
112                 public override string ToString ()
113                 {
114                         return ToString (null, null);
115                 }
116
117                 public string ToString (IFormatProvider fp)
118                 {
119                         return ToString (null, fp);
120                 }
121
122                 public string ToString (string format)
123                 {
124                         return ToString (format, null);
125                 }
126
127                 public string ToString (string format, IFormatProvider fp)
128                 {
129                         if (fp is CultureInfo)
130                                 return SingleFormatter.NumberToString(format,
131                                 ((CultureInfo)fp).NumberFormat, value);
132                         else
133                                 return SingleFormatter.NumberToString(format,
134                                 (NumberFormatInfo)fp, value);
135                 }
136
137                 // ============= IConvertible Methods ============ //
138
139                 public TypeCode GetTypeCode ()
140                 {
141                         return TypeCode.Single;
142                 }
143
144                 bool IConvertible.ToBoolean (IFormatProvider provider)
145                 {
146                         return System.Convert.ToBoolean (value);
147                 }
148
149                 byte IConvertible.ToByte (IFormatProvider provider)
150                 {
151                         return System.Convert.ToByte (value);
152                 }
153
154                 char IConvertible.ToChar (IFormatProvider provider)
155                 {
156                         return System.Convert.ToChar (value);
157                 }
158
159                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
160                 {
161                         return System.Convert.ToDateTime (value);
162                 }
163
164                 decimal IConvertible.ToDecimal (IFormatProvider provider)
165                 {
166                         return System.Convert.ToDecimal (value);
167                 }
168
169                 double IConvertible.ToDouble (IFormatProvider provider)
170                 {
171                         return System.Convert.ToDouble (value);
172                 }
173
174                 short IConvertible.ToInt16 (IFormatProvider provider)
175                 {
176                         return System.Convert.ToInt16 (value);
177                 }
178
179                 int IConvertible.ToInt32 (IFormatProvider provider)
180                 {
181                         return System.Convert.ToInt32 (value);
182                 }
183
184                 long IConvertible.ToInt64 (IFormatProvider provider)
185                 {
186                         return System.Convert.ToInt64 (value);
187                 }
188
189                 [CLSCompliant (false)]
190                 sbyte IConvertible.ToSByte (IFormatProvider provider)
191                 {
192                         return System.Convert.ToSByte (value);
193                 }
194                 
195                 float IConvertible.ToSingle (IFormatProvider provider)
196                 {
197                         return System.Convert.ToSingle (value);
198                 }
199
200                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
201                 {
202                         return System.Convert.ToType (value, conversionType, provider);
203                 }
204
205                 [CLSCompliant (false)]
206                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
207                 {
208                         return System.Convert.ToUInt16 (value);
209                 }
210
211                 [CLSCompliant (false)]
212                 uint IConvertible.ToUInt32 (IFormatProvider provider)
213                 {
214                         return System.Convert.ToUInt32 (value);
215                 }
216
217                 [CLSCompliant (false)]
218                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
219                 {
220                         return System.Convert.ToUInt64 (value);
221                 }
222         }
223 }