2002-06-08 Martin Baulig <martin@gnome.org>
[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                 // VES needs to know about value.  public is workaround
25                 // so source will compile
26                 public float value;
27                         
28                 public int CompareTo (object v)
29                 {
30                         if (v == null)
31                                 return 1;
32
33                         if (!(v is System.Single))
34                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Single"));
35
36                         return (int) (value - ((float) v));
37                 }
38
39                 public override bool Equals (object o)
40                 {
41                         if (!(o is System.Single))
42                                 return false;
43
44                         return ((float) o) == value;
45                 }
46
47                 public override int GetHashCode ()
48                 {
49                         return (int) value;
50                 }
51
52                 public static bool IsInfinity (float f)
53                 {
54                         return (f == PositiveInfinity || f == NegativeInfinity);
55                 }
56
57                 public static bool IsNaN (float f)
58                 {
59                         return (f != f);
60                 }
61
62                 public static bool IsNegativeInfinity (float f)
63                 {
64                         return (f < 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
65                 }
66
67                 public static bool IsPositiveInfinity (float f)
68                 {
69                         return (f > 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
70                 }
71
72                 public static float Parse (string s)
73                 {
74                         double parsed_value = Double.Parse (
75                                 s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
76                         if (parsed_value > (double) float.MaxValue)
77                                 throw new OverflowException();
78                         
79                         return (float) parsed_value;
80                 }
81
82                 public static float Parse (string s, IFormatProvider fp)
83                 {
84                         double parsed_value = Double.Parse (
85                                 s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
86                         if (parsed_value > (double) float.MaxValue)
87                                 throw new OverflowException();
88                         
89                         return (float) parsed_value;
90                 }
91                 
92                 public static float Parse (string s, NumberStyles style)
93                 {
94                         double parsed_value = Double.Parse (s, style, null);
95                         if (parsed_value > (double) float.MaxValue)
96                                 throw new OverflowException();
97                         
98                         return (float) parsed_value;
99                 }
100
101                 public static float Parse (string s, NumberStyles style, IFormatProvider fp) 
102                 {
103                         double parsed_value = Double.Parse (s, style, fp);
104                         if (parsed_value > (double) float.MaxValue)
105                                 throw new OverflowException();
106                         
107                         return (float) parsed_value;
108                 }
109
110                 public override string ToString ()
111                 {
112                         return ToString (null, null);
113                 }
114
115                 public string ToString (IFormatProvider fp)
116                 {
117                         return ToString (null, fp);
118                 }
119
120                 public string ToString (string format)
121                 {
122                         return ToString (format, null);
123                 }
124
125                 [MonoTODO]
126                 public string ToString (string format, IFormatProvider fp)
127                 {
128                         // FIXME: Need to pass format and provider info to this call too.
129                         return ToStringImpl(value);
130                 }
131
132                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
133                 private static extern string ToStringImpl (float value);
134
135                 // ============= IConvertible Methods ============ //
136
137                 public TypeCode GetTypeCode ()
138                 {
139                         return TypeCode.Single;
140                 }
141
142                 bool IConvertible.ToBoolean (IFormatProvider provider)
143                 {
144                         return System.Convert.ToBoolean (value);
145                 }
146
147                 byte IConvertible.ToByte (IFormatProvider provider)
148                 {
149                         return System.Convert.ToByte (value);
150                 }
151
152                 char IConvertible.ToChar (IFormatProvider provider)
153                 {
154                         return System.Convert.ToChar (value);
155                 }
156
157                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
158                 {
159                         return System.Convert.ToDateTime (value);
160                 }
161
162                 decimal IConvertible.ToDecimal (IFormatProvider provider)
163                 {
164                         return System.Convert.ToDecimal (value);
165                 }
166
167                 double IConvertible.ToDouble (IFormatProvider provider)
168                 {
169                         return System.Convert.ToDouble (value);
170                 }
171
172                 short IConvertible.ToInt16 (IFormatProvider provider)
173                 {
174                         return System.Convert.ToInt16 (value);
175                 }
176
177                 int IConvertible.ToInt32 (IFormatProvider provider)
178                 {
179                         return System.Convert.ToInt32 (value);
180                 }
181
182                 long IConvertible.ToInt64 (IFormatProvider provider)
183                 {
184                         return System.Convert.ToInt64 (value);
185                 }
186
187                 [CLSCompliant (false)]
188                 sbyte IConvertible.ToSByte (IFormatProvider provider)
189                 {
190                         return System.Convert.ToSByte (value);
191                 }
192                 
193                 float IConvertible.ToSingle (IFormatProvider provider)
194                 {
195                         return System.Convert.ToSingle (value);
196                 }
197
198                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
199                 {
200                         return System.Convert.ToType (value, conversionType, provider);
201                 }
202
203                 [CLSCompliant (false)]
204                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
205                 {
206                         return System.Convert.ToUInt16 (value);
207                 }
208
209                 [CLSCompliant (false)]
210                 uint IConvertible.ToUInt32 (IFormatProvider provider)
211                 {
212                         return System.Convert.ToUInt32 (value);
213                 }
214
215                 [CLSCompliant (false)]
216                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
217                 {
218                         return System.Convert.ToUInt64 (value);
219                 }
220         }
221 }