2002-06-08 Martin Baulig <martin@gnome.org>
[mono.git] / mcs / class / corlib / System / Math.cs
index f6f231ee7cb38b0d812546b7cb55921edb50bb02..b38a67666790ae6e725de3f63b8d39977bbdc050 100644 (file)
@@ -14,11 +14,14 @@ using System.Runtime.CompilerServices;
 
 namespace System
 {
-       [CLSCompliant(false)]
+//     [CLSCompliant(false)]
         public sealed class Math
         {
                 public const double E = 2.7182818284590452354;
                 public const double PI = 3.14159265358979323846;
+
+               private Math () {}
+
                 public static decimal Abs(decimal value)
                 {
                         return (value < 0)? -value: value;
@@ -43,6 +46,7 @@ namespace System
                                 throw new OverflowException(Locale.GetText ("Value is too small"));
                         return (value < 0)? -value: value;
                 }
+               [CLSCompliant (false)]
                 public static sbyte Abs(sbyte value)
                 {
                         if (value == SByte.MinValue)
@@ -58,11 +62,36 @@ namespace System
 
                 public static double Ceiling(double a)
                 {
-                        double b = (double)((long)a);
+                       if (Double.IsNaN(a)){
+                               return Double.NaN;
+                       }
+
+                       if (Double.IsNegativeInfinity(a)){
+                               return Double.NegativeInfinity;
+                       }
+
+                       if (Double.IsPositiveInfinity(a)){
+                               return Double.PositiveInfinity;
+                       }
+
+                       double b = (double)((long)a);
                         return (b < a)? b+1: b;
                 }
                 public static double Floor(double d) {
-                   return (double)((long)d) ;
+                       if (Double.IsNaN(d)){
+                               return Double.NaN;
+                       }
+
+                       if (Double.IsNegativeInfinity(d)){
+                               return Double.NegativeInfinity;
+                       }
+
+                       if (Double.IsPositiveInfinity(d)){
+                               return Double.PositiveInfinity;
+                       }
+
+                       double b = (double)((long)d);
+                       return (d < 0 && d != b) ? --b : b;
                 }
                 public static double IEEERemainder(double x, double y)
                 {
@@ -103,6 +132,8 @@ namespace System
                 {
                         return (val1 > val2)? val1: val2;
                 }
+
+               [CLSCompliant (false)]
                 public static sbyte Max(sbyte val1, sbyte val2)
                 {
                         return (val1 > val2)? val1: val2;
@@ -111,14 +142,20 @@ namespace System
                 {
                         return (val1 > val2)? val1: val2;
                 }
+
+               [CLSCompliant (false)]
                 public static uint Max(uint val1, uint val2)
                 {
                         return (val1 > val2)? val1: val2;
                 }
+
+               [CLSCompliant (false)]
                 public static ulong Max(ulong val1, ulong val2)
                 {
                         return (val1 > val2)? val1: val2;
                 }
+
+               [CLSCompliant (false)]
                 public static ushort Max(ushort val1, ushort val2)
                 {
                         return (val1 > val2)? val1: val2;
@@ -147,6 +184,8 @@ namespace System
                 {
                         return (val1 < val2)? val1: val2;
                 }
+
+               [CLSCompliant (false)]
                 public static sbyte Min(sbyte val1, sbyte val2)
                 {
                         return (val1 < val2)? val1: val2;
@@ -155,14 +194,20 @@ namespace System
                 {
                         return (val1 < val2)? val1: val2;
                 }
+
+               [CLSCompliant (false)]
                 public static uint Min(uint val1, uint val2)
                 {
                         return (val1 < val2)? val1: val2;
                 }
+
+               [CLSCompliant (false)]
                 public static ulong Min(ulong val1, ulong val2)
                 {
                         return (val1 < val2)? val1: val2;
                 }
+
+               [CLSCompliant (false)]
                 public static ushort Min(ushort val1, ushort val2)
                 {
                         return (val1 < val2)? val1: val2;
@@ -171,8 +216,8 @@ namespace System
                 public static decimal Round(decimal d)
                 {
                         decimal r = (decimal)((long)d);
-                        decimal a = d-r;
-                        if (a > .5M) return ++r;
+                        decimal a = Abs (d - r);
+                        if (a > .5M) return (r >= 0 ? ++r : --r);
                         else if (a <.5M) return r;
                         else
                         {
@@ -182,7 +227,7 @@ namespace System
                 }
                 public static decimal Round(decimal d, int decimals)
                 {
-                        long p = 10;
+                        long p = 1;
                         int c;
                         decimal retval = d;
                         if (decimals < 0 || decimals > 15)
@@ -199,8 +244,8 @@ namespace System
                 public static double Round(double d)
                 {
                         double r = (double)((long)d);
-                        double a = d-r;
-                        if (a > .5) return ++r;
+                        double a = Abs (d - r);
+                        if (a > .5) return (r >= 0 ? ++r : --r);
                         else if (a <.5) return r;
                         else
                         {
@@ -209,7 +254,7 @@ namespace System
                         }
                 }
                 public static double Round(double value, int digits) {
-                        long p = 10;
+                        long p = 1;
                         int c;
                         double retval = value;
                         if (digits < 0 || digits > 15)
@@ -248,6 +293,8 @@ namespace System
                         if (value > 0) return 1;
                         return (value == 0)? 0: -1;
                 }
+
+               [CLSCompliant (false)]
                 public static int Sign(sbyte value)
                 {
                         if (value > 0) return 1;
@@ -300,8 +347,27 @@ namespace System
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                 public extern static double Log10 (double x);
 
+               public static double Pow (double x, double y) {
+                       if (Double.IsNaN (x) || Double.IsNaN (y))
+                               return Double.NaN;
+                       if (Double.IsNegativeInfinity (x))
+                               if (((int)y & 1) == 1)
+                                       return Double.NegativeInfinity;
+                               else
+                                       return Double.PositiveInfinity;
+                       if (Double.IsPositiveInfinity (x))
+                               if (Double.IsNegativeInfinity (y))
+                                       return 0;
+                               else
+                                       return Double.PositiveInfinity;
+                       if (Double.IsNegativeInfinity (y) || Double.IsPositiveInfinity (y))
+                               return Double.NaN;
+                       
+                       return PowImpl (x, y);
+               }
+
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-                public extern static double Pow (double x, double y);
+                public extern static double PowImpl (double x, double y);
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                 public extern static double Sqrt (double x);