-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathModule.cpp
executable file
·430 lines (374 loc) · 9.89 KB
/
MathModule.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "tinypy.h"
#include <math.h>
#ifndef M_E
#define M_E 2.7182818284590452354
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include <errno.h>
/*
* template for tinypy math functions
* with one parameter.
*
* @cfunc is the coresponding function name in C
* math library.
*/
#define TP_MATH_FUNC1(cfunc) \
static tp_obj math_##cfunc(tp_vm *tp) { \
double x = TP_NUM(); \
double r = 0.0; \
\
errno = 0; \
r = cfunc(x); \
if (errno == EDOM || errno == ERANGE) { \
tp_raise(tp_None, tp_printf(tp, "%s(x): x=%f " \
"out of range", __func__, x)); \
} \
\
return (tp_number(r)); \
}
/*
* template for tinypy math functions
* with two parameters.
*
* @cfunc is the coresponding function name in C
* math library.
*/
#define TP_MATH_FUNC2(cfunc) \
static tp_obj math_##cfunc(tp_vm *tp) { \
double x = TP_NUM(); \
double y = TP_NUM(); \
double r = 0.0; \
\
errno = 0; \
r = cfunc(x, y); \
if (errno == EDOM || errno == ERANGE) { \
tp_raise(tp_None, tp_printf(tp, "%s(x, y): x=%f,y=%f " \
"out of range", __func__, x, y)); \
} \
\
return (tp_number(r)); \
}
/*
* PI definition: 3.1415926535897931
*/
static tp_obj math_pi;
/*
* E definition: 2.7182818284590451
*/
static tp_obj math_e;
/*
* acos(x)
*
* return arc cosine of x, return value is measured in radians.
* if x falls out -1 to 1, raise out-of-range exception.
*/
TP_MATH_FUNC1(acos)
/*
* asin(x)
*
* return arc sine of x, measured in radians, actually [-PI/2, PI/2]
* if x falls out of -1 to 1, raise out-of-range exception
*/
TP_MATH_FUNC1(asin)
/*
* atan(x)
*
* return arc tangent of x, measured in radians,
*/
TP_MATH_FUNC1(atan)
/*
* atan2(x, y)
*
* return arc tangent of x/y, measured in radians.
* unlike atan(x/y), both the signs of x and y
* are considered to determine the quaderant of
* the result.
*/
TP_MATH_FUNC2(atan2)
/*
* ceil(x)
*
* return the ceiling of x, i.e, the smallest
* integer >= x.
*/
TP_MATH_FUNC1(ceil)
/*
* cos(x)
*
* return cosine of x. x is measured in radians.
*/
TP_MATH_FUNC1(cos)
/*
* cosh(x)
*
* return hyperbolic cosine of x.
*/
TP_MATH_FUNC1(cosh)
/*
* degrees(x)
*
* converts angle x from radians to degrees.
* NOTE: this function is introduced by python,
* so we cannot wrap it directly in TP_MATH_FUNC1(),
* here the solution is defining a new
* C function - degrees().
*/
static const double degToRad =
3.141592653589793238462643383 / 180.0;
static double degrees(double x)
{
return (x / degToRad);
}
TP_MATH_FUNC1(degrees)
/*
* exp(x)
*
* return the value e raised to power of x.
* e is the base of natural logarithms.
*/
TP_MATH_FUNC1(exp)
/*
* fabs(x)
*
* return the absolute value of x.
*/
TP_MATH_FUNC1(fabs)
/*
* floor(x)
*
* return the floor of x, i.e, the largest integer <= x
*/
TP_MATH_FUNC1(floor)
/*
* fmod(x, y)
*
* return the remainder of dividing x by y. that is,
* return x - n * y, where n is the quotient of x/y.
* NOTE: this function relies on the underlying platform.
*/
TP_MATH_FUNC2(fmod)
/*
* frexp(x)
*
* return a pair (r, y), which satisfies:
* x = r * (2 ** y), and r is normalized fraction
* which is laid between 1/2 <= abs(r) < 1.
* if x = 0, the (r, y) = (0, 0).
*/
static tp_obj math_frexp(tp_vm *tp) {
double x = TP_NUM();
int y = 0;
double r = 0.0;
tp_obj rList = tp_list(tp);
errno = 0;
r = frexp(x, &y);
if (errno == EDOM || errno == ERANGE) {
tp_raise(tp_None, tp_printf(tp, "%s(x): x=%f, ""out of range", __func__, x));
}
_tp_list_append(tp, rList.list.val, tp_number(r));
_tp_list_append(tp, rList.list.val, tp_number((double)y));
return rList;
}
/*
* hypot(x, y)
*
* return Euclidean distance, namely,
* sqrt(x*x + y*y)
*/
TP_MATH_FUNC2(hypot)
/*
* ldexp(x, y)
*
* return the result of multiplying x by 2
* raised to y.
*/
TP_MATH_FUNC2(ldexp)
/*
* log(x, [base])
*
* return logarithm of x to given base. If base is
* not given, return the natural logarithm of x.
* Note: common logarithm(log10) is used to compute
* the denominator and numerator. based on fomula:
* log(x, base) = log10(x) / log10(base).
*/
static tp_obj math_log(tp_vm *tp) {
double x = TP_NUM();
tp_obj b = TP_DEFAULT(tp_None);
double y = 0.0;
double den = 0.0; /* denominator */
double num = 0.0; /* numinator */
double r = 0.0; /* result */
if (b.type == TP_NONE)
y = M_E;
else if (b.type == TP_NUMBER)
y = (double)b.number.val;
else
tp_raise(tp_None, tp_printf(tp, "%s(x, [base]): base invalid", __func__));
errno = 0;
num = log10(x);
if (errno == EDOM || errno == ERANGE)
goto excep;
errno = 0;
den = log10(y);
if (errno == EDOM || errno == ERANGE)
goto excep;
r = num / den;
return (tp_number(r));
excep:
tp_raise(tp_None, tp_printf(tp, "%s(x, y): x=%f,y=%f "
"out of range", __func__, x, y));
}
/*
* log10(x)
*
* return 10-based logarithm of x.
*/
TP_MATH_FUNC1(log10)
/*
* modf(x)
*
* return a pair (r, y). r is the integral part of
* x and y is the fractional part of x, both holds
* the same sign as x.
*/
static tp_obj math_modf(tp_vm *tp) {
double x = TP_NUM();
double y = 0.0;
double r = 0.0;
tp_obj rList = tp_list(tp);
errno = 0;
r = modf(x, &y);
if (errno == EDOM || errno == ERANGE) {
tp_raise(tp_None, tp_printf(tp, "%s(x): x=%f, "
"out of range", __func__, x));
}
_tp_list_append(tp, rList.list.val, tp_number(r));
_tp_list_append(tp, rList.list.val, tp_number(y));
return (rList);
}
/*
* pow(x, y)
*
* return value of x raised to y. equivalence of x ** y.
* NOTE: conventionally, tp_pow() is the implementation
* of builtin function pow(); whilst, math_pow() is an
* alternative in math module.
*/
static tp_obj math_pow(tp_vm *tp) {
double x = TP_NUM();
double y = TP_NUM();
double r = 0.0;
errno = 0;
r = pow(x, y);
if (errno == EDOM || errno == ERANGE) {
tp_raise(tp_None, tp_printf(tp, "%s(x, y): x=%f,y=%f "
"out of range", __func__, x, y));
}
return (tp_number(r));
}
/*
* radians(x)
*
* converts angle x from degrees to radians.
* NOTE: this function is introduced by python,
* adopt same solution as degrees(x).
*/
static double radians(double x)
{
return (x * degToRad);
}
TP_MATH_FUNC1(radians)
/*
* sin(x)
*
* return sine of x, x is measured in radians.
*/
TP_MATH_FUNC1(sin)
/*
* sinh(x)
*
* return hyperbolic sine of x.
* mathematically, sinh(x) = (exp(x) - exp(-x)) / 2.
*/
TP_MATH_FUNC1(sinh)
/*
* sqrt(x)
*
* return square root of x.
* if x is negtive, raise out-of-range exception.
*/
TP_MATH_FUNC1(sqrt)
/*
* tan(x)
*
* return tangent of x, x is measured in radians.
*/
TP_MATH_FUNC1(tan)
/*
* tanh(x)
*
* return hyperbolic tangent of x.
* mathematically, tanh(x) = sinh(x) / cosh(x).
*/
TP_MATH_FUNC1(tanh)
/*
* init math module, namely, set its dictionary
*/
void math_init(tp_vm *tp)
{
/*
* new a module dict for math
*/
tp_obj math_mod = tp_dict(tp);
/*
* initialize pi and e
*/
math_pi = tp_number(M_PI);
math_e = tp_number(M_E);
/*
* bind math functions to math module
*/
tp_set(tp, math_mod, tp_string("pi"), math_pi);
tp_set(tp, math_mod, tp_string("e"), math_e);
tp_set(tp, math_mod, tp_string("acos"), tp_fnc(tp, math_acos));
tp_set(tp, math_mod, tp_string("asin"), tp_fnc(tp, math_asin));
tp_set(tp, math_mod, tp_string("atan"), tp_fnc(tp, math_atan));
tp_set(tp, math_mod, tp_string("atan2"), tp_fnc(tp, math_atan2));
tp_set(tp, math_mod, tp_string("ceil"), tp_fnc(tp, math_ceil));
tp_set(tp, math_mod, tp_string("cos"), tp_fnc(tp, math_cos));
tp_set(tp, math_mod, tp_string("cosh"), tp_fnc(tp, math_cosh));
tp_set(tp, math_mod, tp_string("degrees"), tp_fnc(tp, math_degrees));
tp_set(tp, math_mod, tp_string("exp"), tp_fnc(tp, math_exp));
tp_set(tp, math_mod, tp_string("fabs"), tp_fnc(tp, math_fabs));
tp_set(tp, math_mod, tp_string("floor"), tp_fnc(tp, math_floor));
tp_set(tp, math_mod, tp_string("fmod"), tp_fnc(tp, math_fmod));
tp_set(tp, math_mod, tp_string("frexp"), tp_fnc(tp, math_frexp));
tp_set(tp, math_mod, tp_string("hypot"), tp_fnc(tp, math_hypot));
tp_set(tp, math_mod, tp_string("ldexp"), tp_fnc(tp, math_ldexp));
tp_set(tp, math_mod, tp_string("log"), tp_fnc(tp, math_log));
tp_set(tp, math_mod, tp_string("log10"), tp_fnc(tp, math_log10));
tp_set(tp, math_mod, tp_string("modf"), tp_fnc(tp, math_modf));
tp_set(tp, math_mod, tp_string("pow"), tp_fnc(tp, math_pow));
tp_set(tp, math_mod, tp_string("radians"), tp_fnc(tp, math_radians));
tp_set(tp, math_mod, tp_string("sin"), tp_fnc(tp, math_sin));
tp_set(tp, math_mod, tp_string("sinh"), tp_fnc(tp, math_sinh));
tp_set(tp, math_mod, tp_string("sqrt"), tp_fnc(tp, math_sqrt));
tp_set(tp, math_mod, tp_string("tan"), tp_fnc(tp, math_tan));
tp_set(tp, math_mod, tp_string("tanh"), tp_fnc(tp, math_tanh));
/*
* bind special attributes to math module
*/
tp_set(tp, math_mod, tp_string("__doc__"),
tp_string(
"This module is always available. It provides access to the\n"
"mathematical functions defined by the C standard."));
tp_set(tp, math_mod, tp_string("__name__"), tp_string("math"));
tp_set(tp, math_mod, tp_string("__file__"), tp_string(__FILE__));
/*
* bind to tiny modules[]
*/
tp_set(tp, tp->modules, tp_string("math"), math_mod);
}