Merge pull request #376 from atomia/master
[mono.git] / mcs / class / corlib / System / Single.cs
index 2fee2c00f4a1531d9596f1db0aa0434d04833f1f..de0f7c4ea1a5a137cb1a78c805984adb4fc98b4a 100644 (file)
 //
 
 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 <float>, IEquatable <float>
-#endif
+       public struct Single : IComparable, IFormattable, IConvertible, IComparable <float>, IEquatable <float>
        {
                public const float Epsilon = 1.4e-45f;
                public const float MaxValue =  3.40282346638528859e38f;
@@ -54,6 +46,10 @@ 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 value)
@@ -105,7 +101,6 @@ namespace System
                        return (value == m_value);
                }
 
-#if NET_2_0
                public int CompareTo (float value)
                {
                        if (IsPositiveInfinity (m_value) && IsPositiveInfinity (value))
@@ -141,7 +136,6 @@ namespace System
 
                        return obj == m_value;
                }
-#endif
 
                public unsafe override int GetHashCode ()
                {
@@ -149,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
@@ -178,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;
@@ -188,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;
@@ -197,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;
@@ -206,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;
@@ -219,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;
                        }
@@ -231,7 +254,7 @@ namespace System
                {
                        return TryParse (s, NumberStyles.Any, null, out result);
                }
-#endif
+
                public override string ToString ()
                {
                        return NumberFormatter.NumberToString (m_value, null);
@@ -313,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)