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