-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmath.c
71 lines (47 loc) · 1.73 KB
/
math.c
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
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include "gracelib.h"
Object math_module = NULL;
Object math_sin(Object self, int nparams, int *argcv, Object *argv, int flags) {
return alloc_Float64(sin(*(double*) argv[0]->data));
}
Object math_cos(Object self, int nparams, int *argcv, Object *argv, int flags) {
return alloc_Float64(cos(*(double*) argv[0]->data));
}
Object math_tan(Object self, int nparams, int *argcv, Object *argv, int flags) {
return alloc_Float64(tan(*(double*) argv[0]->data));
}
Object math_asin(Object self, int nparams, int *argcv, Object *argv, int flags) {
return alloc_Float64(asin(*(double*) argv[0]->data));
}
Object math_acos(Object self, int nparams, int *argcv, Object *argv, int flags) {
return alloc_Float64(acos(*(double*) argv[0]->data));
}
Object math_atan(Object self, int nparams, int *argcv, Object *argv, int flags) {
return alloc_Float64(atan(*(double*) argv[0]->data));
}
Object math_random(Object self, int nparams, int *argcv, Object *argv, int flags) {
return alloc_Float64((double)rand() / RAND_MAX);
}
// Create and return a grace object which contains the above functions
Object module_math_init() {
if (math_module != NULL)
return math_module;
srand(time(NULL));
ClassData c = alloc_class("Module<math>", 7);
add_Method(c, "sin", &math_sin);
add_Method(c, "cos", &math_cos);
add_Method(c, "tan", &math_tan);
add_Method(c, "asin", &math_asin);
add_Method(c, "acos", &math_acos);
add_Method(c, "atan", &math_atan);
add_Method(c, "random", &math_random);
Object o = alloc_newobj(0, c);
math_module = o;
gc_root(math_module);
return o;
}