4 * A single-file Javascript-alike engine
\r
6 * - Math and Trigonometry functions
\r
8 * Authored By O.Z.L.B. <ozlbinfo@gmail.com>
\r
10 * Copyright (C) 2011 O.Z.L.B.
\r
12 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
13 * this software and associated documentation files (the "Software"), to deal in
\r
14 * the Software without restriction, including without limitation the rights to
\r
15 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
\r
16 * of the Software, and to permit persons to whom the Software is furnished to do
\r
17 * so, subject to the following conditions:
\r
19 * The above copyright notice and this permission notice shall be included in all
\r
20 * copies or substantial portions of the Software.
\r
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
34 #include "TinyJS_MathFunctions.h"
\r
36 using namespace std;
\r
38 #define k_E exp(1.0)
\r
39 #define k_PI 3.1415926535897932384626433832795
\r
41 #define F_ABS(a) ((a)>=0 ? (a) : (-(a)))
\r
42 #define F_MIN(a,b) ((a)>(b) ? (b) : (a))
\r
43 #define F_MAX(a,b) ((a)>(b) ? (a) : (b))
\r
44 #define F_SGN(a) ((a)>0 ? 1 : ((a)<0 ? -1 : 0 ))
\r
45 #define F_RNG(a,min,max) ((a)<(min) ? min : ((a)>(max) ? max : a ))
\r
46 #define F_ROUND(a) ((a)>0 ? (int) ((a)+0.5) : (int) ((a)-0.5) )
\r
48 //CScriptVar shortcut macro
\r
49 #define scIsInt(a) ( c->getParameter(a)->isInt() )
\r
50 #define scIsDouble(a) ( c->getParameter(a)->isDouble() )
\r
51 #define scGetInt(a) ( c->getParameter(a)->getInt() )
\r
52 #define scGetDouble(a) ( c->getParameter(a)->getDouble() )
\r
53 #define scReturnInt(a) ( c->getReturnVar()->setInt(a) )
\r
54 #define scReturnDouble(a) ( c->getReturnVar()->setDouble(a) )
\r
59 double asinh( const double &value )
\r
64 returned = log(value + sqrt(value * value + 1));
\r
66 returned = -log(-value + sqrt(value * value + 1));
\r
71 double acosh( const double &value )
\r
76 returned = log(value + sqrt(value * value - 1));
\r
78 returned = -log(-value + sqrt(value * value - 1));
\r
85 //Math.abs(x) - returns absolute of given value
\r
86 void scMathAbs(CScriptVar *c, void *userdata) {
\r
87 if ( scIsInt("a") ) {
\r
88 scReturnInt( F_ABS( scGetInt("a") ) );
\r
89 } else if ( scIsDouble("a") ) {
\r
90 scReturnDouble( F_ABS( scGetDouble("a") ) );
\r
94 //Math.round(a) - returns nearest round of given value
\r
95 void scMathRound(CScriptVar *c, void *userdata) {
\r
96 if ( scIsInt("a") ) {
\r
97 scReturnInt( F_ROUND( scGetInt("a") ) );
\r
98 } else if ( scIsDouble("a") ) {
\r
99 scReturnDouble( F_ROUND( scGetDouble("a") ) );
\r
103 //Math.min(a,b) - returns minimum of two given values
\r
104 void scMathMin(CScriptVar *c, void *userdata) {
\r
105 if ( (scIsInt("a")) && (scIsInt("b")) ) {
\r
106 scReturnInt( F_MIN( scGetInt("a"), scGetInt("b") ) );
\r
108 scReturnDouble( F_MIN( scGetDouble("a"), scGetDouble("b") ) );
\r
112 //Math.max(a,b) - returns maximum of two given values
\r
113 void scMathMax(CScriptVar *c, void *userdata) {
\r
114 if ( (scIsInt("a")) && (scIsInt("b")) ) {
\r
115 scReturnInt( F_MAX( scGetInt("a"), scGetInt("b") ) );
\r
117 scReturnDouble( F_MAX( scGetDouble("a"), scGetDouble("b") ) );
\r
121 //Math.range(x,a,b) - returns value limited between two given values
\r
122 void scMathRange(CScriptVar *c, void *userdata) {
\r
123 if ( (scIsInt("x")) ) {
\r
124 scReturnInt( F_RNG( scGetInt("x"), scGetInt("a"), scGetInt("b") ) );
\r
126 scReturnDouble( F_RNG( scGetDouble("x"), scGetDouble("a"), scGetDouble("b") ) );
\r
130 //Math.sign(a) - returns sign of given value (-1==negative,0=zero,1=positive)
\r
131 void scMathSign(CScriptVar *c, void *userdata) {
\r
132 if ( scIsInt("a") ) {
\r
133 scReturnInt( F_SGN( scGetInt("a") ) );
\r
134 } else if ( scIsDouble("a") ) {
\r
135 scReturnDouble( F_SGN( scGetDouble("a") ) );
\r
139 //Math.PI() - returns PI value
\r
140 void scMathPI(CScriptVar *c, void *userdata) {
\r
141 scReturnDouble(k_PI);
\r
144 //Math.toDegrees(a) - returns degree value of a given angle in radians
\r
145 void scMathToDegrees(CScriptVar *c, void *userdata) {
\r
146 scReturnDouble( (180.0/k_PI)*( scGetDouble("a") ) );
\r
149 //Math.toRadians(a) - returns radians value of a given angle in degrees
\r
150 void scMathToRadians(CScriptVar *c, void *userdata) {
\r
151 scReturnDouble( (k_PI/180.0)*( scGetDouble("a") ) );
\r
154 //Math.sin(a) - returns trig. sine of given angle in radians
\r
155 void scMathSin(CScriptVar *c, void *userdata) {
\r
156 scReturnDouble( sin( scGetDouble("a") ) );
\r
159 //Math.asin(a) - returns trig. arcsine of given angle in radians
\r
160 void scMathASin(CScriptVar *c, void *userdata) {
\r
161 scReturnDouble( asin( scGetDouble("a") ) );
\r
164 //Math.cos(a) - returns trig. cosine of given angle in radians
\r
165 void scMathCos(CScriptVar *c, void *userdata) {
\r
166 scReturnDouble( cos( scGetDouble("a") ) );
\r
169 //Math.acos(a) - returns trig. arccosine of given angle in radians
\r
170 void scMathACos(CScriptVar *c, void *userdata) {
\r
171 scReturnDouble( acos( scGetDouble("a") ) );
\r
174 //Math.tan(a) - returns trig. tangent of given angle in radians
\r
175 void scMathTan(CScriptVar *c, void *userdata) {
\r
176 scReturnDouble( tan( scGetDouble("a") ) );
\r
179 //Math.atan(a) - returns trig. arctangent of given angle in radians
\r
180 void scMathATan(CScriptVar *c, void *userdata) {
\r
181 scReturnDouble( atan( scGetDouble("a") ) );
\r
184 //Math.sinh(a) - returns trig. hyperbolic sine of given angle in radians
\r
185 void scMathSinh(CScriptVar *c, void *userdata) {
\r
186 scReturnDouble( sinh( scGetDouble("a") ) );
\r
189 //Math.asinh(a) - returns trig. hyperbolic arcsine of given angle in radians
\r
190 void scMathASinh(CScriptVar *c, void *userdata) {
\r
191 scReturnDouble( asinh( scGetDouble("a") ) );
\r
194 //Math.cosh(a) - returns trig. hyperbolic cosine of given angle in radians
\r
195 void scMathCosh(CScriptVar *c, void *userdata) {
\r
196 scReturnDouble( cosh( scGetDouble("a") ) );
\r
199 //Math.acosh(a) - returns trig. hyperbolic arccosine of given angle in radians
\r
200 void scMathACosh(CScriptVar *c, void *userdata) {
\r
201 scReturnDouble( acosh( scGetDouble("a") ) );
\r
204 //Math.tanh(a) - returns trig. hyperbolic tangent of given angle in radians
\r
205 void scMathTanh(CScriptVar *c, void *userdata) {
\r
206 scReturnDouble( tanh( scGetDouble("a") ) );
\r
209 //Math.atan(a) - returns trig. hyperbolic arctangent of given angle in radians
\r
210 void scMathATanh(CScriptVar *c, void *userdata) {
\r
211 scReturnDouble( atan( scGetDouble("a") ) );
\r
214 //Math.E() - returns E Neplero value
\r
215 void scMathE(CScriptVar *c, void *userdata) {
\r
216 scReturnDouble(k_E);
\r
219 //Math.log(a) - returns natural logaritm (base E) of given value
\r
220 void scMathLog(CScriptVar *c, void *userdata) {
\r
221 scReturnDouble( log( scGetDouble("a") ) );
\r
224 //Math.log10(a) - returns logaritm(base 10) of given value
\r
225 void scMathLog10(CScriptVar *c, void *userdata) {
\r
226 scReturnDouble( log10( scGetDouble("a") ) );
\r
229 //Math.exp(a) - returns e raised to the power of a given number
\r
230 void scMathExp(CScriptVar *c, void *userdata) {
\r
231 scReturnDouble( exp( scGetDouble("a") ) );
\r
234 //Math.pow(a,b) - returns the result of a number raised to a power (a)^(b)
\r
235 void scMathPow(CScriptVar *c, void *userdata) {
\r
236 scReturnDouble( pow( scGetDouble("a"), scGetDouble("b") ) );
\r
239 //Math.sqr(a) - returns square of given value
\r
240 void scMathSqr(CScriptVar *c, void *userdata) {
\r
241 scReturnDouble( ( scGetDouble("a") * scGetDouble("a") ) );
\r
244 //Math.sqrt(a) - returns square root of given value
\r
245 void scMathSqrt(CScriptVar *c, void *userdata) {
\r
246 scReturnDouble( sqrtf( scGetDouble("a") ) );
\r
249 // ----------------------------------------------- Register Functions
\r
250 void registerMathFunctions(CTinyJS *tinyJS) {
\r
252 // --- Math and Trigonometry functions ---
\r
253 tinyJS->addNative("function Math.abs(a)", scMathAbs, 0);
\r
254 tinyJS->addNative("function Math.round(a)", scMathRound, 0);
\r
255 tinyJS->addNative("function Math.min(a,b)", scMathMin, 0);
\r
256 tinyJS->addNative("function Math.max(a,b)", scMathMax, 0);
\r
257 tinyJS->addNative("function Math.range(x,a,b)", scMathRange, 0);
\r
258 tinyJS->addNative("function Math.sign(a)", scMathSign, 0);
\r
260 tinyJS->addNative("function Math.PI()", scMathPI, 0);
\r
261 tinyJS->addNative("function Math.toDegrees(a)", scMathToDegrees, 0);
\r
262 tinyJS->addNative("function Math.toRadians(a)", scMathToRadians, 0);
\r
263 tinyJS->addNative("function Math.sin(a)", scMathSin, 0);
\r
264 tinyJS->addNative("function Math.asin(a)", scMathASin, 0);
\r
265 tinyJS->addNative("function Math.cos(a)", scMathCos, 0);
\r
266 tinyJS->addNative("function Math.acos(a)", scMathACos, 0);
\r
267 tinyJS->addNative("function Math.tan(a)", scMathTan, 0);
\r
268 tinyJS->addNative("function Math.atan(a)", scMathATan, 0);
\r
269 tinyJS->addNative("function Math.sinh(a)", scMathSinh, 0);
\r
270 tinyJS->addNative("function Math.asinh(a)", scMathASinh, 0);
\r
271 tinyJS->addNative("function Math.cosh(a)", scMathCosh, 0);
\r
272 tinyJS->addNative("function Math.acosh(a)", scMathACosh, 0);
\r
273 tinyJS->addNative("function Math.tanh(a)", scMathTanh, 0);
\r
274 tinyJS->addNative("function Math.atanh(a)", scMathATanh, 0);
\r
276 tinyJS->addNative("function Math.E()", scMathE, 0);
\r
277 tinyJS->addNative("function Math.log(a)", scMathLog, 0);
\r
278 tinyJS->addNative("function Math.log10(a)", scMathLog10, 0);
\r
279 tinyJS->addNative("function Math.exp(a)", scMathExp, 0);
\r
280 tinyJS->addNative("function Math.pow(a,b)", scMathPow, 0);
\r
282 tinyJS->addNative("function Math.sqr(a)", scMathSqr, 0);
\r
283 tinyJS->addNative("function Math.sqrt(a)", scMathSqrt, 0);
\r