2001-08-01 Dietmar Maurer <dietmar@ximian.com>
[mono.git] / mcs / class / corlib / System / Math.cs
1 //
2 // System.Math.cs
3 //
4 // Author:
5 //   Bob Smith (bob@thestuff.net)
6 //
7 // (C) 2001 Bob Smith.  http://www.thestuff.net
8 //
9
10 using System;
11 using System.Runtime.InteropServices;
12
13 namespace System
14 {
15         public sealed class Math
16         {
17                 public const double E = 2.7182818284590452354;
18                 public const double PI = 3.14159265358979323846;
19                 public static decimal Abs(decimal value)
20                 {
21                         return (value < 0)? -value: value;
22                 }
23                 public static double Abs(double value)
24                 {
25                         return (value < 0)? -value: value;
26                 }
27                 public static float Abs(float value)
28                 {
29                         return (value < 0)? -value: value;
30                 }
31                 public static int Abs(int value)
32                 {
33                         if (value == Int32.MinValue)
34                                 throw new OverflowException("Value is too small.");
35                         return (value < 0)? -value: value;
36                 }
37                 public static long Abs(long value)
38                 {
39                         if (value == Int64.MinValue)
40                                 throw new OverflowException("Value is too small.");
41                         return (value < 0)? -value: value;
42                 }
43                 public static sbyte Abs(sbyte value)
44                 {
45                         if (value == SByte.MinValue)
46                                 throw new OverflowException("Value is too small.");
47                         return (sbyte)((value < 0)? -value: value);
48                 }
49                 public static short Abs(short value)
50                 {
51                         if (value == Int16.MinValue)
52                                 throw new OverflowException("Value is too small.");
53                         return (short)((value < 0)? -value: value);
54                 }
55                 [ DllImport("libc", EntryPoint="acos") ]
56                 private extern static double _Acos(double d);
57                 public static double Acos(double d)
58                 {
59                         if (d < -1 || d > 1) return Double.NaN;
60                         return Math._Acos(d);
61                 }
62                 [ DllImport("libc", EntryPoint="asin") ]
63                 private extern static double _Asin(double d);
64                 public static double Asin(double d)
65                 {
66                         if (d < -1 || d > 1) return Double.NaN;
67                         return Math._Asin(d);
68                 }
69                 [ DllImport("libc", EntryPoint="atan") ]
70                 public extern static double Atan(double d);
71                 [ DllImport("libc", EntryPoint="atan2") ]
72                 public extern static double Atan2(double y, double x);
73                 public static double Ceiling(double a)
74                 {
75                         double b = (double)((long)a);
76                         return (b < a)? b+1: b;
77                 }
78                 [ DllImport("libc", EntryPoint="cos") ]
79                 public extern static double Cos(double d);
80                 [ DllImport("libc", EntryPoint="cosh") ]
81                 public extern static double Cosh(double value);
82                 [ DllImport("libc", EntryPoint="exp") ]
83                 public extern static double Exp(double d);
84                 public static double Floor(double d) {
85                     return (double)((long)d) ;
86                 }
87                 public static double IEEERemainder(double x, double y)
88                 {
89                         double r;
90                         if (y == 0) return Double.NaN;
91                         r = x - (y * Math.Round(x/y));
92                         if (r != 0) return r;
93                         return (x > 0)? 0: -0;
94                 }
95                 [ DllImport("libc", EntryPoint="log") ]
96                 private extern static double _Log(double d);
97                 public static double Log(double d)
98                 {
99                         if (d == 0) return Double.NegativeInfinity;
100                         else if (d < 0) return Double.NaN;
101                         return Math._Log(d);
102                 }
103                 public static double Log(double a, double newBase)
104                 {
105                         if (a == 0) return Double.NegativeInfinity;
106                         else if (a < 0) return Double.NaN;
107                         return Math._Log(a)/Math._Log(newBase);
108                 }
109                 [ DllImport("libc", EntryPoint="log10") ]
110                 private extern static double _Log10(double d);
111                 public static double Log10(double d)
112                 {
113                         if (d == 0) return Double.NegativeInfinity;
114                         else if (d < 0) return Double.NaN;
115                         return Math._Log10(d);
116                 }
117                 public static byte Max(byte val1, byte val2)
118                 {
119                         return (val1 > val2)? val1: val2;
120                 }
121                 public static decimal Max(decimal val1, decimal val2)
122                 {
123                         return (val1 > val2)? val1: val2;
124                 }
125                 public static double Max(double val1, double val2)
126                 {
127                         return (val1 > val2)? val1: val2;
128                 }
129                 public static float Max(float val1, float val2)
130                 {
131                         return (val1 > val2)? val1: val2;
132                 }
133                 public static int Max(int val1, int val2)
134                 {
135                         return (val1 > val2)? val1: val2;
136                 }
137                 public static long Max(long val1, long val2)
138                 {
139                         return (val1 > val2)? val1: val2;
140                 }
141                 public static sbyte Max(sbyte val1, sbyte val2)
142                 {
143                         return (val1 > val2)? val1: val2;
144                 }
145                 public static short Max(short val1, short val2)
146                 {
147                         return (val1 > val2)? val1: val2;
148                 }
149                 public static uint Max(uint val1, uint val2)
150                 {
151                         return (val1 > val2)? val1: val2;
152                 }
153                 public static ulong Max(ulong val1, ulong val2)
154                 {
155                         return (val1 > val2)? val1: val2;
156                 }
157                 public static ushort Max(ushort val1, ushort val2)
158                 {
159                         return (val1 > val2)? val1: val2;
160                 }
161                 public static byte Min(byte val1, byte val2)
162                 {
163                         return (val1 < val2)? val1: val2;
164                 }
165                 public static decimal Min(decimal val1, decimal val2)
166                 {
167                         return (val1 < val2)? val1: val2;
168                 }
169                 public static double Min(double val1, double val2)
170                 {
171                         return (val1 < val2)? val1: val2;
172                 }
173                 public static float Min(float val1, float val2)
174                 {
175                         return (val1 < val2)? val1: val2;
176                 }
177                 public static int Min(int val1, int val2)
178                 {
179                         return (val1 < val2)? val1: val2;
180                 }
181                 public static long Min(long val1, long val2)
182                 {
183                         return (val1 < val2)? val1: val2;
184                 }
185                 public static sbyte Min(sbyte val1, sbyte val2)
186                 {
187                         return (val1 < val2)? val1: val2;
188                 }
189                 public static short Min(short val1, short val2)
190                 {
191                         return (val1 < val2)? val1: val2;
192                 }
193                 public static uint Min(uint val1, uint val2)
194                 {
195                         return (val1 < val2)? val1: val2;
196                 }
197                 public static ulong Min(ulong val1, ulong val2)
198                 {
199                         return (val1 < val2)? val1: val2;
200                 }
201                 public static ushort Min(ushort val1, ushort val2)
202                 {
203                         return (val1 < val2)? val1: val2;
204                 }
205                 [ DllImport("libc", EntryPoint="pow") ]
206                 public extern static double Pow(double x, double y);
207                 public static decimal Round(decimal d)
208                 {
209                         decimal r = (decimal)((long)d);
210                         decimal a = d-r;
211                         if (a > .5M) return ++r;
212                         else if (a <.5M) return r;
213                         else
214                         {
215                                 if (r%2 == 0) return r;
216                                 else return ++r;
217                         }
218                 }
219                 public static decimal Round(decimal d, int decimals)
220                 {
221                         long p = 10;
222                         int c;
223                         decimal retval = d;
224                         if (decimals < 0 || decimals > 15)
225                                 throw new ArgumentOutOfRangeException("Value is too small or too big.");
226                         else if (decimals == 0)
227                                 return Math.Round(d);
228                         for (c=0; c<decimals; c++) p*=10;
229                         retval*=p;
230                         retval=Math.Round(retval);
231                         retval/=p;
232                         return retval;
233                 }
234                 public static double Round(double d)
235                 {
236                         double r = (double)((long)d);
237                         double a = d-r;
238                         if (a > .5) return ++r;
239                         else if (a <.5) return r;
240                         else
241                         {
242                                 if (r%2 == 0) return r;
243                                 else return ++r;
244                         }
245                 }
246                 public static double Round(double value, int digits) {
247                         long p = 10;
248                         int c;
249                         double retval = value;
250                         if (digits < 0 || digits > 15)
251                                 throw new ArgumentOutOfRangeException("Value is too small or too big.");
252                         else if (digits == 0)
253                                 return Math.Round(value);
254                         for (c=0; c<digits; c++) p*=10;
255                         retval*=p;
256                         retval=Math.Round(retval);
257                         retval/=p;
258                         return retval;
259                 }
260                 public static int Sign(decimal value)
261                 {
262                         if (value > 0) return 1;
263                         return (value == 0)? 0: -1;
264                 }
265                 public static int Sign(double value)
266                 {
267                         if (value > 0) return 1;
268                         return (value == 0)? 0: -1;
269                 }
270                 public static int Sign(float value)
271                 {
272                         if (value > 0) return 1;
273                         return (value == 0)? 0: -1;
274                 }
275                 public static int Sign(int value)
276                 {
277                         if (value > 0) return 1;
278                         return (value == 0)? 0: -1;
279                 }
280                 public static int Sign(long value)
281                 {
282                         if (value > 0) return 1;
283                         return (value == 0)? 0: -1;
284                 }
285                 public static int Sign(sbyte value)
286                 {
287                         if (value > 0) return 1;
288                         return (value == 0)? 0: -1;
289                 }
290                 public static int Sign(short value)
291                 {
292                         if (value > 0) return 1;
293                         return (value == 0)? 0: -1;
294                 }
295                 [ DllImport("libc", EntryPoint="sin") ]
296                 public extern static double Sin(double a);
297                 [ DllImport("libc", EntryPoint="sinh") ]
298                 public extern static double Sinh(double value);
299                 [ DllImport("libc", EntryPoint="sqrt") ]
300                 private extern static double _Sqrt(double d);
301                 public static double Sqrt(double d) 
302                 {
303                         if (d < 0) return Double.NaN;
304                         return Math._Sqrt(d);
305                 }
306                 [ DllImport("libc", EntryPoint="tan") ]
307                 public extern static double Tan(double a);
308                 [ DllImport("libc", EntryPoint="tanh") ]
309                 public extern static double Tanh(double value);
310         }
311 }