X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FSystem%2FSingle.cs;h=de0f7c4ea1a5a137cb1a78c805984adb4fc98b4a;hb=b783623ed18a3c1cb25f9c459cbffdd77d60393c;hp=0c6387d1d430aef756e380ce8c1a803dfaa2251a;hpb=41501f96be20f44aceeae47136abcb6a55984c9c;p=mono.git diff --git a/mcs/class/corlib/System/Single.cs b/mcs/class/corlib/System/Single.cs index 0c6387d1d43..de0f7c4ea1a 100644 --- a/mcs/class/corlib/System/Single.cs +++ b/mcs/class/corlib/System/Single.cs @@ -31,21 +31,13 @@ // using System.Globalization; - -#if NET_2_0 using System.Runtime.ConstrainedExecution; -#endif namespace System { [Serializable] -#if NET_2_0 [System.Runtime.InteropServices.ComVisible (true)] -#endif - public struct Single : IComparable, IFormattable, IConvertible -#if NET_2_0 - , IComparable , IEquatable -#endif + public struct Single : IComparable, IFormattable, IConvertible, IComparable , IEquatable { public const float Epsilon = 1.4e-45f; public const float MaxValue = 3.40282346638528859e38f; @@ -54,17 +46,21 @@ namespace System public const float PositiveInfinity = 1.0f / 0.0f; public const float NegativeInfinity = -1.0f / 0.0f; + // Maximum allowed rounding-error so that float.MaxValue can round-trip successfully; calculated + // using: (double.Parse (float.MaxValue.ToString ("r")) - (double) float.MaxValue).ToString ("r") + private const double MaxValueEpsilon = 3.6147112457961776e29d; + internal float m_value; - public int CompareTo (object v) + public int CompareTo (object value) { - if (v == null) + if (value == null) return 1; - if (!(v is System.Single)) + if (!(value is System.Single)) throw new ArgumentException (Locale.GetText ("Value is not a System.Single.")); - float fv = (float)v; + float fv = (float)value; if (IsPositiveInfinity (m_value) && IsPositiveInfinity (fv)) return 0; @@ -92,19 +88,19 @@ namespace System return -1; } - public override bool Equals (object o) + public override bool Equals (object obj) { - if (!(o is System.Single)) + if (!(obj is System.Single)) return false; - if (IsNaN ((float) o)) { + float value = (float) obj; + + if (IsNaN (value)) return IsNaN (m_value); - } - return ((float) o) == m_value; + return (value == m_value); } -#if NET_2_0 public int CompareTo (float value) { if (IsPositiveInfinity (m_value) && IsPositiveInfinity (value)) @@ -133,14 +129,13 @@ namespace System return -1; } - public bool Equals (float value) + public bool Equals (float obj) { - if (IsNaN (value)) + if (IsNaN (obj)) return IsNaN (m_value); - return value == m_value; + return obj == m_value; } -#endif public unsafe override int GetHashCode () { @@ -148,14 +143,44 @@ namespace System return *((int*)&f); } +#if NET_4_0 + public static bool operator==(float left, float right) + { + return left == right; + } + + public static bool operator!=(float left, float right) + { + return left != right; + } + + public static bool operator>(float left, float right) + { + return left > right; + } + + public static bool operator>=(float left, float right) + { + return left >= right; + } + + public static bool operator<(float left, float right) + { + return left < right; + } + + public static bool operator<=(float left, float right) + { + return left <= right; + } +#endif + public static bool IsInfinity (float f) { return (f == PositiveInfinity || f == NegativeInfinity); } -#if NET_2_0 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)] -#endif public static bool IsNaN (float f) { #pragma warning disable 1718 @@ -177,7 +202,7 @@ namespace System { double parsed_value = Double.Parse ( s, (NumberStyles.Float | NumberStyles.AllowThousands), null); - if (parsed_value > (double) float.MaxValue) + if (parsed_value - (double) float.MaxValue > MaxValueEpsilon && (!double.IsPositiveInfinity (parsed_value))) throw new OverflowException(); return (float) parsed_value; @@ -187,7 +212,7 @@ namespace System { double parsed_value = Double.Parse ( s, (NumberStyles.Float | NumberStyles.AllowThousands), provider); - if (parsed_value > (double) float.MaxValue) + if (parsed_value - (double) float.MaxValue > MaxValueEpsilon && (!double.IsPositiveInfinity (parsed_value))) throw new OverflowException(); return (float) parsed_value; @@ -196,7 +221,7 @@ namespace System public static float Parse (string s, NumberStyles style) { double parsed_value = Double.Parse (s, style, null); - if (parsed_value > (double) float.MaxValue) + if (parsed_value - (double) float.MaxValue > MaxValueEpsilon && (!double.IsPositiveInfinity (parsed_value))) throw new OverflowException(); return (float) parsed_value; @@ -205,12 +230,11 @@ namespace System public static float Parse (string s, NumberStyles style, IFormatProvider provider) { double parsed_value = Double.Parse (s, style, provider); - if (parsed_value > (double) float.MaxValue) + if (parsed_value - (double) float.MaxValue > MaxValueEpsilon && (!double.IsPositiveInfinity (parsed_value))) throw new OverflowException(); return (float) parsed_value; } -#if NET_2_0 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out float result) { double parsed_value; @@ -218,7 +242,7 @@ namespace System if (!Double.Parse (s, style, provider, true, out parsed_value, out exc)) { result = 0; return false; - } else if (parsed_value > (double) float.MaxValue) { + } else if (parsed_value - (double) float.MaxValue > MaxValueEpsilon && (!double.IsPositiveInfinity (parsed_value))) { result = 0; return false; } @@ -230,7 +254,7 @@ namespace System { return TryParse (s, NumberStyles.Any, null, out result); } -#endif + public override string ToString () { return NumberFormatter.NumberToString (m_value, null); @@ -312,9 +336,11 @@ namespace System return System.Convert.ToSingle (m_value); } - object IConvertible.ToType (Type conversionType, IFormatProvider provider) + object IConvertible.ToType (Type targetType, IFormatProvider provider) { - return System.Convert.ToType (m_value, conversionType, provider); + if (targetType == null) + throw new ArgumentNullException ("targetType"); + return System.Convert.ToType (m_value, targetType, provider, false); } ushort IConvertible.ToUInt16 (IFormatProvider provider)