8 # define M_PI (3.14159265358979323846)
12 #define single_arg_func(InType, OutType, v, fn_cb) \
16 VXObject& ob = v->StackGet(2); \
17 arg = ob.As<InType>(); \
18 v->Push((OutType)(fn_cb(arg))); \
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
)
30 if(VX_FAILED(v
->GetInteger(2, &seed
)))
34 srand((unsigned int)seed
);
38 VXInteger
math_rand(VXState
* v
)
40 v
->Push(VXInteger(rand()));
44 VXInteger
math_abs(VXState
* v
)
50 VXInteger
math_sqrt(VXState
* v
)
56 VXInteger
math_fabs(VXState
* v
)
62 VXInteger
math_sin(VXState
* v
)
68 VXInteger
math_cos(VXState
* v
)
75 VXInteger
math_asin(VXState
* v
)
82 VXInteger
math_acos(VXState
* v
)
89 VXInteger
math_log(VXState
* v
)
96 VXInteger
math_log10(VXState
* v
)
103 VXInteger
math_tan(VXState
* v
)
110 VXInteger
math_atan(VXState
* v
)
117 VXInteger
math_floor(VXState
* v
)
124 VXInteger
math_ceil(VXState
* v
)
131 VXInteger
math_exp(VXState
* v
)
138 VXInteger
math_atan2(VXState
* v
)
144 v
->Push(VXFloat(atan2(p1
, p2
)));
149 VXInteger
math_pow(VXState
* v
)
154 v
->Push(VXFloat(pow(p1
, p2
)));
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"},
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
));