1    // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] 
2    
3    package org.xwt.js; 
4    import org.xwt.util.*; 
5    import java.io.*;
6    import java.util.*;
7    
8    /** The JavaScript Math object */
9    public class Math extends JS.Obj {
10       public static Math singleton = new Math();
11   
12       private static final JS.Callable ceil = new JS.Callable() { public Object call(JS.Array args) {
13           return new Long((long)java.lang.Math.ceil(JS.toNumber(args.elementAt(0)).doubleValue())); } };
14   
15       private static final JS.Callable floor = new JS.Callable() { public Object call(JS.Array args) {
16           return new Long((long)java.lang.Math.floor(JS.toNumber(args.elementAt(0)).doubleValue())); } };
17   
18       private static final JS.Callable round = new JS.Callable() { public Object call(JS.Array args) {
19           return new Long((long)java.lang.Math.round(JS.toNumber(args.elementAt(0)).doubleValue())); } };
20   
21       private static final JS.Callable min = new JS.Callable() { public Object call(JS.Array args) {
22           return new Double(java.lang.Math.min(JS.toNumber(args.elementAt(0)).doubleValue(),
23               JS.toNumber(args.elementAt(1)).doubleValue())); } };
24   
25       private static final JS.Callable max = new JS.Callable() { public Object call(JS.Array args) {
26           return new Double(java.lang.Math.max(JS.toNumber(args.elementAt(0)).doubleValue(),
27               JS.toNumber(args.elementAt(1)).doubleValue())); } };
28   
29       private static final JS.Callable pow = new JS.Callable() { public Object call(JS.Array args) {
30           return new Double(java.lang.Math.pow(JS.toNumber(args.elementAt(0)).doubleValue(),
31               JS.toNumber(args.elementAt(1)).doubleValue())); } };
32   
33       private static final JS.Callable atan2 = new JS.Callable() { public Object call(JS.Array args) {
34           return new Double(java.lang.Math.atan2(JS.toNumber(args.elementAt(0)).doubleValue(),
35               JS.toNumber(args.elementAt(1)).doubleValue())); } };
36   
37       private static final JS.Callable abs = new JS.Callable() { public Object call(JS.Array args) {
38           return new Double(java.lang.Math.abs(JS.toNumber(args.elementAt(0)).doubleValue())); } };
39   
40       private static final JS.Callable sin = new JS.Callable() { public Object call(JS.Array args) {
41           return new Double(java.lang.Math.sin(JS.toNumber(args.elementAt(0)).doubleValue())); } };
42   
43       private static final JS.Callable cos = new JS.Callable() { public Object call(JS.Array args) {
44           return new Double(java.lang.Math.cos(JS.toNumber(args.elementAt(0)).doubleValue())); } };
45   
46       private static final JS.Callable tan = new JS.Callable() { public Object call(JS.Array args) {
47           return new Double(java.lang.Math.tan(JS.toNumber(args.elementAt(0)).doubleValue())); } };
48   
49       private static final JS.Callable asin = new JS.Callable() { public Object call(JS.Array args) {
50           return new Double(java.lang.Math.asin(JS.toNumber(args.elementAt(0)).doubleValue())); } };
51   
52       private static final JS.Callable acos = new JS.Callable() { public Object call(JS.Array args) {
53           return new Double(java.lang.Math.acos(JS.toNumber(args.elementAt(0)).doubleValue())); } };
54   
55       private static final JS.Callable atan = new JS.Callable() { public Object call(JS.Array args) {
56           return new Double(java.lang.Math.atan(JS.toNumber(args.elementAt(0)).doubleValue())); } };
57   
58       private static final JS.Callable sqrt = new JS.Callable() { public Object call(JS.Array args) {
59           return new Double(java.lang.Math.sqrt(JS.toNumber(args.elementAt(0)).doubleValue())); } };
60   
61       private static final JS.Callable exp = new JS.Callable() { public Object call(JS.Array args) {
62           return new Double(java.lang.Math.exp(JS.toNumber(args.elementAt(0)).doubleValue())); } };
63   
64       private static final JS.Callable log = new JS.Callable() { public Object call(JS.Array args) {
65           return new Double(java.lang.Math.log(JS.toNumber(args.elementAt(0)).doubleValue())); } };
66   
67       private static final JS.Callable random = new JS.Callable() { public Object call(JS.Array args) {
68           return new Double(java.lang.Math.random()); } };
69   
70       private static final Double E       = new Double(java.lang.Math.E);
71       private static final Double PI      = new Double(java.lang.Math.PI);
72       private static final Double LN10    = new Double(java.lang.Math.log(10));
73       private static final Double LN2     = new Double(java.lang.Math.log(2));
74       private static final Double LOG10E  = new Double(1/java.lang.Math.log(10));
75       private static final Double LOG2E   = new Double(1/java.lang.Math.log(2));
76       private static final Double SQRT1_2 = new Double(1/java.lang.Math.sqrt(2));
77       private static final Double SQRT2   = new Double(java.lang.Math.sqrt(2));
78   
79       protected Math() {
80           put("abs", abs);
81           put("acos", acos);
82           put("asin", asin);
83           put("atan", atan);
84           put("atan2", atan2);
85           put("ceil", ceil);
86           put("cos", cos);
87           put("exp", exp);
88           put("floor", floor);
89           put("log", log);
90           put("max", max);
91           put("min", min);
92           put("pow", pow);
93           put("random", random);
94           put("round",round);
95           put("sin", sin);
96           put("sqrt", sqrt);
97           put("tan", tan);
98   
99           put("E", E);
100          put("LN10", LN10);
101          put("LN2", LN2);
102          put("LOG10E", LOG10E);
103          put("LOG2E", LOG2E);
104          put("PI", PI);
105          put("SQRT1_2", SQRT1_2);
106          put("SQRT2", SQRT2);
107  
108          setSeal(true);
109      }
110  }
111