first commit, still mostly just touching up formatting
[tinyjs-rewrite.git] / src / mathfuncs.cpp
blob1d79882d805aa0d3425bd9fbd392cdf21ec443f4
1 /*
2 * TinyJS
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
28 * SOFTWARE.
31 #include <math.h>
32 #include <cstdlib>
33 #include <sstream>
34 #include "TinyJS_MathFunctions.h"
36 using namespace std;
38 #define k_E exp(1.0)
39 #define k_PI 3.1415926535897932384626433832795
41 #define F_ABS(a) ((a)>=0 ? (a) : (-(a)))
42 #define F_MIN(a,b) ((a)>(b) ? (b) : (a))
43 #define F_MAX(a,b) ((a)>(b) ? (a) : (b))
44 #define F_SGN(a) ((a)>0 ? 1 : ((a)<0 ? -1 : 0 ))
45 #define F_RNG(a,min,max) ((a)<(min) ? min : ((a)>(max) ? max : a ))
46 #define F_ROUND(a) ((a)>0 ? (int) ((a)+0.5) : (int) ((a)-0.5) )
48 //CScriptVar shortcut macro
49 #define scIsInt(a) ( c->getParameter(a)->isInt() )
50 #define scIsDouble(a) ( c->getParameter(a)->isDouble() )
51 #define scGetInt(a) ( c->getParameter(a)->getInt() )
52 #define scGetDouble(a) ( c->getParameter(a)->getDouble() )
53 #define scReturnInt(a) ( c->getReturnVar()->setInt(a) )
54 #define scReturnDouble(a) ( c->getReturnVar()->setDouble(a) )
56 #ifdef _MSC_VER
57 namespace
59 double asinh( const double &value )
61 double returned;
63 if(value>0)
64 returned = log(value + sqrt(value * value + 1));
65 else
66 returned = -log(-value + sqrt(value * value + 1));
68 return(returned);
71 double acosh( const double &value )
73 double returned;
75 if(value>0)
76 returned = log(value + sqrt(value * value - 1));
77 else
78 returned = -log(-value + sqrt(value * value - 1));
80 return(returned);
83 #endif
85 //Math.abs(x) - returns absolute of given value
86 void scMathAbs(CScriptVar *c, void *userdata) {
87 if ( scIsInt("a") ) {
88 scReturnInt( F_ABS( scGetInt("a") ) );
89 } else if ( scIsDouble("a") ) {
90 scReturnDouble( F_ABS( scGetDouble("a") ) );
94 //Math.round(a) - returns nearest round of given value
95 void scMathRound(CScriptVar *c, void *userdata) {
96 if ( scIsInt("a") ) {
97 scReturnInt( F_ROUND( scGetInt("a") ) );
98 } else if ( scIsDouble("a") ) {
99 scReturnDouble( F_ROUND( scGetDouble("a") ) );
103 //Math.min(a,b) - returns minimum of two given values
104 void scMathMin(CScriptVar *c, void *userdata) {
105 if ( (scIsInt("a")) && (scIsInt("b")) ) {
106 scReturnInt( F_MIN( scGetInt("a"), scGetInt("b") ) );
107 } else {
108 scReturnDouble( F_MIN( scGetDouble("a"), scGetDouble("b") ) );
112 //Math.max(a,b) - returns maximum of two given values
113 void scMathMax(CScriptVar *c, void *userdata) {
114 if ( (scIsInt("a")) && (scIsInt("b")) ) {
115 scReturnInt( F_MAX( scGetInt("a"), scGetInt("b") ) );
116 } else {
117 scReturnDouble( F_MAX( scGetDouble("a"), scGetDouble("b") ) );
121 //Math.range(x,a,b) - returns value limited between two given values
122 void scMathRange(CScriptVar *c, void *userdata) {
123 if ( (scIsInt("x")) ) {
124 scReturnInt( F_RNG( scGetInt("x"), scGetInt("a"), scGetInt("b") ) );
125 } else {
126 scReturnDouble( F_RNG( scGetDouble("x"), scGetDouble("a"), scGetDouble("b") ) );
130 //Math.sign(a) - returns sign of given value (-1==negative,0=zero,1=positive)
131 void scMathSign(CScriptVar *c, void *userdata) {
132 if ( scIsInt("a") ) {
133 scReturnInt( F_SGN( scGetInt("a") ) );
134 } else if ( scIsDouble("a") ) {
135 scReturnDouble( F_SGN( scGetDouble("a") ) );
139 //Math.PI() - returns PI value
140 void scMathPI(CScriptVar *c, void *userdata) {
141 scReturnDouble(k_PI);
144 //Math.toDegrees(a) - returns degree value of a given angle in radians
145 void scMathToDegrees(CScriptVar *c, void *userdata) {
146 scReturnDouble( (180.0/k_PI)*( scGetDouble("a") ) );
149 //Math.toRadians(a) - returns radians value of a given angle in degrees
150 void scMathToRadians(CScriptVar *c, void *userdata) {
151 scReturnDouble( (k_PI/180.0)*( scGetDouble("a") ) );
154 //Math.sin(a) - returns trig. sine of given angle in radians
155 void scMathSin(CScriptVar *c, void *userdata) {
156 scReturnDouble( sin( scGetDouble("a") ) );
159 //Math.asin(a) - returns trig. arcsine of given angle in radians
160 void scMathASin(CScriptVar *c, void *userdata) {
161 scReturnDouble( asin( scGetDouble("a") ) );
164 //Math.cos(a) - returns trig. cosine of given angle in radians
165 void scMathCos(CScriptVar *c, void *userdata) {
166 scReturnDouble( cos( scGetDouble("a") ) );
169 //Math.acos(a) - returns trig. arccosine of given angle in radians
170 void scMathACos(CScriptVar *c, void *userdata) {
171 scReturnDouble( acos( scGetDouble("a") ) );
174 //Math.tan(a) - returns trig. tangent of given angle in radians
175 void scMathTan(CScriptVar *c, void *userdata) {
176 scReturnDouble( tan( scGetDouble("a") ) );
179 //Math.atan(a) - returns trig. arctangent of given angle in radians
180 void scMathATan(CScriptVar *c, void *userdata) {
181 scReturnDouble( atan( scGetDouble("a") ) );
184 //Math.sinh(a) - returns trig. hyperbolic sine of given angle in radians
185 void scMathSinh(CScriptVar *c, void *userdata) {
186 scReturnDouble( sinh( scGetDouble("a") ) );
189 //Math.asinh(a) - returns trig. hyperbolic arcsine of given angle in radians
190 void scMathASinh(CScriptVar *c, void *userdata) {
191 scReturnDouble( asinh( scGetDouble("a") ) );
194 //Math.cosh(a) - returns trig. hyperbolic cosine of given angle in radians
195 void scMathCosh(CScriptVar *c, void *userdata) {
196 scReturnDouble( cosh( scGetDouble("a") ) );
199 //Math.acosh(a) - returns trig. hyperbolic arccosine of given angle in radians
200 void scMathACosh(CScriptVar *c, void *userdata) {
201 scReturnDouble( acosh( scGetDouble("a") ) );
204 //Math.tanh(a) - returns trig. hyperbolic tangent of given angle in radians
205 void scMathTanh(CScriptVar *c, void *userdata) {
206 scReturnDouble( tanh( scGetDouble("a") ) );
209 //Math.atan(a) - returns trig. hyperbolic arctangent of given angle in radians
210 void scMathATanh(CScriptVar *c, void *userdata) {
211 scReturnDouble( atan( scGetDouble("a") ) );
214 //Math.E() - returns E Neplero value
215 void scMathE(CScriptVar *c, void *userdata) {
216 scReturnDouble(k_E);
219 //Math.log(a) - returns natural logaritm (base E) of given value
220 void scMathLog(CScriptVar *c, void *userdata) {
221 scReturnDouble( log( scGetDouble("a") ) );
224 //Math.log10(a) - returns logaritm(base 10) of given value
225 void scMathLog10(CScriptVar *c, void *userdata) {
226 scReturnDouble( log10( scGetDouble("a") ) );
229 //Math.exp(a) - returns e raised to the power of a given number
230 void scMathExp(CScriptVar *c, void *userdata) {
231 scReturnDouble( exp( scGetDouble("a") ) );
234 //Math.pow(a,b) - returns the result of a number raised to a power (a)^(b)
235 void scMathPow(CScriptVar *c, void *userdata) {
236 scReturnDouble( pow( scGetDouble("a"), scGetDouble("b") ) );
239 //Math.sqr(a) - returns square of given value
240 void scMathSqr(CScriptVar *c, void *userdata) {
241 scReturnDouble( ( scGetDouble("a") * scGetDouble("a") ) );
244 //Math.sqrt(a) - returns square root of given value
245 void scMathSqrt(CScriptVar *c, void *userdata) {
246 scReturnDouble( sqrtf( scGetDouble("a") ) );
249 // ----------------------------------------------- Register Functions
250 void registerMathFunctions(CTinyJS *tinyJS) {
252 // --- Math and Trigonometry functions ---
253 tinyJS->addNative("function Math.abs(a)", scMathAbs, 0);
254 tinyJS->addNative("function Math.round(a)", scMathRound, 0);
255 tinyJS->addNative("function Math.min(a,b)", scMathMin, 0);
256 tinyJS->addNative("function Math.max(a,b)", scMathMax, 0);
257 tinyJS->addNative("function Math.range(x,a,b)", scMathRange, 0);
258 tinyJS->addNative("function Math.sign(a)", scMathSign, 0);
260 tinyJS->addNative("function Math.PI()", scMathPI, 0);
261 tinyJS->addNative("function Math.toDegrees(a)", scMathToDegrees, 0);
262 tinyJS->addNative("function Math.toRadians(a)", scMathToRadians, 0);
263 tinyJS->addNative("function Math.sin(a)", scMathSin, 0);
264 tinyJS->addNative("function Math.asin(a)", scMathASin, 0);
265 tinyJS->addNative("function Math.cos(a)", scMathCos, 0);
266 tinyJS->addNative("function Math.acos(a)", scMathACos, 0);
267 tinyJS->addNative("function Math.tan(a)", scMathTan, 0);
268 tinyJS->addNative("function Math.atan(a)", scMathATan, 0);
269 tinyJS->addNative("function Math.sinh(a)", scMathSinh, 0);
270 tinyJS->addNative("function Math.asinh(a)", scMathASinh, 0);
271 tinyJS->addNative("function Math.cosh(a)", scMathCosh, 0);
272 tinyJS->addNative("function Math.acosh(a)", scMathACosh, 0);
273 tinyJS->addNative("function Math.tanh(a)", scMathTanh, 0);
274 tinyJS->addNative("function Math.atanh(a)", scMathATanh, 0);
276 tinyJS->addNative("function Math.E()", scMathE, 0);
277 tinyJS->addNative("function Math.log(a)", scMathLog, 0);
278 tinyJS->addNative("function Math.log10(a)", scMathLog10, 0);
279 tinyJS->addNative("function Math.exp(a)", scMathExp, 0);
280 tinyJS->addNative("function Math.pow(a,b)", scMathPow, 0);
282 tinyJS->addNative("function Math.sqr(a)", scMathSqr, 0);
283 tinyJS->addNative("function Math.sqrt(a)", scMathSqrt, 0);