1 /* see copyright notice in squirrel.h */
7 #define SINGLE_ARG_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
10 sq_pushfloat(v,(SQFloat)_funcname(f)); \
14 #define TWO_ARGS_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
16 sq_getfloat(v,2,&p1); \
17 sq_getfloat(v,3,&p2); \
18 sq_pushfloat(v,(SQFloat)_funcname(p1,p2)); \
22 static SQInteger math_srand(HSQUIRRELVM v)
25 if(SQ_FAILED(sq_getinteger(v,2,&i)))
26 return sq_throwerror(v,_SC("invalid param"));
27 srand((unsigned int)i);
31 static SQInteger math_rand(HSQUIRRELVM v)
33 sq_pushinteger(v,rand());
37 static SQInteger math_abs(HSQUIRRELVM v)
40 sq_getinteger(v,2,&n);
41 sq_pushinteger(v,(SQInteger)abs((int)n));
52 SINGLE_ARG_FUNC(log10)
57 SINGLE_ARG_FUNC(floor)
61 #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),math_##name,nparams,tycheck}
62 static SQRegFunction mathlib_funcs[] = {
63 _DECL_FUNC(sqrt,2,_SC(".n")),
64 _DECL_FUNC(sin,2,_SC(".n")),
65 _DECL_FUNC(cos,2,_SC(".n")),
66 _DECL_FUNC(asin,2,_SC(".n")),
67 _DECL_FUNC(acos,2,_SC(".n")),
68 _DECL_FUNC(log,2,_SC(".n")),
69 _DECL_FUNC(log10,2,_SC(".n")),
70 _DECL_FUNC(tan,2,_SC(".n")),
71 _DECL_FUNC(atan,2,_SC(".n")),
72 _DECL_FUNC(atan2,3,_SC(".nn")),
73 _DECL_FUNC(pow,3,_SC(".nn")),
74 _DECL_FUNC(floor,2,_SC(".n")),
75 _DECL_FUNC(ceil,2,_SC(".n")),
76 _DECL_FUNC(exp,2,_SC(".n")),
77 _DECL_FUNC(srand,2,_SC(".n")),
78 _DECL_FUNC(rand,1,NULL),
79 _DECL_FUNC(fabs,2,_SC(".n")),
80 _DECL_FUNC(abs,2,_SC(".n")),
85 #define M_PI (3.14159265358979323846)
88 SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
91 while(mathlib_funcs[i].name!=0) {
92 sq_pushstring(v,mathlib_funcs[i].name,-1);
93 sq_newclosure(v,mathlib_funcs[i].f,0);
94 sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
95 sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
99 sq_pushstring(v,_SC("RAND_MAX"),-1);
100 sq_pushinteger(v,RAND_MAX);
102 sq_pushstring(v,_SC("PI"),-1);
103 sq_pushfloat(v,(SQFloat)M_PI);