rlm@1: /* rlm@1: ** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ rlm@1: ** Standard mathematical library rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #include rlm@1: #include rlm@1: rlm@1: #define lmathlib_c rlm@1: #define LUA_LIB rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "lauxlib.h" rlm@1: #include "lualib.h" rlm@1: rlm@1: rlm@1: #undef PI rlm@1: #define PI (3.14159265358979323846) rlm@1: #define RADIANS_PER_DEGREE (PI/180.0) rlm@1: rlm@1: rlm@1: rlm@1: static int math_abs (lua_State *L) { rlm@1: lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_sin (lua_State *L) { rlm@1: lua_pushnumber(L, sin(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_sinh (lua_State *L) { rlm@1: lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_cos (lua_State *L) { rlm@1: lua_pushnumber(L, cos(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_cosh (lua_State *L) { rlm@1: lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_tan (lua_State *L) { rlm@1: lua_pushnumber(L, tan(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_tanh (lua_State *L) { rlm@1: lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_asin (lua_State *L) { rlm@1: lua_pushnumber(L, asin(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_acos (lua_State *L) { rlm@1: lua_pushnumber(L, acos(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_atan (lua_State *L) { rlm@1: lua_pushnumber(L, atan(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_atan2 (lua_State *L) { rlm@1: lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_ceil (lua_State *L) { rlm@1: lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_floor (lua_State *L) { rlm@1: lua_pushnumber(L, floor(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_fmod (lua_State *L) { rlm@1: lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_modf (lua_State *L) { rlm@1: double ip; rlm@1: double fp = modf(luaL_checknumber(L, 1), &ip); rlm@1: lua_pushnumber(L, ip); rlm@1: lua_pushnumber(L, fp); rlm@1: return 2; rlm@1: } rlm@1: rlm@1: static int math_sqrt (lua_State *L) { rlm@1: lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_pow (lua_State *L) { rlm@1: lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_log (lua_State *L) { rlm@1: lua_pushnumber(L, log(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_log10 (lua_State *L) { rlm@1: lua_pushnumber(L, log10(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_exp (lua_State *L) { rlm@1: lua_pushnumber(L, exp(luaL_checknumber(L, 1))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_deg (lua_State *L) { rlm@1: lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_rad (lua_State *L) { rlm@1: lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: static int math_frexp (lua_State *L) { rlm@1: int e; rlm@1: lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); rlm@1: lua_pushinteger(L, e); rlm@1: return 2; rlm@1: } rlm@1: rlm@1: static int math_ldexp (lua_State *L) { rlm@1: lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: static int math_min (lua_State *L) { rlm@1: int n = lua_gettop(L); /* number of arguments */ rlm@1: lua_Number dmin = luaL_checknumber(L, 1); rlm@1: int i; rlm@1: for (i=2; i<=n; i++) { rlm@1: lua_Number d = luaL_checknumber(L, i); rlm@1: if (d < dmin) rlm@1: dmin = d; rlm@1: } rlm@1: lua_pushnumber(L, dmin); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int math_max (lua_State *L) { rlm@1: int n = lua_gettop(L); /* number of arguments */ rlm@1: lua_Number dmax = luaL_checknumber(L, 1); rlm@1: int i; rlm@1: for (i=2; i<=n; i++) { rlm@1: lua_Number d = luaL_checknumber(L, i); rlm@1: if (d > dmax) rlm@1: dmax = d; rlm@1: } rlm@1: lua_pushnumber(L, dmax); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int math_random (lua_State *L) { rlm@1: /* the `%' avoids the (rare) case of r==1, and is needed also because on rlm@1: some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ rlm@1: lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; rlm@1: switch (lua_gettop(L)) { /* check number of arguments */ rlm@1: case 0: { /* no arguments */ rlm@1: lua_pushnumber(L, r); /* Number between 0 and 1 */ rlm@1: break; rlm@1: } rlm@1: case 1: { /* only upper limit */ rlm@1: int u = luaL_checkint(L, 1); rlm@1: luaL_argcheck(L, 1<=u, 1, "interval is empty"); rlm@1: lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ rlm@1: break; rlm@1: } rlm@1: case 2: { /* lower and upper limits */ rlm@1: int l = luaL_checkint(L, 1); rlm@1: int u = luaL_checkint(L, 2); rlm@1: luaL_argcheck(L, l<=u, 2, "interval is empty"); rlm@1: lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ rlm@1: break; rlm@1: } rlm@1: default: return luaL_error(L, "wrong number of arguments"); rlm@1: } rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int math_randomseed (lua_State *L) { rlm@1: srand(luaL_checkint(L, 1)); rlm@1: return 0; rlm@1: } rlm@1: rlm@1: rlm@1: static const luaL_Reg mathlib[] = { rlm@1: {"abs", math_abs}, rlm@1: {"acos", math_acos}, rlm@1: {"asin", math_asin}, rlm@1: {"atan2", math_atan2}, rlm@1: {"atan", math_atan}, rlm@1: {"ceil", math_ceil}, rlm@1: {"cosh", math_cosh}, rlm@1: {"cos", math_cos}, rlm@1: {"deg", math_deg}, rlm@1: {"exp", math_exp}, rlm@1: {"floor", math_floor}, rlm@1: {"fmod", math_fmod}, rlm@1: {"frexp", math_frexp}, rlm@1: {"ldexp", math_ldexp}, rlm@1: {"log10", math_log10}, rlm@1: {"log", math_log}, rlm@1: {"max", math_max}, rlm@1: {"min", math_min}, rlm@1: {"modf", math_modf}, rlm@1: {"pow", math_pow}, rlm@1: {"rad", math_rad}, rlm@1: {"random", math_random}, rlm@1: {"randomseed", math_randomseed}, rlm@1: {"sinh", math_sinh}, rlm@1: {"sin", math_sin}, rlm@1: {"sqrt", math_sqrt}, rlm@1: {"tanh", math_tanh}, rlm@1: {"tan", math_tan}, rlm@1: {NULL, NULL} rlm@1: }; rlm@1: rlm@1: rlm@1: /* rlm@1: ** Open math library rlm@1: */ rlm@1: LUALIB_API int luaopen_math (lua_State *L) { rlm@1: luaL_register(L, LUA_MATHLIBNAME, mathlib); rlm@1: lua_pushnumber(L, PI); rlm@1: lua_setfield(L, -2, "pi"); rlm@1: lua_pushnumber(L, HUGE_VAL); rlm@1: lua_setfield(L, -2, "huge"); rlm@1: #if defined(LUA_COMPAT_MOD) rlm@1: lua_getfield(L, -1, "fmod"); rlm@1: lua_setfield(L, -2, "mod"); rlm@1: #endif rlm@1: return 1; rlm@1: } rlm@1: