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>
12 // Copyright (C) 2004 Novell (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Runtime.CompilerServices;
35 using System.Runtime.ConstrainedExecution;
39 public static class Math
41 public const double E = 2.7182818284590452354;
42 public const double PI = 3.14159265358979323846;
44 public static decimal Abs (decimal value)
46 return (value < 0)? -value: value;
49 public static double Abs (double value)
51 return (value < 0)? -value: value;
54 public static float Abs (float value)
56 return (value < 0)? -value: value;
59 public static int Abs (int value)
61 if (value == Int32.MinValue)
62 throw new OverflowException (Locale.GetText ("Value is too small."));
63 return (value < 0)? -value: value;
66 public static long Abs (long value)
68 if (value == Int64.MinValue)
69 throw new OverflowException (Locale.GetText ("Value is too small."));
70 return (value < 0)? -value: value;
73 [CLSCompliant (false)]
74 public static sbyte Abs (sbyte value)
76 if (value == SByte.MinValue)
77 throw new OverflowException (Locale.GetText ("Value is too small."));
78 return (sbyte)((value < 0)? -value: value);
81 public static short Abs (short value)
83 if (value == Int16.MinValue)
84 throw new OverflowException (Locale.GetText ("Value is too small."));
85 return (short)((value < 0)? -value: value);
88 public static decimal Ceiling (decimal d)
90 decimal result = Floor(d);
97 public static double Ceiling (double a)
99 double result = Floor(a);
106 // The following methods are defined in ECMA specs but they are
107 // not implemented in MS.NET. However, they are in MS.NET 1.1
109 public static long BigMul (int a, int b)
111 return ((long)a * (long)b);
114 public static int DivRem (int a, int b, out int result)
120 public static long DivRem (long a, long b, out long result)
123 return (long)(a / b);
126 [MethodImplAttribute (MethodImplOptions.InternalCall)]
127 public extern static double Floor (double d);
129 public static double IEEERemainder (double x, double y)
134 r = x - (y * Math.Round(x/y));
137 /* Int64BitsToDouble is not endian-aware, but that is fine here */
138 return (x > 0) ? 0: (BitConverter.Int64BitsToDouble (Int64.MinValue));
141 public static double Log (double a, double newBase)
145 double result = Log(a) / Log(newBase);
146 return (result == -0)? 0: result;
149 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
150 public static byte Max (byte val1, byte val2)
152 return (val1 > val2)? val1: val2;
155 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
156 public static decimal Max (decimal val1, decimal val2)
158 return (val1 > val2)? val1: val2;
161 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
162 public static double Max (double val1, double val2)
164 if (Double.IsNaN (val1) || Double.IsNaN (val2)) {
167 return (val1 > val2)? val1: val2;
170 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
171 public static float Max (float val1, float val2)
173 if (Single.IsNaN (val1) || Single.IsNaN (val2)) {
176 return (val1 > val2)? val1: val2;
179 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
180 public static int Max (int val1, int val2)
182 return (val1 > val2)? val1: val2;
185 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
186 public static long Max (long val1, long val2)
188 return (val1 > val2)? val1: val2;
191 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
192 [CLSCompliant (false)]
193 public static sbyte Max (sbyte val1, sbyte val2)
195 return (val1 > val2)? val1: val2;
198 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
199 public static short Max (short val1, short val2)
201 return (val1 > val2)? val1: val2;
204 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
205 [CLSCompliant (false)]
206 public static uint Max (uint val1, uint val2)
208 return (val1 > val2)? val1: val2;
211 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
212 [CLSCompliant (false)]
213 public static ulong Max (ulong val1, ulong val2)
215 return (val1 > val2)? val1: val2;
218 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
219 [CLSCompliant (false)]
220 public static ushort Max (ushort val1, ushort val2)
222 return (val1 > val2)? val1: val2;
225 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
226 public static byte Min (byte val1, byte val2)
228 return (val1 < val2)? val1: val2;
231 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
232 public static decimal Min (decimal val1, decimal val2)
234 return (val1 < val2)? val1: val2;
237 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
238 public static double Min (double val1, double val2)
240 if (Double.IsNaN (val1) || Double.IsNaN (val2)) {
243 return (val1 < val2)? val1: val2;
246 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
247 public static float Min (float val1, float val2)
249 if (Single.IsNaN (val1) || Single.IsNaN (val2)) {
252 return (val1 < val2)? val1: val2;
255 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
256 public static int Min (int val1, int val2)
258 return (val1 < val2)? val1: val2;
261 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
262 public static long Min (long val1, long val2)
264 return (val1 < val2)? val1: val2;
267 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
268 [CLSCompliant (false)]
269 public static sbyte Min (sbyte val1, sbyte val2)
271 return (val1 < val2)? val1: val2;
274 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
275 public static short Min (short val1, short val2)
277 return (val1 < val2)? val1: val2;
280 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
281 [CLSCompliant (false)]
282 public static uint Min (uint val1, uint val2)
284 return (val1 < val2)? val1: val2;
287 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
288 [CLSCompliant (false)]
289 public static ulong Min (ulong val1, ulong val2)
291 return (val1 < val2)? val1: val2;
294 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
295 [CLSCompliant (false)]
296 public static ushort Min (ushort val1, ushort val2)
298 return (val1 < val2)? val1: val2;
301 public static decimal Round (decimal d)
303 // Just call Decimal.Round(d, 0); when it rounds well.
304 decimal int_part = Decimal.Floor(d);
305 decimal dec_part = d - int_part;
306 if (((dec_part == 0.5M) &&
307 ((2.0M * ((int_part / 2.0M) -
308 Decimal.Floor(int_part / 2.0M))) != 0.0M)) ||
315 public static decimal Round (decimal d, int decimals)
317 return Decimal.Round (d, decimals);
320 public static decimal Round (decimal d, MidpointRounding mode)
322 if ((mode != MidpointRounding.ToEven) && (mode != MidpointRounding.AwayFromZero))
323 throw new ArgumentException ("The value '" + mode + "' is not valid for this usage of the type MidpointRounding.", "mode");
325 if (mode == MidpointRounding.ToEven)
328 return RoundAwayFromZero (d);
331 static decimal RoundAwayFromZero (decimal d)
333 decimal int_part = Decimal.Floor(d);
334 decimal dec_part = d - int_part;
335 if (int_part >= 0 && dec_part >= 0.5M)
337 else if (int_part < 0 && dec_part > 0.5M)
342 public static decimal Round (decimal d, int decimals, MidpointRounding mode)
344 return Decimal.Round (d, decimals, mode);
347 [MethodImplAttribute (MethodImplOptions.InternalCall)]
348 public extern static double Round (double a);
350 public static double Round (double value, int digits)
352 if (digits < 0 || digits > 15)
353 throw new ArgumentOutOfRangeException (Locale.GetText ("Value is too small or too big."));
355 return Round (value);
357 return Round2(value, digits, false);
360 [MethodImplAttribute (MethodImplOptions.InternalCall)]
361 private extern static double Round2 (double value, int digits, bool away_from_zero);
364 public static double Round (double value, MidpointRounding mode)
366 if ((mode != MidpointRounding.ToEven) && (mode != MidpointRounding.AwayFromZero))
367 throw new ArgumentException ("The value '" + mode + "' is not valid for this usage of the type MidpointRounding.", "mode");
369 if (mode == MidpointRounding.ToEven)
370 return Round (value);
372 return Floor (value + 0.5);
374 return Ceiling (value - 0.5);
377 public static double Round (double value, int digits, MidpointRounding mode)
379 if ((mode != MidpointRounding.ToEven) && (mode != MidpointRounding.AwayFromZero))
380 throw new ArgumentException ("The value '" + mode + "' is not valid for this usage of the type MidpointRounding.", "mode");
382 return Round (value, mode);
384 if (mode == MidpointRounding.ToEven)
385 return Round (value, digits);
387 return Round2 (value, digits, true);
390 public static double Truncate (double d)
400 public static decimal Truncate (decimal d)
402 return Decimal.Truncate (d);
405 public static decimal Floor (Decimal d)
407 return Decimal.Floor (d);
410 public static int Sign (decimal value)
412 if (value > 0) return 1;
413 return (value == 0)? 0: -1;
416 public static int Sign (double value)
418 if (Double.IsNaN (value))
419 throw new ArithmeticException ("NAN");
420 if (value > 0) return 1;
421 return (value == 0)? 0: -1;
424 public static int Sign (float value)
426 if (Single.IsNaN (value))
427 throw new ArithmeticException ("NAN");
428 if (value > 0) return 1;
429 return (value == 0)? 0: -1;
432 public static int Sign (int value)
434 if (value > 0) return 1;
435 return (value == 0)? 0: -1;
438 public static int Sign (long value)
440 if (value > 0) return 1;
441 return (value == 0)? 0: -1;
444 [CLSCompliant (false)]
445 public static int Sign (sbyte value)
447 if (value > 0) return 1;
448 return (value == 0)? 0: -1;
451 public static int Sign (short value)
453 if (value > 0) return 1;
454 return (value == 0)? 0: -1;
458 [MethodImplAttribute (MethodImplOptions.InternalCall)]
459 public extern static double Sin (double a);
461 [MethodImplAttribute (MethodImplOptions.InternalCall)]
462 public extern static double Cos (double d);
464 [MethodImplAttribute (MethodImplOptions.InternalCall)]
465 public extern static double Tan (double a);
467 [MethodImplAttribute (MethodImplOptions.InternalCall)]
468 public extern static double Sinh (double value);
470 [MethodImplAttribute (MethodImplOptions.InternalCall)]
471 public extern static double Cosh (double value);
473 [MethodImplAttribute (MethodImplOptions.InternalCall)]
474 public extern static double Tanh (double value);
476 [MethodImplAttribute (MethodImplOptions.InternalCall)]
477 public extern static double Acos (double d);
479 [MethodImplAttribute (MethodImplOptions.InternalCall)]
480 public extern static double Asin (double d);
482 [MethodImplAttribute (MethodImplOptions.InternalCall)]
483 public extern static double Atan (double d);
485 [MethodImplAttribute (MethodImplOptions.InternalCall)]
486 public extern static double Atan2 (double y, double x);
488 [MethodImplAttribute (MethodImplOptions.InternalCall)]
489 public extern static double Exp (double d);
491 [MethodImplAttribute (MethodImplOptions.InternalCall)]
492 public extern static double Log (double d);
494 [MethodImplAttribute (MethodImplOptions.InternalCall)]
495 public extern static double Log10 (double d);
497 [MethodImplAttribute (MethodImplOptions.InternalCall)]
498 public extern static double Pow (double x, double y);
500 [MethodImplAttribute (MethodImplOptions.InternalCall)]
501 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
502 public extern static double Sqrt (double d);