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"));
38 if (this.value == fv) return 0;
39 else if (this.value > fv) return 1;
43 public override bool Equals (object o)
45 if (!(o is System.Single))
48 return ((float) o) == value;
51 public override int GetHashCode ()
56 public static bool IsInfinity (float f)
58 return (f == PositiveInfinity || f == NegativeInfinity);
61 public static bool IsNaN (float f)
66 public static bool IsNegativeInfinity (float f)
68 return (f < 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
71 public static bool IsPositiveInfinity (float f)
73 return (f > 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
76 public static float Parse (string s)
78 double parsed_value = Double.Parse (
79 s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
80 if (parsed_value > (double) float.MaxValue)
81 throw new OverflowException();
83 return (float) parsed_value;
86 public static float Parse (string s, IFormatProvider fp)
88 double parsed_value = Double.Parse (
89 s, (NumberStyles.Float | NumberStyles.AllowThousands), fp);
90 if (parsed_value > (double) float.MaxValue)
91 throw new OverflowException();
93 return (float) parsed_value;
96 public static float Parse (string s, NumberStyles style)
98 double parsed_value = Double.Parse (s, style, null);
99 if (parsed_value > (double) float.MaxValue)
100 throw new OverflowException();
102 return (float) parsed_value;
105 public static float Parse (string s, NumberStyles style, IFormatProvider fp)
107 double parsed_value = Double.Parse (s, style, fp);
108 if (parsed_value > (double) float.MaxValue)
109 throw new OverflowException();
111 return (float) parsed_value;
114 public override string ToString ()
116 return ToString (null, null);
119 public string ToString (IFormatProvider fp)
121 return ToString (null, fp);
124 public string ToString (string format)
126 return ToString (format, null);
130 public string ToString (string format, IFormatProvider fp)
132 // FIXME: Need to pass format and provider info to this call too.
133 return ToStringImpl(value);
136 [MethodImplAttribute(MethodImplOptions.InternalCall)]
137 private static extern string ToStringImpl (float value);
139 // ============= IConvertible Methods ============ //
141 public TypeCode GetTypeCode ()
143 return TypeCode.Single;
146 bool IConvertible.ToBoolean (IFormatProvider provider)
148 return System.Convert.ToBoolean (value);
151 byte IConvertible.ToByte (IFormatProvider provider)
153 return System.Convert.ToByte (value);
156 char IConvertible.ToChar (IFormatProvider provider)
158 return System.Convert.ToChar (value);
161 DateTime IConvertible.ToDateTime (IFormatProvider provider)
163 return System.Convert.ToDateTime (value);
166 decimal IConvertible.ToDecimal (IFormatProvider provider)
168 return System.Convert.ToDecimal (value);
171 double IConvertible.ToDouble (IFormatProvider provider)
173 return System.Convert.ToDouble (value);
176 short IConvertible.ToInt16 (IFormatProvider provider)
178 return System.Convert.ToInt16 (value);
181 int IConvertible.ToInt32 (IFormatProvider provider)
183 return System.Convert.ToInt32 (value);
186 long IConvertible.ToInt64 (IFormatProvider provider)
188 return System.Convert.ToInt64 (value);
191 [CLSCompliant (false)]
192 sbyte IConvertible.ToSByte (IFormatProvider provider)
194 return System.Convert.ToSByte (value);
197 float IConvertible.ToSingle (IFormatProvider provider)
199 return System.Convert.ToSingle (value);
202 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
204 return System.Convert.ToType (value, conversionType, provider);
207 [CLSCompliant (false)]
208 ushort IConvertible.ToUInt16 (IFormatProvider provider)
210 return System.Convert.ToUInt16 (value);
213 [CLSCompliant (false)]
214 uint IConvertible.ToUInt32 (IFormatProvider provider)
216 return System.Convert.ToUInt32 (value);
219 [CLSCompliant (false)]
220 ulong IConvertible.ToUInt64 (IFormatProvider provider)
222 return System.Convert.ToUInt64 (value);