4 * A single-file Javascript-alike engine
6 * - Math and Trigonometry functions
8 * Authored By O.Z.L.B. <ozlbinfo@gmail.com>
10 * Copyright (C) 2011 O.Z.L.B.
12 * Permission is hereby granted, free of charge, to any person obtaining a copy of
13 * this software and associated documentation files (the "Software"), to deal in
14 * the Software without restriction, including without limitation the rights to
15 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
16 * of the Software, and to permit persons to whom the Software is furnished to do
17 * so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in all
20 * copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 using namespace TinyJS
;
40 #define k_PI 3.1415926535897932384626433832795
42 #define F_ABS(a) ((a)>=0 ? (a) : (-(a)))
43 #define F_MIN(a,b) ((a)>(b) ? (b) : (a))
44 #define F_MAX(a,b) ((a)>(b) ? (a) : (b))
45 #define F_SGN(a) ((a)>0 ? 1 : ((a)<0 ? -1 : 0 ))
46 #define F_RNG(a,min,max) ((a)<(min) ? min : ((a)>(max) ? max : a ))
47 #define F_ROUND(a) ((a)>0 ? (int) ((a)+0.5) : (int) ((a)-0.5) )
49 //Variable shortcut macro
50 #define scIsInt(a) ( c->getParameter(a)->isInt() )
51 #define scIsDouble(a) ( c->getParameter(a)->isDouble() )
52 #define scGetInt(a) ( c->getParameter(a)->getInt() )
53 #define scGetDouble(a) ( c->getParameter(a)->getDouble() )
54 #define scReturnInt(a) ( c->getReturnVar()->setInt(a) )
55 #define scReturnDouble(a) ( c->getReturnVar()->setDouble(a) )
60 double asinh( const double &value
)
65 returned
= log(value
+ sqrt(value
* value
+ 1));
67 returned
= -log(-value
+ sqrt(value
* value
+ 1));
72 double acosh( const double &value
)
77 returned
= log(value
+ sqrt(value
* value
- 1));
79 returned
= -log(-value
+ sqrt(value
* value
- 1));
86 //Math.abs(x) - returns absolute of given value
87 void scMathAbs(Variable
*c
, void *userdata
) {
89 scReturnInt( F_ABS( scGetInt("a") ) );
90 } else if ( scIsDouble("a") ) {
91 scReturnDouble( F_ABS( scGetDouble("a") ) );
95 //Math.round(a) - returns nearest round of given value
96 void scMathRound(Variable
*c
, void *userdata
) {
98 scReturnInt( F_ROUND( scGetInt("a") ) );
99 } else if ( scIsDouble("a") ) {
100 scReturnDouble( F_ROUND( scGetDouble("a") ) );
104 //Math.min(a,b) - returns minimum of two given values
105 void scMathMin(Variable
*c
, void *userdata
) {
106 if ( (scIsInt("a")) && (scIsInt("b")) ) {
107 scReturnInt( F_MIN( scGetInt("a"), scGetInt("b") ) );
109 scReturnDouble( F_MIN( scGetDouble("a"), scGetDouble("b") ) );
113 //Math.max(a,b) - returns maximum of two given values
114 void scMathMax(Variable
*c
, void *userdata
) {
115 if ( (scIsInt("a")) && (scIsInt("b")) ) {
116 scReturnInt( F_MAX( scGetInt("a"), scGetInt("b") ) );
118 scReturnDouble( F_MAX( scGetDouble("a"), scGetDouble("b") ) );
122 //Math.range(x,a,b) - returns value limited between two given values
123 void scMathRange(Variable
*c
, void *userdata
) {
124 if ( (scIsInt("x")) ) {
125 scReturnInt( F_RNG( scGetInt("x"), scGetInt("a"), scGetInt("b") ) );
127 scReturnDouble( F_RNG( scGetDouble("x"), scGetDouble("a"), scGetDouble("b") ) );
131 //Math.sign(a) - returns sign of given value (-1==negative,0=zero,1=positive)
132 void scMathSign(Variable
*c
, void *userdata
) {
133 if ( scIsInt("a") ) {
134 scReturnInt( F_SGN( scGetInt("a") ) );
135 } else if ( scIsDouble("a") ) {
136 scReturnDouble( F_SGN( scGetDouble("a") ) );
140 //Math.PI() - returns PI value
141 void scMathPI(Variable
*c
, void *userdata
) {
142 scReturnDouble(k_PI
);
145 //Math.toDegrees(a) - returns degree value of a given angle in radians
146 void scMathToDegrees(Variable
*c
, void *userdata
) {
147 scReturnDouble( (180.0/k_PI
)*( scGetDouble("a") ) );
150 //Math.toRadians(a) - returns radians value of a given angle in degrees
151 void scMathToRadians(Variable
*c
, void *userdata
) {
152 scReturnDouble( (k_PI
/180.0)*( scGetDouble("a") ) );
155 //Math.sin(a) - returns trig. sine of given angle in radians
156 void scMathSin(Variable
*c
, void *userdata
) {
157 scReturnDouble( sin( scGetDouble("a") ) );
160 //Math.asin(a) - returns trig. arcsine of given angle in radians
161 void scMathASin(Variable
*c
, void *userdata
) {
162 scReturnDouble( asin( scGetDouble("a") ) );
165 //Math.cos(a) - returns trig. cosine of given angle in radians
166 void scMathCos(Variable
*c
, void *userdata
) {
167 scReturnDouble( cos( scGetDouble("a") ) );
170 //Math.acos(a) - returns trig. arccosine of given angle in radians
171 void scMathACos(Variable
*c
, void *userdata
) {
172 scReturnDouble( acos( scGetDouble("a") ) );
175 //Math.tan(a) - returns trig. tangent of given angle in radians
176 void scMathTan(Variable
*c
, void *userdata
) {
177 scReturnDouble( tan( scGetDouble("a") ) );
180 //Math.atan(a) - returns trig. arctangent of given angle in radians
181 void scMathATan(Variable
*c
, void *userdata
) {
182 scReturnDouble( atan( scGetDouble("a") ) );
185 //Math.sinh(a) - returns trig. hyperbolic sine of given angle in radians
186 void scMathSinh(Variable
*c
, void *userdata
) {
187 scReturnDouble( sinh( scGetDouble("a") ) );
190 //Math.asinh(a) - returns trig. hyperbolic arcsine of given angle in radians
191 void scMathASinh(Variable
*c
, void *userdata
) {
192 scReturnDouble( asinh( scGetDouble("a") ) );
195 //Math.cosh(a) - returns trig. hyperbolic cosine of given angle in radians
196 void scMathCosh(Variable
*c
, void *userdata
) {
197 scReturnDouble( cosh( scGetDouble("a") ) );
200 //Math.acosh(a) - returns trig. hyperbolic arccosine of given angle in radians
201 void scMathACosh(Variable
*c
, void *userdata
) {
202 scReturnDouble( acosh( scGetDouble("a") ) );
205 //Math.tanh(a) - returns trig. hyperbolic tangent of given angle in radians
206 void scMathTanh(Variable
*c
, void *userdata
) {
207 scReturnDouble( tanh( scGetDouble("a") ) );
210 //Math.atan(a) - returns trig. hyperbolic arctangent of given angle in radians
211 void scMathATanh(Variable
*c
, void *userdata
) {
212 scReturnDouble( atan( scGetDouble("a") ) );
215 //Math.E() - returns E Neplero value
216 void scMathE(Variable
*c
, void *userdata
) {
220 //Math.log(a) - returns natural logaritm (base E) of given value
221 void scMathLog(Variable
*c
, void *userdata
) {
222 scReturnDouble( log( scGetDouble("a") ) );
225 //Math.log10(a) - returns logaritm(base 10) of given value
226 void scMathLog10(Variable
*c
, void *userdata
) {
227 scReturnDouble( log10( scGetDouble("a") ) );
230 //Math.exp(a) - returns e raised to the power of a given number
231 void scMathExp(Variable
*c
, void *userdata
) {
232 scReturnDouble( exp( scGetDouble("a") ) );
235 //Math.pow(a,b) - returns the result of a number raised to a power (a)^(b)
236 void scMathPow(Variable
*c
, void *userdata
) {
237 scReturnDouble( pow( scGetDouble("a"), scGetDouble("b") ) );
240 //Math.sqr(a) - returns square of given value
241 void scMathSqr(Variable
*c
, void *userdata
) {
242 scReturnDouble( ( scGetDouble("a") * scGetDouble("a") ) );
245 //Math.sqrt(a) - returns square root of given value
246 void scMathSqrt(Variable
*c
, void *userdata
) {
247 scReturnDouble( sqrtf( scGetDouble("a") ) );
250 // ----------------------------------------------- Register Functions
251 void registerMathFunctions(Interpreter
*tinyJS
) {
253 // --- Math and Trigonometry functions ---
254 tinyJS
->addNative("function Math.abs(a)", scMathAbs
, 0);
255 tinyJS
->addNative("function Math.round(a)", scMathRound
, 0);
256 tinyJS
->addNative("function Math.min(a,b)", scMathMin
, 0);
257 tinyJS
->addNative("function Math.max(a,b)", scMathMax
, 0);
258 tinyJS
->addNative("function Math.range(x,a,b)", scMathRange
, 0);
259 tinyJS
->addNative("function Math.sign(a)", scMathSign
, 0);
261 tinyJS
->addNative("function Math.PI()", scMathPI
, 0);
262 tinyJS
->addNative("function Math.toDegrees(a)", scMathToDegrees
, 0);
263 tinyJS
->addNative("function Math.toRadians(a)", scMathToRadians
, 0);
264 tinyJS
->addNative("function Math.sin(a)", scMathSin
, 0);
265 tinyJS
->addNative("function Math.asin(a)", scMathASin
, 0);
266 tinyJS
->addNative("function Math.cos(a)", scMathCos
, 0);
267 tinyJS
->addNative("function Math.acos(a)", scMathACos
, 0);
268 tinyJS
->addNative("function Math.tan(a)", scMathTan
, 0);
269 tinyJS
->addNative("function Math.atan(a)", scMathATan
, 0);
270 tinyJS
->addNative("function Math.sinh(a)", scMathSinh
, 0);
271 tinyJS
->addNative("function Math.asinh(a)", scMathASinh
, 0);
272 tinyJS
->addNative("function Math.cosh(a)", scMathCosh
, 0);
273 tinyJS
->addNative("function Math.acosh(a)", scMathACosh
, 0);
274 tinyJS
->addNative("function Math.tanh(a)", scMathTanh
, 0);
275 tinyJS
->addNative("function Math.atanh(a)", scMathATanh
, 0);
277 tinyJS
->addNative("function Math.E()", scMathE
, 0);
278 tinyJS
->addNative("function Math.log(a)", scMathLog
, 0);
279 tinyJS
->addNative("function Math.log10(a)", scMathLog10
, 0);
280 tinyJS
->addNative("function Math.exp(a)", scMathExp
, 0);
281 tinyJS
->addNative("function Math.pow(a,b)", scMathPow
, 0);
283 tinyJS
->addNative("function Math.sqr(a)", scMathSqr
, 0);
284 tinyJS
->addNative("function Math.sqrt(a)", scMathSqrt
, 0);