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)
143 double result = Log(a) / Log(newBase);
144 return (result == -0)? 0: result;
147 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
148 public static byte Max (byte val1, byte val2)
150 return (val1 > val2)? val1: val2;
153 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
154 public static decimal Max (decimal val1, decimal val2)
156 return (val1 > val2)? val1: val2;
159 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
160 public static double Max (double val1, double val2)
162 if (Double.IsNaN (val1) || Double.IsNaN (val2)) {
165 return (val1 > val2)? val1: val2;
168 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
169 public static float Max (float val1, float val2)
171 if (Single.IsNaN (val1) || Single.IsNaN (val2)) {
174 return (val1 > val2)? val1: val2;
177 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
178 public static int Max (int val1, int val2)
180 return (val1 > val2)? val1: val2;
183 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
184 public static long Max (long val1, long val2)
186 return (val1 > val2)? val1: val2;
189 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
190 [CLSCompliant (false)]
191 public static sbyte Max (sbyte val1, sbyte val2)
193 return (val1 > val2)? val1: val2;
196 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
197 public static short Max (short val1, short val2)
199 return (val1 > val2)? val1: val2;
202 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
203 [CLSCompliant (false)]
204 public static uint Max (uint val1, uint val2)
206 return (val1 > val2)? val1: val2;
209 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
210 [CLSCompliant (false)]
211 public static ulong Max (ulong val1, ulong val2)
213 return (val1 > val2)? val1: val2;
216 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
217 [CLSCompliant (false)]
218 public static ushort Max (ushort val1, ushort val2)
220 return (val1 > val2)? val1: val2;
223 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
224 public static byte Min (byte val1, byte val2)
226 return (val1 < val2)? val1: val2;
229 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
230 public static decimal Min (decimal val1, decimal val2)
232 return (val1 < val2)? val1: val2;
235 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
236 public static double Min (double val1, double val2)
238 if (Double.IsNaN (val1) || Double.IsNaN (val2)) {
241 return (val1 < val2)? val1: val2;
244 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
245 public static float Min (float val1, float val2)
247 if (Single.IsNaN (val1) || Single.IsNaN (val2)) {
250 return (val1 < val2)? val1: val2;
253 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
254 public static int Min (int val1, int val2)
256 return (val1 < val2)? val1: val2;
259 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
260 public static long Min (long val1, long val2)
262 return (val1 < val2)? val1: val2;
265 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
266 [CLSCompliant (false)]
267 public static sbyte Min (sbyte val1, sbyte val2)
269 return (val1 < val2)? val1: val2;
272 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
273 public static short Min (short val1, short val2)
275 return (val1 < val2)? val1: val2;
278 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
279 [CLSCompliant (false)]
280 public static uint Min (uint val1, uint val2)
282 return (val1 < val2)? val1: val2;
285 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
286 [CLSCompliant (false)]
287 public static ulong Min (ulong val1, ulong val2)
289 return (val1 < val2)? val1: val2;
292 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
293 [CLSCompliant (false)]
294 public static ushort Min (ushort val1, ushort val2)
296 return (val1 < val2)? val1: val2;
299 public static decimal Round (decimal d)
301 // Just call Decimal.Round(d, 0); when it rounds well.
302 decimal int_part = Decimal.Floor(d);
303 decimal dec_part = d - int_part;
304 if (((dec_part == 0.5M) &&
305 ((2.0M * ((int_part / 2.0M) -
306 Decimal.Floor(int_part / 2.0M))) != 0.0M)) ||
313 public static decimal Round (decimal d, int decimals)
315 return Decimal.Round (d, decimals);
318 public static decimal Round (decimal d, MidpointRounding mode)
320 if ((mode != MidpointRounding.ToEven) && (mode != MidpointRounding.AwayFromZero))
321 throw new ArgumentException ("The value '" + mode + "' is not valid for this usage of the type MidpointRounding.", "mode");
323 if (mode == MidpointRounding.ToEven)
326 return RoundAwayFromZero (d);
329 static decimal RoundAwayFromZero (decimal d)
331 decimal int_part = Decimal.Floor(d);
332 decimal dec_part = d - int_part;
333 if (int_part >= 0 && dec_part >= 0.5M)
335 else if (int_part < 0 && dec_part > 0.5M)
340 public static decimal Round (decimal d, int decimals, MidpointRounding mode)
342 return Decimal.Round (d, decimals, mode);
345 [MethodImplAttribute (MethodImplOptions.InternalCall)]
346 public extern static double Round (double a);
348 public static double Round (double value, int digits)
350 if (digits < 0 || digits > 15)
351 throw new ArgumentOutOfRangeException (Locale.GetText ("Value is too small or too big."));
353 return Round2(value, digits, false);
356 [MethodImplAttribute (MethodImplOptions.InternalCall)]
357 private extern static double Round2 (double value, int digits, bool away_from_zero);
360 public static double Round (double value, MidpointRounding mode)
362 if ((mode != MidpointRounding.ToEven) && (mode != MidpointRounding.AwayFromZero))
363 throw new ArgumentException ("The value '" + mode + "' is not valid for this usage of the type MidpointRounding.", "mode");
365 if (mode == MidpointRounding.ToEven)
366 return Round (value);
368 return Floor (value + 0.5);
370 return Ceiling (value - 0.5);
373 public static double Round (double value, int digits, MidpointRounding mode)
375 if ((mode != MidpointRounding.ToEven) && (mode != MidpointRounding.AwayFromZero))
376 throw new ArgumentException ("The value '" + mode + "' is not valid for this usage of the type MidpointRounding.", "mode");
378 if (mode == MidpointRounding.ToEven)
379 return Round (value, digits);
381 return Round2 (value, digits, true);
384 public static double Truncate (double d)
394 public static decimal Truncate (decimal d)
396 return Decimal.Truncate (d);
399 public static decimal Floor (Decimal d)
401 return Decimal.Floor (d);
404 public static int Sign (decimal value)
406 if (value > 0) return 1;
407 return (value == 0)? 0: -1;
410 public static int Sign (double value)
412 if (Double.IsNaN (value))
413 throw new ArithmeticException ("NAN");
414 if (value > 0) return 1;
415 return (value == 0)? 0: -1;
418 public static int Sign (float value)
420 if (Single.IsNaN (value))
421 throw new ArithmeticException ("NAN");
422 if (value > 0) return 1;
423 return (value == 0)? 0: -1;
426 public static int Sign (int value)
428 if (value > 0) return 1;
429 return (value == 0)? 0: -1;
432 public static int Sign (long value)
434 if (value > 0) return 1;
435 return (value == 0)? 0: -1;
438 [CLSCompliant (false)]
439 public static int Sign (sbyte value)
441 if (value > 0) return 1;
442 return (value == 0)? 0: -1;
445 public static int Sign (short value)
447 if (value > 0) return 1;
448 return (value == 0)? 0: -1;
452 [MethodImplAttribute (MethodImplOptions.InternalCall)]
453 public extern static double Sin (double a);
455 [MethodImplAttribute (MethodImplOptions.InternalCall)]
456 public extern static double Cos (double d);
458 [MethodImplAttribute (MethodImplOptions.InternalCall)]
459 public extern static double Tan (double a);
461 [MethodImplAttribute (MethodImplOptions.InternalCall)]
462 public extern static double Sinh (double value);
464 [MethodImplAttribute (MethodImplOptions.InternalCall)]
465 public extern static double Cosh (double value);
467 [MethodImplAttribute (MethodImplOptions.InternalCall)]
468 public extern static double Tanh (double value);
470 [MethodImplAttribute (MethodImplOptions.InternalCall)]
471 public extern static double Acos (double d);
473 [MethodImplAttribute (MethodImplOptions.InternalCall)]
474 public extern static double Asin (double d);
476 [MethodImplAttribute (MethodImplOptions.InternalCall)]
477 public extern static double Atan (double d);
479 [MethodImplAttribute (MethodImplOptions.InternalCall)]
480 public extern static double Atan2 (double y, double x);
482 [MethodImplAttribute (MethodImplOptions.InternalCall)]
483 public extern static double Exp (double d);
485 [MethodImplAttribute (MethodImplOptions.InternalCall)]
486 public extern static double Log (double d);
488 [MethodImplAttribute (MethodImplOptions.InternalCall)]
489 public extern static double Log10 (double d);
491 [MethodImplAttribute (MethodImplOptions.InternalCall)]
492 public extern static double Pow (double x, double y);
494 [MethodImplAttribute (MethodImplOptions.InternalCall)]
495 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
496 public extern static double Sqrt (double d);