* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / MathObject.cs
1 //
2 // MathObject.cs:
3 //
4 // Author: Cesar Octavio Lopez Nataren
5 //
6 // (C) 2003, Cesar Octavio Lopez Nataren, <cesar@ciencias.unam.mx>
7 //
8
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.Collections;
32
33 namespace Microsoft.JScript {
34
35         public class MathObject : JSObject {
36
37                 internal static Random random_gen = new Random ();
38                 
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;
47
48                 internal static MathObject Object = new MathObject ();
49
50                 internal MathObject ()
51                 {
52                 }
53
54                 [JSFunctionAttribute (0, JSBuiltin.Math_abs)]
55                 public static double abs (double d)
56                 {
57                         return Math.Abs (d);
58                 }
59
60                 [JSFunctionAttribute (0, JSBuiltin.Math_acos)]
61                 public static double acos (double x)
62                 {
63                         return Math.Acos (x);
64                 }
65
66                 [JSFunctionAttribute (0, JSBuiltin.Math_asin)]
67                 public static double asin (double x)
68                 {
69                         return Math.Asin (x);
70                 }
71
72                 [JSFunctionAttribute (0, JSBuiltin.Math_atan)]
73                 public static double atan (double x)
74                 {
75                         return Math.Atan (x);
76                 }
77
78                 [JSFunctionAttribute (0, JSBuiltin.Math_atan2)]
79                 public static double atan2 (double dy, double dx)
80                 {
81                         return Math.Atan2 (dy, dx);
82                 }
83
84                 [JSFunctionAttribute (0, JSBuiltin.Math_ceil)]
85                 public static double ceil (double x)
86                 {
87                         return Math.Ceiling (x);
88                 }
89
90                 [JSFunctionAttribute (0, JSBuiltin.Math_cos)]
91                 public static double cos (double x)
92                 {
93                         return Math.Cos (x);
94                 }
95
96                 [JSFunctionAttribute (0, JSBuiltin.Math_exp)]
97                 public static double exp (double x)
98                 {
99                         return Math.Exp (x);
100                 }
101
102                 [JSFunctionAttribute (0, JSBuiltin.Math_floor)]
103                 public static double floor (double x)
104                 {
105                         return Math.Floor (x);
106                 }
107
108                 [JSFunctionAttribute (0, JSBuiltin.Math_log)]
109                 public static double log (double x)
110                 {
111                         return Math.Log (x);
112                 }
113
114                 [JSFunctionAttribute (JSFunctionAttributeEnum.HasVarArgs, JSBuiltin.Math_max)]
115                 public static double max (Object x, Object y, params Object [] args)
116                 {
117                         ArrayList values = new ArrayList (args);
118
119                         if (x != null)
120                                 values.Add (x);
121                         if (y != null)
122                                 values.Add (y);
123
124                         double val;
125                         double result = Double.NegativeInfinity;
126
127                         foreach (object value in values) {
128                                 val = Convert.ToNumber (value);
129                                 if (Double.IsNaN (val))
130                                         return Double.NaN;
131                                 else if (val > result)
132                                         result = val;
133                         }
134                         return result;
135                 }
136
137                 [JSFunctionAttribute (JSFunctionAttributeEnum.HasVarArgs, JSBuiltin.Math_min)]
138                 public static double min (Object x, Object y, params Object [] args)
139                 {
140                         ArrayList values = new ArrayList (args);
141
142                         if (x != null)
143                                 values.Add (x);
144                         if (y != null)
145                                 values.Add (y);
146
147                         double val;
148                         double result = Double.PositiveInfinity;
149
150                         foreach (object value in values) {
151                                 val = Convert.ToNumber (value);
152                                 if (Double.IsNaN (val))
153                                         return Double.NaN;
154                                 else if (val < result)
155                                         result = val;
156                         }
157                         return result;
158                 }
159
160                 [JSFunctionAttribute (0, JSBuiltin.Math_pow)]
161                 public static double pow (double dx, double dy)
162                 {
163                         return Math.Pow (dx, dy);
164                 }
165
166                 [JSFunctionAttribute (0, JSBuiltin.Math_random)]
167                 public static double random ()
168                 {
169                         return random_gen.Next (1);
170                 }
171
172                 [JSFunctionAttribute (0, JSBuiltin.Math_round)]
173                 public static double round (double d)
174                 {
175                         return Math.Round (d);
176                 }
177
178                 [JSFunctionAttribute (0, JSBuiltin.Math_sin)]
179                 public static double sin (double x)
180                 {
181                         return Math.Sin (x);
182                 }
183
184                 [JSFunctionAttribute (0, JSBuiltin.Math_sqrt)]
185                 public static double sqrt (double x)
186                 {
187                         return Math.Sqrt (x);
188                 }
189
190                 [JSFunctionAttribute (0, JSBuiltin.Math_tan)]
191                 public static double tan (double x)
192                 {
193                         return Math.Tan (x);
194                 }
195         }
196 }