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;
34 using System.Runtime.ConstrainedExecution;
39 [System.Runtime.InteropServices.ComVisible (true)]
40 public struct Single : IComparable, IFormattable, IConvertible, IComparable <float>, IEquatable <float>
42 public const float Epsilon = 1.4e-45f;
43 public const float MaxValue = 3.40282346638528859e38f;
44 public const float MinValue = -3.40282346638528859e38f;
45 public const float NaN = 0.0f / 0.0f;
46 public const float PositiveInfinity = 1.0f / 0.0f;
47 public const float NegativeInfinity = -1.0f / 0.0f;
49 // Maximum allowed rounding-error so that float.MaxValue can round-trip successfully; calculated
50 // using: (double.Parse (float.MaxValue.ToString ("r")) - (double) float.MaxValue).ToString ("r")
51 private const double MaxValueEpsilon = 3.6147112457961776e29d;
53 internal float m_value;
55 public int CompareTo (object value)
60 if (!(value is System.Single))
61 throw new ArgumentException (Locale.GetText ("Value is not a System.Single."));
63 float fv = (float)value;
65 if (IsPositiveInfinity (m_value) && IsPositiveInfinity (fv))
68 if (IsNegativeInfinity (m_value) && IsNegativeInfinity (fv))
83 if (this.m_value == fv)
85 else if (this.m_value > fv)
91 public override bool Equals (object obj)
93 if (!(obj is System.Single))
96 float value = (float) obj;
99 return IsNaN (m_value);
101 return (value == m_value);
104 public int CompareTo (float value)
106 if (IsPositiveInfinity (m_value) && IsPositiveInfinity (value))
109 if (IsNegativeInfinity (m_value) && IsNegativeInfinity (value))
124 if (this.m_value == value)
126 else if (this.m_value > value)
132 public bool Equals (float obj)
135 return IsNaN (m_value);
137 return obj == m_value;
140 public unsafe override int GetHashCode ()
146 public static bool IsInfinity (float f)
148 return (f == PositiveInfinity || f == NegativeInfinity);
151 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
152 public static bool IsNaN (float f)
154 #pragma warning disable 1718
156 #pragma warning restore
159 public static bool IsNegativeInfinity (float f)
161 return (f < 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
164 public static bool IsPositiveInfinity (float f)
166 return (f > 0.0f && (f == NegativeInfinity || f == PositiveInfinity));
169 public static float Parse (string s)
171 double parsed_value = Double.Parse (
172 s, (NumberStyles.Float | NumberStyles.AllowThousands), null);
173 if (parsed_value - (double) float.MaxValue > MaxValueEpsilon && (!double.IsPositiveInfinity (parsed_value)))
174 throw new OverflowException();
176 return (float) parsed_value;
179 public static float Parse (string s, IFormatProvider provider)
181 double parsed_value = Double.Parse (
182 s, (NumberStyles.Float | NumberStyles.AllowThousands), provider);
183 if (parsed_value - (double) float.MaxValue > MaxValueEpsilon && (!double.IsPositiveInfinity (parsed_value)))
184 throw new OverflowException();
186 return (float) parsed_value;
189 public static float Parse (string s, NumberStyles style)
191 double parsed_value = Double.Parse (s, style, null);
192 if (parsed_value - (double) float.MaxValue > MaxValueEpsilon && (!double.IsPositiveInfinity (parsed_value)))
193 throw new OverflowException();
195 return (float) parsed_value;
198 public static float Parse (string s, NumberStyles style, IFormatProvider provider)
200 double parsed_value = Double.Parse (s, style, provider);
201 if (parsed_value - (double) float.MaxValue > MaxValueEpsilon && (!double.IsPositiveInfinity (parsed_value)))
202 throw new OverflowException();
204 return (float) parsed_value;
206 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out float result)
210 if (!Double.Parse (s, style, provider, true, out parsed_value, out exc)) {
213 } else if (parsed_value - (double) float.MaxValue > MaxValueEpsilon && (!double.IsPositiveInfinity (parsed_value))) {
217 result = (float) parsed_value;
221 public static bool TryParse (string s, out float result)
223 return TryParse (s, NumberStyles.Any, null, out result);
226 public override string ToString ()
228 return NumberFormatter.NumberToString (m_value, null);
231 public string ToString (IFormatProvider provider)
233 return NumberFormatter.NumberToString (m_value, provider);
236 public string ToString (string format)
238 return ToString (format, null);
241 public string ToString (string format, IFormatProvider provider)
243 return NumberFormatter.NumberToString (format, m_value, provider);
246 // ============= IConvertible Methods ============ //
247 public TypeCode GetTypeCode ()
249 return TypeCode.Single;
252 bool IConvertible.ToBoolean (IFormatProvider provider)
254 return System.Convert.ToBoolean (m_value);
257 byte IConvertible.ToByte (IFormatProvider provider)
259 return System.Convert.ToByte (m_value);
262 char IConvertible.ToChar (IFormatProvider provider)
264 return System.Convert.ToChar (m_value);
267 DateTime IConvertible.ToDateTime (IFormatProvider provider)
269 return System.Convert.ToDateTime (m_value);
272 decimal IConvertible.ToDecimal (IFormatProvider provider)
274 return System.Convert.ToDecimal (m_value);
277 double IConvertible.ToDouble (IFormatProvider provider)
279 return System.Convert.ToDouble (m_value);
282 short IConvertible.ToInt16 (IFormatProvider provider)
284 return System.Convert.ToInt16 (m_value);
287 int IConvertible.ToInt32 (IFormatProvider provider)
289 return System.Convert.ToInt32 (m_value);
292 long IConvertible.ToInt64 (IFormatProvider provider)
294 return System.Convert.ToInt64 (m_value);
297 sbyte IConvertible.ToSByte (IFormatProvider provider)
299 return System.Convert.ToSByte (m_value);
302 float IConvertible.ToSingle (IFormatProvider provider)
304 return System.Convert.ToSingle (m_value);
307 object IConvertible.ToType (Type targetType, IFormatProvider provider)
309 if (targetType == null)
310 throw new ArgumentNullException ("targetType");
311 return System.Convert.ToType (m_value, targetType, provider, false);
314 ushort IConvertible.ToUInt16 (IFormatProvider provider)
316 return System.Convert.ToUInt16 (m_value);
319 uint IConvertible.ToUInt32 (IFormatProvider provider)
321 return System.Convert.ToUInt32 (m_value);
324 ulong IConvertible.ToUInt64 (IFormatProvider provider)
326 return System.Convert.ToUInt64 (m_value);