5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 using System.Globalization;
11 using System.Runtime.CompilerServices;
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;
24 // VES needs to know about value. public is workaround
25 // so source will compile
28 public int CompareTo (object v)
33 if (!(v is System.Single))
34 throw new ArgumentException (Locale.GetText ("Value is not a System.Single"));
36 return (int) (value - ((float) v));
39 public override bool Equals (object o)
41 if (!(o is System.Single))
44 return ((float) o) == value;
47 public override int GetHashCode ()
52 public static bool IsInfinity (float f)
54 return (f == PositiveInfinity || f == NegativeInfinity);
57 public static bool IsNaN (float f)
62 public static bool IsNegativeInfinity (float f)
64 return (f < 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
67 public static bool IsPositiveInfinity (float f)
69 return (f > 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
72 public static float Parse (string s)
74 double parsed_value = Double.Parse (
75 s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
76 if (parsed_value > (double) float.MaxValue)
77 throw new OverflowException();
79 return (float) parsed_value;
82 public static float Parse (string s, IFormatProvider fp)
84 double parsed_value = Double.Parse (
85 s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
86 if (parsed_value > (double) float.MaxValue)
87 throw new OverflowException();
89 return (float) parsed_value;
92 public static float Parse (string s, NumberStyles style)
94 double parsed_value = Double.Parse (s, style, null);
95 if (parsed_value > (double) float.MaxValue)
96 throw new OverflowException();
98 return (float) parsed_value;
101 public static float Parse (string s, NumberStyles style, IFormatProvider fp)
103 double parsed_value = Double.Parse (s, style, fp);
104 if (parsed_value > (double) float.MaxValue)
105 throw new OverflowException();
107 return (float) parsed_value;
110 public override string ToString ()
112 return ToString (null, null);
115 public string ToString (IFormatProvider fp)
117 return ToString (null, fp);
120 public string ToString (string format)
122 return ToString (format, null);
126 public string ToString (string format, IFormatProvider fp)
128 // FIXME: Need to pass format and provider info to this call too.
129 return ToStringImpl(value);
132 [MethodImplAttribute(MethodImplOptions.InternalCall)]
133 private static extern string ToStringImpl (float value);
135 // ============= IConvertible Methods ============ //
137 public TypeCode GetTypeCode ()
139 return TypeCode.Single;
142 bool IConvertible.ToBoolean (IFormatProvider provider)
144 return System.Convert.ToBoolean (value);
147 byte IConvertible.ToByte (IFormatProvider provider)
149 return System.Convert.ToByte (value);
152 char IConvertible.ToChar (IFormatProvider provider)
154 return System.Convert.ToChar (value);
157 DateTime IConvertible.ToDateTime (IFormatProvider provider)
159 return System.Convert.ToDateTime (value);
162 decimal IConvertible.ToDecimal (IFormatProvider provider)
164 return System.Convert.ToDecimal (value);
167 double IConvertible.ToDouble (IFormatProvider provider)
169 return System.Convert.ToDouble (value);
172 short IConvertible.ToInt16 (IFormatProvider provider)
174 return System.Convert.ToInt16 (value);
177 int IConvertible.ToInt32 (IFormatProvider provider)
179 return System.Convert.ToInt32 (value);
182 long IConvertible.ToInt64 (IFormatProvider provider)
184 return System.Convert.ToInt64 (value);
187 [CLSCompliant (false)]
188 sbyte IConvertible.ToSByte (IFormatProvider provider)
190 return System.Convert.ToSByte (value);
193 float IConvertible.ToSingle (IFormatProvider provider)
195 return System.Convert.ToSingle (value);
198 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
200 return System.Convert.ToType (value, conversionType, provider);
203 [CLSCompliant (false)]
204 ushort IConvertible.ToUInt16 (IFormatProvider provider)
206 return System.Convert.ToUInt16 (value);
209 [CLSCompliant (false)]
210 uint IConvertible.ToUInt32 (IFormatProvider provider)
212 return System.Convert.ToUInt32 (value);
215 [CLSCompliant (false)]
216 ulong IConvertible.ToUInt64 (IFormatProvider provider)
218 return System.Convert.ToUInt64 (value);