4 // Author: Cesar Octavio Lopez Nataren
6 // (C) 2003, Cesar Octavio Lopez Nataren, <cesar@ciencias.unam.mx>
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
33 namespace Microsoft.JScript {
35 public class MathObject : JSObject {
37 internal static Random random_gen = new Random ();
39 public const double E = 2.7182818284590452354;
40 public const double LN10 = 2.302585092994046;
41 public const double LN2 = 0.6931471805599453;
42 public const double LOG2E = 1.4426950408889634;
43 public const double LOG10E = 0.4342944819032518;
44 public const double PI = 3.14159265358979323846;
45 public const double SQRT1_2 = 0.7071067811865476;
46 public const double SQRT2 = 1.4142135623730951;
48 internal static MathObject Object = new MathObject ();
50 internal MathObject ()
54 [JSFunctionAttribute (0, JSBuiltin.Math_abs)]
55 public static double abs (double d)
60 [JSFunctionAttribute (0, JSBuiltin.Math_acos)]
61 public static double acos (double x)
66 [JSFunctionAttribute (0, JSBuiltin.Math_asin)]
67 public static double asin (double x)
72 [JSFunctionAttribute (0, JSBuiltin.Math_atan)]
73 public static double atan (double x)
78 [JSFunctionAttribute (0, JSBuiltin.Math_atan2)]
79 public static double atan2 (double dy, double dx)
81 return Math.Atan2 (dy, dx);
84 [JSFunctionAttribute (0, JSBuiltin.Math_ceil)]
85 public static double ceil (double x)
87 return Math.Ceiling (x);
90 [JSFunctionAttribute (0, JSBuiltin.Math_cos)]
91 public static double cos (double x)
96 [JSFunctionAttribute (0, JSBuiltin.Math_exp)]
97 public static double exp (double x)
102 [JSFunctionAttribute (0, JSBuiltin.Math_floor)]
103 public static double floor (double x)
105 return Math.Floor (x);
108 [JSFunctionAttribute (0, JSBuiltin.Math_log)]
109 public static double log (double x)
114 [JSFunctionAttribute (JSFunctionAttributeEnum.HasVarArgs, JSBuiltin.Math_max)]
115 public static double max (Object x, Object y, params Object [] args)
117 ArrayList values = new ArrayList (args);
125 double result = Double.NegativeInfinity;
127 foreach (object value in values) {
128 val = Convert.ToNumber (value);
129 if (Double.IsNaN (val))
131 else if (val > result)
137 [JSFunctionAttribute (JSFunctionAttributeEnum.HasVarArgs, JSBuiltin.Math_min)]
138 public static double min (Object x, Object y, params Object [] args)
140 ArrayList values = new ArrayList (args);
148 double result = Double.PositiveInfinity;
150 foreach (object value in values) {
151 val = Convert.ToNumber (value);
152 if (Double.IsNaN (val))
154 else if (val < result)
160 [JSFunctionAttribute (0, JSBuiltin.Math_pow)]
161 public static double pow (double dx, double dy)
163 return Math.Pow (dx, dy);
166 [JSFunctionAttribute (0, JSBuiltin.Math_random)]
167 public static double random ()
169 return random_gen.Next (1);
172 [JSFunctionAttribute (0, JSBuiltin.Math_round)]
173 public static double round (double d)
175 return Math.Round (d);
178 [JSFunctionAttribute (0, JSBuiltin.Math_sin)]
179 public static double sin (double x)
184 [JSFunctionAttribute (0, JSBuiltin.Math_sqrt)]
185 public static double sqrt (double x)
187 return Math.Sqrt (x);
190 [JSFunctionAttribute (0, JSBuiltin.Math_tan)]
191 public static double tan (double x)