1 /* see copyright notice in squirrel.h */
7 #define SINGLE_ARG_FUNC(_funcname) static int math_##_funcname(HSQUIRRELVM v){ \
10 sq_pushfloat(v,(SQFloat)_funcname(f)); \
14 #define TWO_ARGS_FUNC(_funcname) static int 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 int math_srand(HSQUIRRELVM v)
25 if(!sq_getinteger(v,2,&i))return sq_throwerror(v,_SC("invalid param"));
30 static int math_rand(HSQUIRRELVM v)
32 sq_pushinteger(v,rand());
36 static int math_abs(HSQUIRRELVM v)
39 sq_getinteger(v,2,&n);
40 sq_pushinteger(v,(SQInteger)abs(n));
51 SINGLE_ARG_FUNC(log10)
56 SINGLE_ARG_FUNC(floor)
60 #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),math_##name,nparams,tycheck}
61 static SQRegFunction mathlib_funcs[] = {
62 _DECL_FUNC(sqrt,2,_SC(".n")),
63 _DECL_FUNC(sin,2,_SC(".n")),
64 _DECL_FUNC(cos,2,_SC(".n")),
65 _DECL_FUNC(asin,2,_SC(".n")),
66 _DECL_FUNC(acos,2,_SC(".n")),
67 _DECL_FUNC(log,2,_SC(".n")),
68 _DECL_FUNC(log10,2,_SC(".n")),
69 _DECL_FUNC(tan,2,_SC(".n")),
70 _DECL_FUNC(atan,2,_SC(".n")),
71 _DECL_FUNC(atan2,3,_SC(".nn")),
72 _DECL_FUNC(pow,3,_SC(".nn")),
73 _DECL_FUNC(floor,2,_SC(".n")),
74 _DECL_FUNC(ceil,2,_SC(".n")),
75 _DECL_FUNC(exp,2,_SC(".n")),
76 _DECL_FUNC(srand,2,_SC(".n")),
77 _DECL_FUNC(rand,1,NULL),
78 _DECL_FUNC(fabs,2,_SC(".n")),
79 _DECL_FUNC(abs,2,_SC(".n")),
84 #define M_PI (3.14159265358979323846)
87 SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
90 while(mathlib_funcs[i].name!=0) {
91 sq_pushstring(v,mathlib_funcs[i].name,-1);
92 sq_newclosure(v,mathlib_funcs[i].f,0);
93 sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
94 sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
98 sq_pushstring(v,_SC("RAND_MAX"),-1);
99 sq_pushinteger(v,RAND_MAX);
101 sq_pushstring(v,_SC("PI"),-1);
102 sq_pushfloat(v,(SQFloat)M_PI);