-
Notifications
You must be signed in to change notification settings - Fork 88
/
TinyJS_MathFunctions.cpp
executable file
·285 lines (238 loc) · 10.2 KB
/
TinyJS_MathFunctions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* TinyJS
*
* A single-file Javascript-alike engine
*
* - Math and Trigonometry functions
*
* Authored By O.Z.L.B. <[email protected]>
*
* Copyright (C) 2011 O.Z.L.B.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <math.h>
#include <cstdlib>
#include <sstream>
#include "TinyJS_MathFunctions.h"
using namespace std;
#define k_E exp(1.0)
#define k_PI 3.1415926535897932384626433832795
#define F_ABS(a) ((a)>=0 ? (a) : (-(a)))
#define F_MIN(a,b) ((a)>(b) ? (b) : (a))
#define F_MAX(a,b) ((a)>(b) ? (a) : (b))
#define F_SGN(a) ((a)>0 ? 1 : ((a)<0 ? -1 : 0 ))
#define F_RNG(a,min,max) ((a)<(min) ? min : ((a)>(max) ? max : a ))
#define F_ROUND(a) ((a)>0 ? (int) ((a)+0.5) : (int) ((a)-0.5) )
//CScriptVar shortcut macro
#define scIsInt(a) ( c->getParameter(a)->isInt() )
#define scIsDouble(a) ( c->getParameter(a)->isDouble() )
#define scGetInt(a) ( c->getParameter(a)->getInt() )
#define scGetDouble(a) ( c->getParameter(a)->getDouble() )
#define scReturnInt(a) ( c->getReturnVar()->setInt(a) )
#define scReturnDouble(a) ( c->getReturnVar()->setDouble(a) )
#ifdef _MSC_VER
namespace
{
double asinh( const double &value )
{
double returned;
if(value>0)
returned = log(value + sqrt(value * value + 1));
else
returned = -log(-value + sqrt(value * value + 1));
return(returned);
}
double acosh( const double &value )
{
double returned;
if(value>0)
returned = log(value + sqrt(value * value - 1));
else
returned = -log(-value + sqrt(value * value - 1));
return(returned);
}
}
#endif
//Math.abs(x) - returns absolute of given value
void scMathAbs(CScriptVar *c, void *userdata) {
if ( scIsInt("a") ) {
scReturnInt( F_ABS( scGetInt("a") ) );
} else if ( scIsDouble("a") ) {
scReturnDouble( F_ABS( scGetDouble("a") ) );
}
}
//Math.round(a) - returns nearest round of given value
void scMathRound(CScriptVar *c, void *userdata) {
if ( scIsInt("a") ) {
scReturnInt( F_ROUND( scGetInt("a") ) );
} else if ( scIsDouble("a") ) {
scReturnDouble( F_ROUND( scGetDouble("a") ) );
}
}
//Math.min(a,b) - returns minimum of two given values
void scMathMin(CScriptVar *c, void *userdata) {
if ( (scIsInt("a")) && (scIsInt("b")) ) {
scReturnInt( F_MIN( scGetInt("a"), scGetInt("b") ) );
} else {
scReturnDouble( F_MIN( scGetDouble("a"), scGetDouble("b") ) );
}
}
//Math.max(a,b) - returns maximum of two given values
void scMathMax(CScriptVar *c, void *userdata) {
if ( (scIsInt("a")) && (scIsInt("b")) ) {
scReturnInt( F_MAX( scGetInt("a"), scGetInt("b") ) );
} else {
scReturnDouble( F_MAX( scGetDouble("a"), scGetDouble("b") ) );
}
}
//Math.range(x,a,b) - returns value limited between two given values
void scMathRange(CScriptVar *c, void *userdata) {
if ( (scIsInt("x")) ) {
scReturnInt( F_RNG( scGetInt("x"), scGetInt("a"), scGetInt("b") ) );
} else {
scReturnDouble( F_RNG( scGetDouble("x"), scGetDouble("a"), scGetDouble("b") ) );
}
}
//Math.sign(a) - returns sign of given value (-1==negative,0=zero,1=positive)
void scMathSign(CScriptVar *c, void *userdata) {
if ( scIsInt("a") ) {
scReturnInt( F_SGN( scGetInt("a") ) );
} else if ( scIsDouble("a") ) {
scReturnDouble( F_SGN( scGetDouble("a") ) );
}
}
//Math.PI() - returns PI value
void scMathPI(CScriptVar *c, void *userdata) {
scReturnDouble(k_PI);
}
//Math.toDegrees(a) - returns degree value of a given angle in radians
void scMathToDegrees(CScriptVar *c, void *userdata) {
scReturnDouble( (180.0/k_PI)*( scGetDouble("a") ) );
}
//Math.toRadians(a) - returns radians value of a given angle in degrees
void scMathToRadians(CScriptVar *c, void *userdata) {
scReturnDouble( (k_PI/180.0)*( scGetDouble("a") ) );
}
//Math.sin(a) - returns trig. sine of given angle in radians
void scMathSin(CScriptVar *c, void *userdata) {
scReturnDouble( sin( scGetDouble("a") ) );
}
//Math.asin(a) - returns trig. arcsine of given angle in radians
void scMathASin(CScriptVar *c, void *userdata) {
scReturnDouble( asin( scGetDouble("a") ) );
}
//Math.cos(a) - returns trig. cosine of given angle in radians
void scMathCos(CScriptVar *c, void *userdata) {
scReturnDouble( cos( scGetDouble("a") ) );
}
//Math.acos(a) - returns trig. arccosine of given angle in radians
void scMathACos(CScriptVar *c, void *userdata) {
scReturnDouble( acos( scGetDouble("a") ) );
}
//Math.tan(a) - returns trig. tangent of given angle in radians
void scMathTan(CScriptVar *c, void *userdata) {
scReturnDouble( tan( scGetDouble("a") ) );
}
//Math.atan(a) - returns trig. arctangent of given angle in radians
void scMathATan(CScriptVar *c, void *userdata) {
scReturnDouble( atan( scGetDouble("a") ) );
}
//Math.sinh(a) - returns trig. hyperbolic sine of given angle in radians
void scMathSinh(CScriptVar *c, void *userdata) {
scReturnDouble( sinh( scGetDouble("a") ) );
}
//Math.asinh(a) - returns trig. hyperbolic arcsine of given angle in radians
void scMathASinh(CScriptVar *c, void *userdata) {
scReturnDouble( asinh( (long double)scGetDouble("a") ) );
}
//Math.cosh(a) - returns trig. hyperbolic cosine of given angle in radians
void scMathCosh(CScriptVar *c, void *userdata) {
scReturnDouble( cosh( scGetDouble("a") ) );
}
//Math.acosh(a) - returns trig. hyperbolic arccosine of given angle in radians
void scMathACosh(CScriptVar *c, void *userdata) {
scReturnDouble( acosh( (long double)scGetDouble("a") ) );
}
//Math.tanh(a) - returns trig. hyperbolic tangent of given angle in radians
void scMathTanh(CScriptVar *c, void *userdata) {
scReturnDouble( tanh( scGetDouble("a") ) );
}
//Math.atan(a) - returns trig. hyperbolic arctangent of given angle in radians
void scMathATanh(CScriptVar *c, void *userdata) {
scReturnDouble( atan( scGetDouble("a") ) );
}
//Math.E() - returns E Neplero value
void scMathE(CScriptVar *c, void *userdata) {
scReturnDouble(k_E);
}
//Math.log(a) - returns natural logaritm (base E) of given value
void scMathLog(CScriptVar *c, void *userdata) {
scReturnDouble( log( scGetDouble("a") ) );
}
//Math.log10(a) - returns logaritm(base 10) of given value
void scMathLog10(CScriptVar *c, void *userdata) {
scReturnDouble( log10( scGetDouble("a") ) );
}
//Math.exp(a) - returns e raised to the power of a given number
void scMathExp(CScriptVar *c, void *userdata) {
scReturnDouble( exp( scGetDouble("a") ) );
}
//Math.pow(a,b) - returns the result of a number raised to a power (a)^(b)
void scMathPow(CScriptVar *c, void *userdata) {
scReturnDouble( pow( scGetDouble("a"), scGetDouble("b") ) );
}
//Math.sqr(a) - returns square of given value
void scMathSqr(CScriptVar *c, void *userdata) {
scReturnDouble( ( scGetDouble("a") * scGetDouble("a") ) );
}
//Math.sqrt(a) - returns square root of given value
void scMathSqrt(CScriptVar *c, void *userdata) {
scReturnDouble( sqrtf( scGetDouble("a") ) );
}
// ----------------------------------------------- Register Functions
void registerMathFunctions(CTinyJS *tinyJS) {
// --- Math and Trigonometry functions ---
tinyJS->addNative("function Math.abs(a)", scMathAbs, 0);
tinyJS->addNative("function Math.round(a)", scMathRound, 0);
tinyJS->addNative("function Math.min(a,b)", scMathMin, 0);
tinyJS->addNative("function Math.max(a,b)", scMathMax, 0);
tinyJS->addNative("function Math.range(x,a,b)", scMathRange, 0);
tinyJS->addNative("function Math.sign(a)", scMathSign, 0);
tinyJS->addNative("function Math.PI()", scMathPI, 0);
tinyJS->addNative("function Math.toDegrees(a)", scMathToDegrees, 0);
tinyJS->addNative("function Math.toRadians(a)", scMathToRadians, 0);
tinyJS->addNative("function Math.sin(a)", scMathSin, 0);
tinyJS->addNative("function Math.asin(a)", scMathASin, 0);
tinyJS->addNative("function Math.cos(a)", scMathCos, 0);
tinyJS->addNative("function Math.acos(a)", scMathACos, 0);
tinyJS->addNative("function Math.tan(a)", scMathTan, 0);
tinyJS->addNative("function Math.atan(a)", scMathATan, 0);
tinyJS->addNative("function Math.sinh(a)", scMathSinh, 0);
tinyJS->addNative("function Math.asinh(a)", scMathASinh, 0);
tinyJS->addNative("function Math.cosh(a)", scMathCosh, 0);
tinyJS->addNative("function Math.acosh(a)", scMathACosh, 0);
tinyJS->addNative("function Math.tanh(a)", scMathTanh, 0);
tinyJS->addNative("function Math.atanh(a)", scMathATanh, 0);
tinyJS->addNative("function Math.E()", scMathE, 0);
tinyJS->addNative("function Math.log(a)", scMathLog, 0);
tinyJS->addNative("function Math.log10(a)", scMathLog10, 0);
tinyJS->addNative("function Math.exp(a)", scMathExp, 0);
tinyJS->addNative("function Math.pow(a,b)", scMathPow, 0);
tinyJS->addNative("function Math.sqr(a)", scMathSqr, 0);
tinyJS->addNative("function Math.sqrt(a)", scMathSqrt, 0);
}