5 // Bob Smith (bob@thestuff.net)
6 // Dan Lewis (dihlewis@yahoo.co.uk)
7 // Pedro Martínez Juliá (yoros@wanadoo.es)
8 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
10 // (C) 2001 Bob Smith. http://www.thestuff.net
11 // Copyright (C) 2003 Pedro Martínez Juliá <yoros@wanadoo.es>
14 using System.Runtime.CompilerServices;
18 public sealed class Math
20 public const double E = 2.7182818284590452354;
21 public const double PI = 3.14159265358979323846;
27 public static decimal Abs (decimal value)
29 return (value < 0)? -value: value;
32 public static double Abs (double value)
34 return (value < 0)? -value: value;
37 public static float Abs (float value)
39 return (value < 0)? -value: value;
42 public static int Abs (int value)
44 if (value == Int32.MinValue)
45 throw new OverflowException (Locale.GetText ("Value is too small."));
46 return (value < 0)? -value: value;
49 public static long Abs (long value)
51 if (value == Int64.MinValue)
52 throw new OverflowException (Locale.GetText ("Value is too small."));
53 return (value < 0)? -value: value;
56 [CLSCompliant (false)]
57 public static sbyte Abs (sbyte value)
59 if (value == SByte.MinValue)
60 throw new OverflowException (Locale.GetText ("Value is too small."));
61 return (sbyte)((value < 0)? -value: value);
64 public static short Abs (short value)
66 if (value == Int16.MinValue)
67 throw new OverflowException (Locale.GetText ("Value is too small."));
68 return (short)((value < 0)? -value: value);
71 public static double Ceiling (double a)
73 double result = Floor(a);
80 // The following methods are defined in ECMA specs but they are
81 // not implemented in MS.NET. However, they are in MS.NET 1.1
84 public static long BigMul (int a, int b)
86 return ((long)a * (long)b);
89 public static int DivRem (int a, int b, out int result)
95 public static long DivRem (long a, long b, out long result)
102 [MethodImplAttribute (MethodImplOptions.InternalCall)]
103 public extern static double Floor (double value);
105 public static double IEEERemainder (double x, double y)
108 if (y == 0) return Double.NaN;
109 r = x - (y * Math.Round(x/y));
112 return (x > 0)? 0: -0;
115 public static double Log (double a, double newBase)
117 double result = Log(a) / Log(newBase);
118 return (result == -0)? 0: result;
121 public static byte Max (byte val1, byte val2)
123 return (val1 > val2)? val1: val2;
126 public static decimal Max (decimal val1, decimal val2)
128 return (val1 > val2)? val1: val2;
131 public static double Max (double val1, double val2)
133 if (Double.IsNaN (val1) || Double.IsNaN (val2)) {
136 return (val1 > val2)? val1: val2;
139 public static float Max (float val1, float val2)
141 if (Single.IsNaN (val1) || Single.IsNaN (val2)) {
144 return (val1 > val2)? val1: val2;
147 public static int Max (int val1, int val2)
149 return (val1 > val2)? val1: val2;
152 public static long Max (long val1, long val2)
154 return (val1 > val2)? val1: val2;
157 [CLSCompliant (false)]
158 public static sbyte Max (sbyte val1, sbyte val2)
160 return (val1 > val2)? val1: val2;
163 public static short Max (short val1, short val2)
165 return (val1 > val2)? val1: val2;
168 [CLSCompliant (false)]
169 public static uint Max (uint val1, uint val2)
171 return (val1 > val2)? val1: val2;
174 [CLSCompliant (false)]
175 public static ulong Max (ulong val1, ulong val2)
177 return (val1 > val2)? val1: val2;
180 [CLSCompliant (false)]
181 public static ushort Max (ushort val1, ushort val2)
183 return (val1 > val2)? val1: val2;
186 public static byte Min (byte val1, byte val2)
188 return (val1 < val2)? val1: val2;
191 public static decimal Min (decimal val1, decimal val2)
193 return (val1 < val2)? val1: val2;
196 public static double Min (double val1, double val2)
198 if (Double.IsNaN (val1) || Double.IsNaN (val2)) {
201 return (val1 < val2)? val1: val2;
204 public static float Min (float val1, float val2)
206 if (Single.IsNaN (val1) || Single.IsNaN (val2)) {
209 return (val1 < val2)? val1: val2;
212 public static int Min (int val1, int val2)
214 return (val1 < val2)? val1: val2;
217 public static long Min (long val1, long val2)
219 return (val1 < val2)? val1: val2;
222 [CLSCompliant (false)]
223 public static sbyte Min (sbyte val1, sbyte val2)
225 return (val1 < val2)? val1: val2;
228 public static short Min (short val1, short val2)
230 return (val1 < val2)? val1: val2;
233 [CLSCompliant (false)]
234 public static uint Min (uint val1, uint val2)
236 return (val1 < val2)? val1: val2;
239 [CLSCompliant (false)]
240 public static ulong Min (ulong val1, ulong val2)
242 return (val1 < val2)? val1: val2;
245 [CLSCompliant (false)]
246 public static ushort Min (ushort val1, ushort val2)
248 return (val1 < val2)? val1: val2;
251 public static decimal Round (decimal d)
253 // Just call Decimal.Round(d, 0); when it rounds well.
254 decimal int_part = Decimal.Floor(d);
255 decimal dec_part = d - int_part;
256 if (((dec_part == 0.5M) &&
257 ((2.0M * ((int_part / 2.0M) -
258 Decimal.Floor(int_part / 2.0M))) != 0.0M)) ||
265 public static decimal Round (decimal d, int decimals)
267 if (decimals < 0 || decimals > 28)
268 throw new ArgumentOutOfRangeException (Locale.GetText ("Value is too small or too big."));
270 // Just call Decimal.Round(d, decimals); when it
272 decimal p = (decimal) Math.Pow(10, decimals);
273 decimal int_part = Decimal.Floor(d);
274 decimal dec_part = d - int_part;
275 dec_part *= 10000000000000000000000000000M;
276 dec_part = Decimal.Floor(dec_part);
277 dec_part /= (10000000000000000000000000000M / p);
278 dec_part = Math.Round(dec_part);
280 return int_part + dec_part;
283 [MethodImplAttribute (MethodImplOptions.InternalCall)]
284 public extern static double Round (double d);
286 public static double Round (double value, int digits)
288 if (digits < 0 || digits > 15)
289 throw new ArgumentOutOfRangeException (Locale.GetText ("Value is too small or too big."));
291 return Round2(value, digits);
294 [MethodImplAttribute (MethodImplOptions.InternalCall)]
295 private extern static double Round2 (double value, int digits);
297 public static int Sign (decimal value)
299 if (value > 0) return 1;
300 return (value == 0)? 0: -1;
303 public static int Sign (double value)
305 if (Double.IsNaN (value))
306 throw new ArithmeticException ("NAN");
307 if (value > 0) return 1;
308 return (value == 0)? 0: -1;
311 public static int Sign (float value)
313 if (Single.IsNaN (value))
314 throw new ArithmeticException ("NAN");
315 if (value > 0) return 1;
316 return (value == 0)? 0: -1;
319 public static int Sign (int value)
321 if (value > 0) return 1;
322 return (value == 0)? 0: -1;
325 public static int Sign (long value)
327 if (value > 0) return 1;
328 return (value == 0)? 0: -1;
331 [CLSCompliant (false)]
332 public static int Sign (sbyte value)
334 if (value > 0) return 1;
335 return (value == 0)? 0: -1;
338 public static int Sign (short value)
340 if (value > 0) return 1;
341 return (value == 0)? 0: -1;
345 [MethodImplAttribute (MethodImplOptions.InternalCall)]
346 public extern static double Sin (double x);
348 [MethodImplAttribute (MethodImplOptions.InternalCall)]
349 public extern static double Cos (double x);
351 [MethodImplAttribute (MethodImplOptions.InternalCall)]
352 public extern static double Tan (double x);
354 [MethodImplAttribute (MethodImplOptions.InternalCall)]
355 public extern static double Sinh (double x);
357 [MethodImplAttribute (MethodImplOptions.InternalCall)]
358 public extern static double Cosh (double x);
360 [MethodImplAttribute (MethodImplOptions.InternalCall)]
361 public extern static double Tanh (double x);
363 [MethodImplAttribute (MethodImplOptions.InternalCall)]
364 public extern static double Acos (double x);
366 [MethodImplAttribute (MethodImplOptions.InternalCall)]
367 public extern static double Asin (double x);
369 [MethodImplAttribute (MethodImplOptions.InternalCall)]
370 public extern static double Atan (double x);
372 [MethodImplAttribute (MethodImplOptions.InternalCall)]
373 public extern static double Atan2 (double y, double x);
375 [MethodImplAttribute (MethodImplOptions.InternalCall)]
376 public extern static double Exp (double x);
378 [MethodImplAttribute (MethodImplOptions.InternalCall)]
379 public extern static double Log (double x);
381 [MethodImplAttribute (MethodImplOptions.InternalCall)]
382 public extern static double Log10 (double x);
384 [MethodImplAttribute (MethodImplOptions.InternalCall)]
385 public extern static double Pow (double x, double y);
387 [MethodImplAttribute (MethodImplOptions.InternalCall)]
388 public extern static double Sqrt (double x);