1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
8 /** The JavaScript Math object */
9 public class Math extends JS.Obj {
10 public static Math singleton = new Math();
11 private Math() { setSeal(true); }
13 public Object get(Object name) {
14 if ("ceil".equals(name)) return new JS.Callable() { public Object call(Array args)
15 { if (args.elementAt(0) == null) return null;
16 return new Long((long)java.lang.Math.ceil(Double.parseDouble(args.elementAt(0).toString()))); } };
17 else if ("floor".equals(name)) return new JS.Callable() { public Object call(Array args)
18 { if (args.elementAt(0) == null) return null;
19 return new Long((long)java.lang.Math.floor(Double.parseDouble(args.elementAt(0).toString()))); } };
20 else if ("round".equals(name)) return new JS.Callable() { public Object call(Array args)
21 { if (args.elementAt(0) == null) return null;
22 return new Long((long)java.lang.Math.round(Double.parseDouble(args.elementAt(0).toString()))); } };
23 else if ("abs".equals(name)) return new JS.Callable() { public Object call(Array args)
24 { if (args.elementAt(0) == null) return null;
25 return new Long((long)java.lang.Math.abs(Double.parseDouble(args.elementAt(0).toString()))); } };
26 else if ("min".equals(name)) return new JS.Callable() { public Object call(Array args) {
27 if (args.length() < 2 || args.elementAt(0) == null || args.elementAt(1) == null) return args.elementAt(0);
28 return new Double(java.lang.Math.min(((Number)args.elementAt(0)).doubleValue(),
29 ((Number)args.elementAt(1)).doubleValue())); } };
30 else if ("max".equals(name)) return new JS.Callable() { public Object call(Array args) {
31 if (args.length() < 2) return args.elementAt(0);
32 return new Double(java.lang.Math.max(((Number)args.elementAt(0)).doubleValue(),
33 ((Number)args.elementAt(1)).doubleValue())); } };
34 else if ("cos".equals(name)) return new JS.Callable() { public Object call(Array args) {
35 return new Double(java.lang.Math.cos(((Number)args.elementAt(0)).doubleValue())); } };
36 else if ("sin".equals(name)) return new JS.Callable() { public Object call(Array args) {
37 return new Double(java.lang.Math.sin(((Number)args.elementAt(0)).doubleValue())); } };
38 else if ("tan".equals(name)) return new JS.Callable() { public Object call(Array args) {
39 return new Double(java.lang.Math.tan(((Number)args.elementAt(0)).doubleValue())); } };
40 else if ("acos".equals(name)) return new JS.Callable() { public Object call(Array args) {
41 return new Double(java.lang.Math.acos(((Number)args.elementAt(0)).doubleValue())); } };
42 else if ("asin".equals(name)) return new JS.Callable() { public Object call(Array args) {
43 return new Double(java.lang.Math.asin(((Number)args.elementAt(0)).doubleValue())); } };
44 else if ("atan".equals(name)) return new JS.Callable() { public Object call(Array args) {
45 return new Double(java.lang.Math.atan(((Number)args.elementAt(0)).doubleValue())); } };
46 else if ("sqrt".equals(name)) return new JS.Callable() { public Object call(Array args) {
47 return new Double(java.lang.Math.sqrt(((Number)args.elementAt(0)).doubleValue())); } };