forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyJS_MathFunctions.cpp
executable file
·332 lines (250 loc) · 8.96 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* 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 "ascript_pch.hpp"
#include "TinyJS_MathFunctions.h"
#include "scriptMain.h"
#include "microVM.h"
#include <math.h>
#include <cstdlib>
#include <sstream>
using namespace std;
//TODO: Replace by constants
#define k_E exp(1.0)
#define k_PI 3.1415926535897932384626433832795
#define F_RNG(a,min,max) ((a)<(min) ? min : ((a)>(max) ? max : a ))
#define scGetDouble(a) ( ec->getParam(a).toDouble(ec) )
#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
ASValue scMathAbs(ExecutionContext* ec)
{
return jsDouble(fabs(ec->getParam(0).toDouble(ec)));
}
//Math.round(a) - returns nearest round of given value
ASValue scMathRound(ExecutionContext* ec)
{
return jsDouble(round(scGetDouble(0)));
}
//Math.min(a,b) - returns minimum of two given values
ASValue scMathMin(ExecutionContext* ec)
{
const double result = min(scGetDouble(0), scGetDouble(1));
return jsDouble(result);
}
//Math.max(a,b) - returns maximum of two given values
ASValue scMathMax(ExecutionContext* ec)
{
const double result = max(scGetDouble(0), scGetDouble(1));
return jsDouble(result);
}
//Math.range(x,a,b) - returns value limited between two given values
ASValue scMathRange(ExecutionContext* ec)
{
const double x = scGetDouble(0);
const double a = scGetDouble(1);
const double b = scGetDouble(2);
if (a < b)
return jsDouble(max(a, min(x, b)));
else
return jsDouble(max(b, min(x, a)));
}
//Math.sign(a) - returns sign of given value (-1==negative,0=zero,1=positive)
ASValue scMathSign(ExecutionContext* ec)
{
const double a = scGetDouble(0);
int result = 0;
if (a < 0)
result = -1;
else if (a > 0)
result = 1;
return jsInt(result);
}
//Math.PI() - returns PI value
ASValue scMathPI(ExecutionContext* ec)
{
return jsDouble(k_PI);
}
//Math.toDegrees(a) - returns degree value of a given angle in radians
ASValue scMathToDegrees(ExecutionContext* ec)
{
return jsDouble((180.0 / k_PI)*(scGetDouble(0)));
}
//Math.toRadians(a) - returns radians value of a given angle in degrees
ASValue scMathToRadians(ExecutionContext* ec)
{
return jsDouble((k_PI / 180.0)*(scGetDouble(0)));
}
//Math.sin(a) - returns trig. sine of given angle in radians
ASValue scMathSin(ExecutionContext* ec)
{
return jsDouble(sin(scGetDouble(0)));
}
//Math.asin(a) - returns trig. arcsine of given angle in radians
ASValue scMathASin(ExecutionContext* ec)
{
return jsDouble(asin(scGetDouble(0)));
}
//Math.cos(a) - returns trig. cosine of given angle in radians
ASValue scMathCos(ExecutionContext* ec)
{
return jsDouble(cos(scGetDouble(0)));
}
//Math.acos(a) - returns trig. arccosine of given angle in radians
ASValue scMathACos(ExecutionContext* ec)
{
return jsDouble(acos(scGetDouble(0)));
}
//Math.tan(a) - returns trig. tangent of given angle in radians
ASValue scMathTan(ExecutionContext* ec)
{
return jsDouble(tan(scGetDouble(0)));
}
//Math.atan(a) - returns trig. arctangent of given angle in radians
ASValue scMathATan(ExecutionContext* ec)
{
return jsDouble(atan(scGetDouble(0)));
}
//Math.sinh(a) - returns trig. hyperbolic sine of given angle in radians
ASValue scMathSinh(ExecutionContext* ec)
{
return jsDouble(sinh(scGetDouble(0)));
}
//Math.asinh(a) - returns trig. hyperbolic arcsine of given angle in radians
ASValue scMathASinh(ExecutionContext* ec)
{
return jsDouble(asinh(scGetDouble(0)));
}
//Math.cosh(a) - returns trig. hyperbolic cosine of given angle in radians
ASValue scMathCosh(ExecutionContext* ec)
{
return jsDouble(cosh(scGetDouble(0)));
}
//Math.acosh(a) - returns trig. hyperbolic arccosine of given angle in radians
ASValue scMathACosh(ExecutionContext* ec)
{
return jsDouble(acosh(scGetDouble(0)));
}
//Math.tanh(a) - returns trig. hyperbolic tangent of given angle in radians
ASValue scMathTanh(ExecutionContext* ec)
{
return jsDouble(tanh(scGetDouble(0)));
}
//Math.atan(a) - returns trig. hyperbolic arctangent of given angle in radians
ASValue scMathATanh(ExecutionContext* ec)
{
return jsDouble(atan(scGetDouble(0)));
}
//Math.E() - returns E Neplero value
ASValue scMathE(ExecutionContext* ec)
{
return jsDouble(k_E);
}
//Math.log(a) - returns natural logaritm (base E) of given value
ASValue scMathLog(ExecutionContext* ec)
{
return jsDouble(log(scGetDouble(0)));
}
//Math.log10(a) - returns logaritm(base 10) of given value
ASValue scMathLog10(ExecutionContext* ec)
{
return jsDouble(log10(scGetDouble(0)));
}
//Math.exp(a) - returns e raised to the power of a given number
ASValue scMathExp(ExecutionContext* ec)
{
return jsDouble(exp(scGetDouble(0)));
}
//Math.pow(a,b) - returns the result of a number raised to a power (a)^(b)
ASValue scMathPow(ExecutionContext* ec)
{
return jsDouble(pow(scGetDouble(0), scGetDouble(1)));
}
//Math.sqr(a) - returns square of given value
ASValue scMathSqr(ExecutionContext* ec)
{
return jsDouble((scGetDouble(0) * scGetDouble(0)));
}
//Math.sqrt(a) - returns square root of given value
ASValue scMathSqrt(ExecutionContext* ec)
{
return jsDouble(sqrt(scGetDouble(0)));
}
// ----------------------------------------------- Register Functions
void registerMathFunctions(Ref<JSObject> scope)
{
// --- Math and Trigonometry functions ---
addNative("function Math.abs(a)", scMathAbs, scope);
addNative("function Math.round(a)", scMathRound, scope);
addNative("function Math.min(a,b)", scMathMin, scope);
addNative("function Math.max(a,b)", scMathMax, scope);
addNative("function Math.range(x,a,b)", scMathRange, scope);
addNative("function Math.sign(a)", scMathSign, scope);
addNative("function Math.PI()", scMathPI, scope);
addNative("function Math.toDegrees(a)", scMathToDegrees, scope);
addNative("function Math.toRadians(a)", scMathToRadians, scope);
addNative("function Math.sin(a)", scMathSin, scope);
addNative("function Math.asin(a)", scMathASin, scope);
addNative("function Math.cos(a)", scMathCos, scope);
addNative("function Math.acos(a)", scMathACos, scope);
addNative("function Math.tan(a)", scMathTan, scope);
addNative("function Math.atan(a)", scMathATan, scope);
addNative("function Math.sinh(a)", scMathSinh, scope);
addNative("function Math.asinh(a)", scMathASinh, scope);
addNative("function Math.cosh(a)", scMathCosh, scope);
addNative("function Math.acosh(a)", scMathACosh, scope);
addNative("function Math.tanh(a)", scMathTanh, scope);
addNative("function Math.atanh(a)", scMathATanh, scope);
addNative("function Math.E()", scMathE, scope);
addNative("function Math.log(a)", scMathLog, scope);
addNative("function Math.log10(a)", scMathLog10, scope);
addNative("function Math.exp(a)", scMathExp, scope);
addNative("function Math.pow(a,b)", scMathPow, scope);
addNative("function Math.sqr(a)", scMathSqr, scope);
addNative("function Math.sqrt(a)", scMathSqrt, scope);
}