5 // Bob Smith (bob@thestuff.net)
6 // Dan Lewis (dihlewis@yahoo.co.uk)
8 // (C) 2001 Bob Smith. http://www.thestuff.net
12 using System.Globalization;
13 using System.Runtime.CompilerServices;
17 // [CLSCompliant(false)]
18 public sealed class Math
20 public const double E = 2.7182818284590452354;
21 public const double PI = 3.14159265358979323846;
25 public static decimal Abs(decimal value)
27 return (value < 0)? -value: value;
29 public static double Abs(double value)
31 return (value < 0)? -value: value;
33 public static float Abs(float value)
35 return (value < 0)? -value: value;
37 public static int Abs(int value)
39 if (value == Int32.MinValue)
40 throw new OverflowException (Locale.GetText (Locale.GetText ("Value is too small")));
41 return (value < 0)? -value: value;
43 public static long Abs(long value)
45 if (value == Int64.MinValue)
46 throw new OverflowException(Locale.GetText ("Value is too small"));
47 return (value < 0)? -value: value;
49 [CLSCompliant (false)]
50 public static sbyte Abs(sbyte value)
52 if (value == SByte.MinValue)
53 throw new OverflowException(Locale.GetText ("Value is too small"));
54 return (sbyte)((value < 0)? -value: value);
56 public static short Abs(short value)
58 if (value == Int16.MinValue)
59 throw new OverflowException(Locale.GetText ("Value is too small"));
60 return (short)((value < 0)? -value: value);
63 public static double Ceiling(double a)
69 if (Double.IsNegativeInfinity(a)){
70 return Double.NegativeInfinity;
73 if (Double.IsPositiveInfinity(a)){
74 return Double.PositiveInfinity;
77 double b = (double)((long)a);
78 return (b < a)? b+1: b;
80 public static double Floor(double d) {
85 if (Double.IsNegativeInfinity(d)){
86 return Double.NegativeInfinity;
89 if (Double.IsPositiveInfinity(d)){
90 return Double.PositiveInfinity;
93 double b = (double)((long)d);
94 return (d < 0 && d != b) ? --b : b;
96 public static double IEEERemainder(double x, double y)
99 if (y == 0) return Double.NaN;
100 r = x - (y * Math.Round(x/y));
101 if (r != 0) return r;
102 return (x > 0)? 0: -0;
104 public static double Log(double a, double newBase)
106 if (a == 0) return Double.NegativeInfinity;
107 else if (a < 0) return Double.NaN;
108 return Log(a)/Log(newBase);
111 public static byte Max(byte val1, byte val2)
113 return (val1 > val2)? val1: val2;
115 public static decimal Max(decimal val1, decimal val2)
117 return (val1 > val2)? val1: val2;
119 public static double Max(double val1, double val2)
121 return (val1 > val2)? val1: val2;
123 public static float Max(float val1, float val2)
125 return (val1 > val2)? val1: val2;
127 public static int Max(int val1, int val2)
129 return (val1 > val2)? val1: val2;
131 public static long Max(long val1, long val2)
133 return (val1 > val2)? val1: val2;
136 [CLSCompliant (false)]
137 public static sbyte Max(sbyte val1, sbyte val2)
139 return (val1 > val2)? val1: val2;
141 public static short Max(short val1, short val2)
143 return (val1 > val2)? val1: val2;
146 [CLSCompliant (false)]
147 public static uint Max(uint val1, uint val2)
149 return (val1 > val2)? val1: val2;
152 [CLSCompliant (false)]
153 public static ulong Max(ulong val1, ulong val2)
155 return (val1 > val2)? val1: val2;
158 [CLSCompliant (false)]
159 public static ushort Max(ushort val1, ushort val2)
161 return (val1 > val2)? val1: val2;
163 public static byte Min(byte val1, byte val2)
165 return (val1 < val2)? val1: val2;
167 public static decimal Min(decimal val1, decimal val2)
169 return (val1 < val2)? val1: val2;
171 public static double Min(double val1, double val2)
173 return (val1 < val2)? val1: val2;
175 public static float Min(float val1, float val2)
177 return (val1 < val2)? val1: val2;
179 public static int Min(int val1, int val2)
181 return (val1 < val2)? val1: val2;
183 public static long Min(long val1, long val2)
185 return (val1 < val2)? val1: val2;
188 [CLSCompliant (false)]
189 public static sbyte Min(sbyte val1, sbyte val2)
191 return (val1 < val2)? val1: val2;
193 public static short Min(short val1, short val2)
195 return (val1 < val2)? val1: val2;
198 [CLSCompliant (false)]
199 public static uint Min(uint val1, uint val2)
201 return (val1 < val2)? val1: val2;
204 [CLSCompliant (false)]
205 public static ulong Min(ulong val1, ulong val2)
207 return (val1 < val2)? val1: val2;
210 [CLSCompliant (false)]
211 public static ushort Min(ushort val1, ushort val2)
213 return (val1 < val2)? val1: val2;
216 public static decimal Round(decimal d)
218 decimal r = (decimal)((long)d);
219 decimal a = Abs (d - r);
220 if (a > .5M) return (r >= 0 ? ++r : --r);
221 else if (a <.5M) return r;
224 if (r%2 == 0) return r;
228 public static decimal Round(decimal d, int decimals)
233 if (decimals < 0 || decimals > 15)
234 throw new ArgumentOutOfRangeException(Locale.GetText (
235 "Value is too small or too big."));
236 else if (decimals == 0)
237 return Math.Round(d);
238 for (c=0; c<decimals; c++) p*=10;
240 retval=Math.Round(retval);
244 public static double Round(double d)
246 double r = (double)((long)d);
247 double a = Abs (d - r);
248 if (a > .5) return (r >= 0 ? ++r : --r);
249 else if (a <.5) return r;
252 if (r%2 == 0) return r;
256 public static double Round(double value, int digits) {
259 double retval = value;
260 if (digits < 0 || digits > 15)
261 throw new ArgumentOutOfRangeException(Locale.GetText (
262 "Value is too small or too big."));
263 else if (digits == 0)
264 return Math.Round(value);
265 for (c=0; c<digits; c++) p*=10;
267 retval=Math.Round(retval);
271 public static int Sign(decimal value)
273 if (value > 0) return 1;
274 return (value == 0)? 0: -1;
276 public static int Sign(double value)
278 if (value > 0) return 1;
279 return (value == 0)? 0: -1;
281 public static int Sign(float value)
283 if (value > 0) return 1;
284 return (value == 0)? 0: -1;
286 public static int Sign(int value)
288 if (value > 0) return 1;
289 return (value == 0)? 0: -1;
291 public static int Sign(long value)
293 if (value > 0) return 1;
294 return (value == 0)? 0: -1;
297 [CLSCompliant (false)]
298 public static int Sign(sbyte value)
300 if (value > 0) return 1;
301 return (value == 0)? 0: -1;
303 public static int Sign(short value)
305 if (value > 0) return 1;
306 return (value == 0)? 0: -1;
311 [MethodImplAttribute (MethodImplOptions.InternalCall)]
312 public extern static double Sin (double x);
314 [MethodImplAttribute (MethodImplOptions.InternalCall)]
315 public extern static double Cos (double x);
317 [MethodImplAttribute (MethodImplOptions.InternalCall)]
318 public extern static double Tan (double x);
320 [MethodImplAttribute (MethodImplOptions.InternalCall)]
321 public extern static double Sinh (double x);
323 [MethodImplAttribute (MethodImplOptions.InternalCall)]
324 public extern static double Cosh (double x);
326 [MethodImplAttribute (MethodImplOptions.InternalCall)]
327 public extern static double Tanh (double x);
329 [MethodImplAttribute (MethodImplOptions.InternalCall)]
330 public extern static double Acos (double x);
332 [MethodImplAttribute (MethodImplOptions.InternalCall)]
333 public extern static double Asin (double x);
335 [MethodImplAttribute (MethodImplOptions.InternalCall)]
336 public extern static double Atan (double x);
338 [MethodImplAttribute (MethodImplOptions.InternalCall)]
339 public extern static double Atan2 (double y, double x);
341 [MethodImplAttribute (MethodImplOptions.InternalCall)]
342 public extern static double Exp (double x);
344 [MethodImplAttribute (MethodImplOptions.InternalCall)]
345 public extern static double Log (double x);
347 [MethodImplAttribute (MethodImplOptions.InternalCall)]
348 public extern static double Log10 (double x);
350 public static double Pow (double x, double y) {
351 if (Double.IsNaN (x) || Double.IsNaN (y))
353 if (Double.IsNegativeInfinity (x))
354 if (((int)y & 1) == 1)
355 return Double.NegativeInfinity;
357 return Double.PositiveInfinity;
358 if (Double.IsPositiveInfinity (x))
359 if (Double.IsNegativeInfinity (y))
362 return Double.PositiveInfinity;
363 if (Double.IsNegativeInfinity (y) || Double.IsPositiveInfinity (y))
366 return PowImpl (x, y);
369 [MethodImplAttribute (MethodImplOptions.InternalCall)]
370 public extern static double PowImpl (double x, double y);
372 [MethodImplAttribute (MethodImplOptions.InternalCall)]
373 public extern static double Sqrt (double x);