moved back to old acc
[vox.git] / src / core / baselib_math.cpp
blob504af0fc67e374fff0b2382f92a612d2029a2c39
2 #include "baselib.hpp"
3 #include <cmath>
4 #include <ctime>
7 #ifndef M_PI
8 # define M_PI (3.14159265358979323846)
9 #endif
12 #define single_arg_func(InType, OutType, v, fn_cb) \
13 do \
14 { \
15 InType arg; \
16 VXObject& ob = v->StackGet(2); \
17 arg = ob.As<InType>(); \
18 v->Push((OutType)(fn_cb(arg))); \
19 } while(0);
21 #define float_func(v, fn_cb) \
22 single_arg_func(VXFloat, VXFloat, v, fn_cb)
24 #define int_func(v, fn_cb) \
25 single_arg_func(VXInteger, VXInteger, v, fn_cb)
27 VXInteger math_srand(VXState* v)
29 VXInteger seed;
30 if(VX_FAILED(v->GetInteger(2, &seed)))
32 seed = time(NULL);
34 srand((unsigned int)seed);
35 return 0;
38 VXInteger math_rand(VXState* v)
40 v->Push(VXInteger(rand()));
41 return 1;
44 VXInteger math_abs(VXState* v)
46 int_func(v, cos);
47 return 1;
50 VXInteger math_sqrt(VXState* v)
52 float_func(v, sqrt);
53 return 1;
56 VXInteger math_fabs(VXState* v)
58 float_func(v, fabs);
59 return 1;
62 VXInteger math_sin(VXState* v)
64 float_func(v, sin);
65 return 1;
68 VXInteger math_cos(VXState* v)
70 float_func(v, cos);
71 return 1;
75 VXInteger math_asin(VXState* v)
77 float_func(v, asin);
78 return 1;
82 VXInteger math_acos(VXState* v)
84 float_func(v, acos)
85 return 1;
89 VXInteger math_log(VXState* v)
91 float_func(v, log)
92 return 1;
96 VXInteger math_log10(VXState* v)
98 float_func(v, log10)
99 return 1;
103 VXInteger math_tan(VXState* v)
105 float_func(v, tan)
106 return 1;
110 VXInteger math_atan(VXState* v)
112 float_func(v, atan)
113 return 1;
117 VXInteger math_floor(VXState* v)
119 float_func(v, floor)
120 return 1;
124 VXInteger math_ceil(VXState* v)
126 float_func(v, ceil)
127 return 1;
131 VXInteger math_exp(VXState* v)
133 float_func(v, ceil)
134 return 1;
138 VXInteger math_atan2(VXState* v)
140 VXFloat p1;
141 VXFloat p2;
142 v->GetFloat(2, &p1);
143 v->GetFloat(3, &p2);
144 v->Push(VXFloat(atan2(p1, p2)));
145 return 1;
149 VXInteger math_pow(VXState* v)
151 VXFloat p1, p2;
152 v->GetFloat(2, &p1);
153 v->GetFloat(3, &p2);
154 v->Push(VXFloat(pow(p1, p2)));
155 return 1;
159 static VXRegFunction vox_mathlib_funcs[] =
162 {"sqrt", math_sqrt, 2, ".n"},
163 {"sin", math_sin, 2, ".n"},
164 {"cos", math_cos, 2, ".n"},
165 {"asin", math_asin, 2, ".n"},
166 {"acos", math_acos, 2, ".n"},
167 {"log", math_log, 2, ".n"},
168 {"log10", math_log10, 2, ".n"},
169 {"tan", math_tan, 2, ".n"},
170 {"atan", math_atan, 2, ".n"},
171 {"atan2", math_atan2, 3, ".nn"},
172 {"pow", math_pow, 3, ".nn"},
173 {"floor", math_floor, 2, ".n"},
174 {"ceil", math_ceil, 2, ".n"},
175 {"exp", math_exp, 2, ".n"},
176 {"srand", math_srand, -1, ".n"},
177 {"rand", math_rand, 1, NULL},
178 {"fabs", math_fabs, 2, ".n"},
179 {"abs", math_abs, 2, ".n"},
180 {0, 0, 0, 0},
183 VXInteger voxstd_register_mathlib(VXState* v)
185 VXTableObj* tb = v->RegisterLib("math", vox_mathlib_funcs, true);
186 tb->NewSlot(v->NewString("PI"), VXFloat(M_PI));
187 v->Push(tb);
188 return 1;