5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Globalization;
38 public struct Single : IComparable, IFormattable, IConvertible
40 public const float Epsilon = 1.4e-45f;
41 public const float MaxValue = 3.40282346638528859e38f;
42 public const float MinValue = -3.40282346638528859e38f;
43 public const float NaN = 0.0f / 0.0f;
44 public const float PositiveInfinity = 1.0f / 0.0f;
45 public const float NegativeInfinity = -1.0f / 0.0f;
47 internal float m_value;
49 public int CompareTo (object v)
54 if (!(v is System.Single))
55 throw new ArgumentException (Locale.GetText ("Value is not a System.Single."));
59 if (IsPositiveInfinity (m_value) && IsPositiveInfinity (fv))
62 if (IsNegativeInfinity (m_value) && IsNegativeInfinity (fv))
77 if (this.m_value == fv)
79 else if (this.m_value > fv)
85 public override bool Equals (object o)
87 if (!(o is System.Single))
90 if (IsNaN ((float) o)) {
91 return IsNaN (m_value);
94 return ((float) o) == m_value;
97 public override int GetHashCode ()
102 public static bool IsInfinity (float f)
104 return (f == PositiveInfinity || f == NegativeInfinity);
107 public static bool IsNaN (float f)
112 public static bool IsNegativeInfinity (float f)
114 return (f < 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
117 public static bool IsPositiveInfinity (float f)
119 return (f > 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
122 public static float Parse (string s)
124 double parsed_value = Double.Parse (
125 s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
126 if (parsed_value > (double) float.MaxValue)
127 throw new OverflowException();
129 return (float) parsed_value;
132 public static float Parse (string s, IFormatProvider provider)
134 double parsed_value = Double.Parse (
135 s, (NumberStyles.Float | NumberStyles.AllowThousands), provider);
136 if (parsed_value > (double) float.MaxValue)
137 throw new OverflowException();
139 return (float) parsed_value;
142 public static float Parse (string s, NumberStyles style)
144 double parsed_value = Double.Parse (s, style, null);
145 if (parsed_value > (double) float.MaxValue)
146 throw new OverflowException();
148 return (float) parsed_value;
151 public static float Parse (string s, NumberStyles style, IFormatProvider provider)
153 double parsed_value = Double.Parse (s, style, provider);
154 if (parsed_value > (double) float.MaxValue)
155 throw new OverflowException();
157 return (float) parsed_value;
160 public override string ToString ()
162 return ToString (null, null);
165 public string ToString (IFormatProvider provider)
167 return ToString (null, provider);
170 public string ToString (string format)
172 return ToString (format, null);
175 public string ToString (string format, IFormatProvider provider)
177 if (provider is CultureInfo)
178 return SingleFormatter.NumberToString (format, ((CultureInfo) provider).NumberFormat, m_value);
180 return SingleFormatter.NumberToString (format, (NumberFormatInfo) provider, m_value);
183 // ============= IConvertible Methods ============ //
184 public TypeCode GetTypeCode ()
186 return TypeCode.Single;
189 bool IConvertible.ToBoolean (IFormatProvider provider)
191 return System.Convert.ToBoolean (m_value);
194 byte IConvertible.ToByte (IFormatProvider provider)
196 return System.Convert.ToByte (m_value);
199 char IConvertible.ToChar (IFormatProvider provider)
201 return System.Convert.ToChar (m_value);
204 DateTime IConvertible.ToDateTime (IFormatProvider provider)
206 return System.Convert.ToDateTime (m_value);
209 decimal IConvertible.ToDecimal (IFormatProvider provider)
211 return System.Convert.ToDecimal (m_value);
214 double IConvertible.ToDouble (IFormatProvider provider)
216 return System.Convert.ToDouble (m_value);
219 short IConvertible.ToInt16 (IFormatProvider provider)
221 return System.Convert.ToInt16 (m_value);
224 int IConvertible.ToInt32 (IFormatProvider provider)
226 return System.Convert.ToInt32 (m_value);
229 long IConvertible.ToInt64 (IFormatProvider provider)
231 return System.Convert.ToInt64 (m_value);
234 sbyte IConvertible.ToSByte (IFormatProvider provider)
236 return System.Convert.ToSByte (m_value);
239 float IConvertible.ToSingle (IFormatProvider provider)
241 return System.Convert.ToSingle (m_value);
244 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
246 return System.Convert.ToType (m_value, conversionType, provider);
249 ushort IConvertible.ToUInt16 (IFormatProvider provider)
251 return System.Convert.ToUInt16 (m_value);
254 uint IConvertible.ToUInt32 (IFormatProvider provider)
256 return System.Convert.ToUInt32 (m_value);
259 ulong IConvertible.ToUInt64 (IFormatProvider provider)
261 return System.Convert.ToUInt64 (m_value);