1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL ]
5 /** The JavaScript Math object */
6 public class JSMath extends JS {
8 public static JSMath singleton = new JSMath();
10 private static final Double E = new Double(java.lang.Math.E);
11 private static final Double PI = new Double(java.lang.Math.PI);
12 private static final Double LN10 = new Double(java.lang.Math.log(10));
13 private static final Double LN2 = new Double(java.lang.Math.log(2));
14 private static final Double LOG10E = new Double(1/java.lang.Math.log(10));
15 private static final Double LOG2E = new Double(1/java.lang.Math.log(2));
16 private static final Double SQRT1_2 = new Double(1/java.lang.Math.sqrt(2));
17 private static final Double SQRT2 = new Double(java.lang.Math.sqrt(2));
19 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
23 case "random": return new Double(java.lang.Math.random());
29 case "ceil": return new Long((long)java.lang.Math.ceil(toDouble(a0)));
30 case "floor": return new Long((long)java.lang.Math.floor(toDouble(a0)));
31 case "round": return new Long((long)java.lang.Math.round(toDouble(a0)));
32 case "abs": return new Double(java.lang.Math.abs(toDouble(a0)));
33 case "sin": return new Double(java.lang.Math.sin(toDouble(a0)));
34 case "cos": return new Double(java.lang.Math.cos(toDouble(a0)));
35 case "tan": return new Double(java.lang.Math.tan(toDouble(a0)));
36 case "asin": return new Double(java.lang.Math.asin(toDouble(a0)));
37 case "acos": return new Double(java.lang.Math.acos(toDouble(a0)));
38 case "atan": return new Double(java.lang.Math.atan(toDouble(a0)));
39 case "sqrt": return new Double(java.lang.Math.sqrt(toDouble(a0)));
40 case "exp": return new Double(java.lang.Math.exp(toDouble(a0)));
41 case "log": return new Double(java.lang.Math.log(toDouble(a0)));
47 case "min": return new Double(java.lang.Math.min(toDouble(a0), toDouble(a1)));
48 case "max": return new Double(java.lang.Math.max(toDouble(a0), toDouble(a1)));
49 case "pow": return new Double(java.lang.Math.pow(toDouble(a0), toDouble(a1)));
50 case "atan2": return new Double(java.lang.Math.atan2(toDouble(a0), toDouble(a1)));
55 return super.callMethod(method, a0, a1, a2, rest, nargs);
58 public void put(Object key, Object val) { }
60 public Object get(Object key) throws JSExn {
63 case "LN10": return LN10;
64 case "LN2": return LN2;
65 case "LOG10E": return LOG10E;
66 case "LOG2E": return LOG2E;
68 case "SQRT1_2": return SQRT1_2;
69 case "SQRT2": return SQRT2;
70 case "ceil": return METHOD;
71 case "floor": return METHOD;
72 case "round": return METHOD;
73 case "min": return METHOD;
74 case "max": return METHOD;
75 case "pow": return METHOD;
76 case "atan2": return METHOD;
77 case "abs": return METHOD;
78 case "sin": return METHOD;
79 case "cos": return METHOD;
80 case "tan": return METHOD;
81 case "asin": return METHOD;
82 case "acos": return METHOD;
83 case "atan": return METHOD;
84 case "sqrt": return METHOD;
85 case "exp": return METHOD;
86 case "log": return METHOD;
87 case "random": return METHOD;
89 return super.get(key);