001 
002 /*
003  *  JScripter Simulation 1.0 - For Java To Script
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.sim;
021 
022 import js.ArrayLike;
023 import js.MathLike;
024 import js.Vars;
025 
026 final class SimMathLike implements MathLike
027 {
028     private SimMathLike() {}
029 
030     private static SimMathLike singleton;
031 
032     public static final synchronized SimMathLike get() {
033         if (singleton == null) {
034             singleton = new SimMathLike();
035         }
036         return singleton;
037     }
038 
039     //@Override
040     public final double abs(Object x) {
041         return Math.abs(SimUtil.getNumber(x).doubleValue());
042     }
043 
044     //@Override
045     public final double acos(Object x) {
046         return Math.acos(SimUtil.getNumber(x).doubleValue());
047     }
048 
049     //@Override
050     public final double asin(Object x) {
051         return Math.asin(SimUtil.getNumber(x).doubleValue());
052     }
053 
054     //@Override
055     public final double atan(Object x) {
056         return Math.atan(SimUtil.getNumber(x).doubleValue());
057     }
058 
059     //@Override
060     public final double atan2(Object y, Object x) {
061         return Math.atan2(
062                 SimUtil.getNumber(y).doubleValue(),
063                 SimUtil.getNumber(x).doubleValue()
064         );
065     }
066 
067     //@Override
068     public final double ceil(Object x) {
069         return Math.ceil(SimUtil.getNumber(x).doubleValue());
070     }
071 
072     //@Override
073     public final double cos(Object x) {
074         return Math.cos(SimUtil.getNumber(x).doubleValue());
075     }
076 
077     //@Override
078     public final double exp(Object x) {
079         return Math.exp(SimUtil.getNumber(x).doubleValue());
080     }
081 
082     //@Override
083     public final double floor(Object x) {
084         return Math.floor(SimUtil.getNumber(x).doubleValue());
085     }
086 
087     //@Override
088     public final double log(Object x) {
089         return Math.log(SimUtil.getNumber(x).doubleValue());
090     }
091 
092     //@Override
093     public final Number max(Vars<?> args) {
094         Number n = null;
095         ArrayLike<?> a = args.var();
096         for (int i = 0, len = a.length(); i < len; i++) {
097             if (n == null) {
098                 n = SimUtil.getNumber(a.get(i));
099             } else {
100                 Number m = SimUtil.getNumber(a.get(i));
101                 if (m.doubleValue() > n.doubleValue()) {
102                     n = m;
103                 }
104             }
105         }
106         return n;
107     }
108 
109     //@Override
110     public final Number max(Object x, Object y) {
111         Number m = SimUtil.getNumber(x);
112         Number n = SimUtil.getNumber(y);
113         return m.doubleValue() < n.doubleValue() ? n : m;
114     }
115 
116     //@Override
117     public final Number min(Vars<?> args) {
118         Number n = null;
119         ArrayLike<?> a = args.var();
120         for (int i = 0, len = a.length(); i < len; i++) {
121             if (n == null) {
122                 n = SimUtil.getNumber(a.get(i));
123             } else {
124                 Number m = SimUtil.getNumber(a.get(i));
125                 if (m.doubleValue() < n.doubleValue()) {
126                     n = m;
127                 }
128             }
129         }
130         return n;
131     }
132 
133     //@Override
134     public final Number min(Object x, Object y) {
135         Number m = SimUtil.getNumber(x);
136         Number n = SimUtil.getNumber(y);
137         return m.doubleValue() > n.doubleValue() ? n : m;
138     }
139 
140     //@Override
141     public final double pow(Object x, Object y) {
142         return Math.pow(SimUtil.getNumber(x).doubleValue(), SimUtil.getNumber(y).doubleValue());
143     }
144 
145     //@Override
146     public final double random() {
147         return Math.random();
148     }
149 
150     //@Override
151     public final double round(Object x) {
152         return Math.round(SimUtil.getNumber(x).doubleValue());
153     }
154 
155     //@Override
156     public final double sin(Object x) {
157         return Math.sin(SimUtil.getNumber(x).doubleValue());
158     }
159 
160     //@Override
161     public final double sinh(Object x) {
162         return Math.sinh(SimUtil.getNumber(x).doubleValue());
163     }
164 
165     //@Override
166     public final double sqrt(Object x) {
167         return Math.sqrt(SimUtil.getNumber(x).doubleValue());
168     }
169 
170     //@Override
171     public final double tan(Object x) {
172         return Math.tan(SimUtil.getNumber(x).doubleValue());
173     }
174 
175     //@Override
176     public final double tanh(Object x) {
177         return Math.tanh(SimUtil.getNumber(x).doubleValue());
178     }
179 
180     //@Override
181     public final String toString() {
182         return "[object Object]";
183     }
184 }