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;
37 using System.Runtime.ConstrainedExecution;
43 public static class Math
46 public sealed class Math
53 public const double E = 2.7182818284590452354;
54 public const double PI = 3.14159265358979323846;
56 public static decimal Abs (decimal value)
58 return (value < 0)? -value: value;
61 public static double Abs (double value)
63 return (value < 0)? -value: value;
66 public static float Abs (float value)
68 return (value < 0)? -value: value;
71 public static int Abs (int value)
73 if (value == Int32.MinValue)
74 throw new OverflowException (Locale.GetText ("Value is too small."));
75 return (value < 0)? -value: value;
78 public static long Abs (long value)
80 if (value == Int64.MinValue)
81 throw new OverflowException (Locale.GetText ("Value is too small."));
82 return (value < 0)? -value: value;
85 [CLSCompliant (false)]
86 public static sbyte Abs (sbyte value)
88 if (value == SByte.MinValue)
89 throw new OverflowException (Locale.GetText ("Value is too small."));
90 return (sbyte)((value < 0)? -value: value);
93 public static short Abs (short value)
95 if (value == Int16.MinValue)
96 throw new OverflowException (Locale.GetText ("Value is too small."));
97 return (short)((value < 0)? -value: value);
101 public static decimal Ceiling (decimal d)
103 decimal result = Floor(d);
111 public static double Ceiling (double a)
113 double result = Floor(a);
120 // The following methods are defined in ECMA specs but they are
121 // not implemented in MS.NET. However, they are in MS.NET 1.1
124 public static long BigMul (int a, int b)
126 return ((long)a * (long)b);
129 public static int DivRem (int a, int b, out int result)
135 public static long DivRem (long a, long b, out long result)
138 return (long)(a / b);
142 [MethodImplAttribute (MethodImplOptions.InternalCall)]
143 public extern static double Floor (double d);
145 public static double IEEERemainder (double x, double y)
150 r = x - (y * Math.Round(x/y));
153 /* Int64BitsToDouble is not endian-aware, but that is fine here */
154 return (x > 0) ? 0: (BitConverter.Int64BitsToDouble (Int64.MinValue));
157 public static double Log (double a, double newBase)
159 double result = Log(a) / Log(newBase);
160 return (result == -0)? 0: result;
164 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
166 public static byte Max (byte val1, byte val2)
168 return (val1 > val2)? val1: val2;
172 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
174 public static decimal Max (decimal val1, decimal val2)
176 return (val1 > val2)? val1: val2;
180 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
182 public static double Max (double val1, double val2)
184 if (Double.IsNaN (val1) || Double.IsNaN (val2)) {
187 return (val1 > val2)? val1: val2;
191 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
193 public static float Max (float val1, float val2)
195 if (Single.IsNaN (val1) || Single.IsNaN (val2)) {
198 return (val1 > val2)? val1: val2;
202 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
204 public static int Max (int val1, int val2)
206 return (val1 > val2)? val1: val2;
210 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
212 public static long Max (long val1, long val2)
214 return (val1 > val2)? val1: val2;
218 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
220 [CLSCompliant (false)]
221 public static sbyte Max (sbyte val1, sbyte val2)
223 return (val1 > val2)? val1: val2;
227 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
229 public static short Max (short val1, short val2)
231 return (val1 > val2)? val1: val2;
235 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
237 [CLSCompliant (false)]
238 public static uint Max (uint val1, uint val2)
240 return (val1 > val2)? val1: val2;
244 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
246 [CLSCompliant (false)]
247 public static ulong Max (ulong val1, ulong val2)
249 return (val1 > val2)? val1: val2;
253 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
255 [CLSCompliant (false)]
256 public static ushort Max (ushort val1, ushort val2)
258 return (val1 > val2)? val1: val2;
262 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
264 public static byte Min (byte val1, byte val2)
266 return (val1 < val2)? val1: val2;
270 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
272 public static decimal Min (decimal val1, decimal val2)
274 return (val1 < val2)? val1: val2;
278 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
280 public static double Min (double val1, double val2)
282 if (Double.IsNaN (val1) || Double.IsNaN (val2)) {
285 return (val1 < val2)? val1: val2;
289 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
291 public static float Min (float val1, float val2)
293 if (Single.IsNaN (val1) || Single.IsNaN (val2)) {
296 return (val1 < val2)? val1: val2;
300 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
302 public static int Min (int val1, int val2)
304 return (val1 < val2)? val1: val2;
308 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
310 public static long Min (long val1, long val2)
312 return (val1 < val2)? val1: val2;
316 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
318 [CLSCompliant (false)]
319 public static sbyte Min (sbyte val1, sbyte val2)
321 return (val1 < val2)? val1: val2;
325 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
327 public static short Min (short val1, short val2)
329 return (val1 < val2)? val1: val2;
333 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
335 [CLSCompliant (false)]
336 public static uint Min (uint val1, uint val2)
338 return (val1 < val2)? val1: val2;
342 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
344 [CLSCompliant (false)]
345 public static ulong Min (ulong val1, ulong val2)
347 return (val1 < val2)? val1: val2;
351 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
353 [CLSCompliant (false)]
354 public static ushort Min (ushort val1, ushort val2)
356 return (val1 < val2)? val1: val2;
359 public static decimal Round (decimal d)
361 // Just call Decimal.Round(d, 0); when it rounds well.
362 decimal int_part = Decimal.Floor(d);
363 decimal dec_part = d - int_part;
364 if (((dec_part == 0.5M) &&
365 ((2.0M * ((int_part / 2.0M) -
366 Decimal.Floor(int_part / 2.0M))) != 0.0M)) ||
373 public static decimal Round (decimal d, int decimals)
375 return Decimal.Round (d, decimals);
379 public static decimal Round (decimal d, MidpointRounding mode)
381 if ((mode != MidpointRounding.ToEven) && (mode != MidpointRounding.AwayFromZero))
382 throw new ArgumentException ("The value '" + mode + "' is not valid for this usage of the type MidpointRounding.", "mode");
384 if (mode == MidpointRounding.ToEven)
387 return RoundAwayFromZero (d);
390 static decimal RoundAwayFromZero (decimal d)
392 decimal int_part = Decimal.Floor(d);
393 decimal dec_part = d - int_part;
394 if (int_part >= 0 && dec_part >= 0.5M)
396 else if (int_part < 0 && dec_part > 0.5M)
401 public static decimal Round (decimal d, int decimals, MidpointRounding mode)
403 return Decimal.Round (d, decimals, mode);
407 [MethodImplAttribute (MethodImplOptions.InternalCall)]
408 public extern static double Round (double a);
410 public static double Round (double value, int digits)
412 if (digits < 0 || digits > 15)
413 throw new ArgumentOutOfRangeException (Locale.GetText ("Value is too small or too big."));
415 return Round2(value, digits, false);
418 [MethodImplAttribute (MethodImplOptions.InternalCall)]
419 private extern static double Round2 (double value, int digits, bool away_from_zero);
423 public static double Round (double value, MidpointRounding mode)
425 if ((mode != MidpointRounding.ToEven) && (mode != MidpointRounding.AwayFromZero))
426 throw new ArgumentException ("The value '" + mode + "' is not valid for this usage of the type MidpointRounding.", "mode");
428 if (mode == MidpointRounding.ToEven)
429 return Round (value);
431 return Floor (value + 0.5);
433 return Ceiling (value - 0.5);
436 public static double Round (double value, int digits, MidpointRounding mode)
438 if ((mode != MidpointRounding.ToEven) && (mode != MidpointRounding.AwayFromZero))
439 throw new ArgumentException ("The value '" + mode + "' is not valid for this usage of the type MidpointRounding.", "mode");
441 if (mode == MidpointRounding.ToEven)
442 return Round (value, digits);
444 return Round2 (value, digits, true);
447 public static double Truncate (double d)
457 public static decimal Truncate (decimal d)
459 return Decimal.Truncate (d);
462 public static decimal Floor (Decimal d)
464 return Decimal.Floor (d);
468 public static int Sign (decimal value)
470 if (value > 0) return 1;
471 return (value == 0)? 0: -1;
474 public static int Sign (double value)
476 if (Double.IsNaN (value))
477 throw new ArithmeticException ("NAN");
478 if (value > 0) return 1;
479 return (value == 0)? 0: -1;
482 public static int Sign (float value)
484 if (Single.IsNaN (value))
485 throw new ArithmeticException ("NAN");
486 if (value > 0) return 1;
487 return (value == 0)? 0: -1;
490 public static int Sign (int value)
492 if (value > 0) return 1;
493 return (value == 0)? 0: -1;
496 public static int Sign (long value)
498 if (value > 0) return 1;
499 return (value == 0)? 0: -1;
502 [CLSCompliant (false)]
503 public static int Sign (sbyte value)
505 if (value > 0) return 1;
506 return (value == 0)? 0: -1;
509 public static int Sign (short value)
511 if (value > 0) return 1;
512 return (value == 0)? 0: -1;
516 [MethodImplAttribute (MethodImplOptions.InternalCall)]
517 public extern static double Sin (double a);
519 [MethodImplAttribute (MethodImplOptions.InternalCall)]
520 public extern static double Cos (double d);
522 [MethodImplAttribute (MethodImplOptions.InternalCall)]
523 public extern static double Tan (double a);
525 [MethodImplAttribute (MethodImplOptions.InternalCall)]
526 public extern static double Sinh (double value);
528 [MethodImplAttribute (MethodImplOptions.InternalCall)]
529 public extern static double Cosh (double value);
531 [MethodImplAttribute (MethodImplOptions.InternalCall)]
532 public extern static double Tanh (double value);
534 [MethodImplAttribute (MethodImplOptions.InternalCall)]
535 public extern static double Acos (double d);
537 [MethodImplAttribute (MethodImplOptions.InternalCall)]
538 public extern static double Asin (double d);
540 [MethodImplAttribute (MethodImplOptions.InternalCall)]
541 public extern static double Atan (double d);
543 [MethodImplAttribute (MethodImplOptions.InternalCall)]
544 public extern static double Atan2 (double y, double x);
546 [MethodImplAttribute (MethodImplOptions.InternalCall)]
547 public extern static double Exp (double d);
549 [MethodImplAttribute (MethodImplOptions.InternalCall)]
550 public extern static double Log (double d);
552 [MethodImplAttribute (MethodImplOptions.InternalCall)]
553 public extern static double Log10 (double d);
555 [MethodImplAttribute (MethodImplOptions.InternalCall)]
556 public extern static double Pow (double x, double y);
558 [MethodImplAttribute (MethodImplOptions.InternalCall)]
560 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
562 public extern static double Sqrt (double d);