+++ /dev/null
-/*\r
-** $Id: lmathlib.c,v 1.83.1.1 2013/04/12 18:48:47 roberto Exp $\r
-** Standard mathematical library\r
-** See Copyright Notice in lua.h\r
-*/\r
-\r
-\r
-#include <stdlib.h>\r
-#include <math.h>\r
-\r
-#define lmathlib_c\r
-#define LUA_LIB\r
-\r
-#include "lua.h"\r
-\r
-#include "lauxlib.h"\r
-#include "lualib.h"\r
-\r
-\r
-#undef PI\r
-#define PI ((lua_Number)(3.1415926535897932384626433832795))\r
-#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0))\r
-\r
-\r
-\r
-static int math_abs (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_sin (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_sinh (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_cos (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_cosh (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_tan (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_tanh (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_asin (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_acos (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_atan (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_atan2 (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1),\r
- luaL_checknumber(L, 2)));\r
- return 1;\r
-}\r
-\r
-static int math_ceil (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_floor (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_fmod (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),\r
- luaL_checknumber(L, 2)));\r
- return 1;\r
-}\r
-\r
-static int math_modf (lua_State *L) {\r
- lua_Number ip;\r
- lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip);\r
- lua_pushnumber(L, ip);\r
- lua_pushnumber(L, fp);\r
- return 2;\r
-}\r
-\r
-static int math_sqrt (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_pow (lua_State *L) {\r
- lua_Number x = luaL_checknumber(L, 1);\r
- lua_Number y = luaL_checknumber(L, 2);\r
- lua_pushnumber(L, l_mathop(pow)(x, y));\r
- return 1;\r
-}\r
-\r
-static int math_log (lua_State *L) {\r
- lua_Number x = luaL_checknumber(L, 1);\r
- lua_Number res;\r
- if (lua_isnoneornil(L, 2))\r
- res = l_mathop(log)(x);\r
- else {\r
- lua_Number base = luaL_checknumber(L, 2);\r
- if (base == (lua_Number)10.0) res = l_mathop(log10)(x);\r
- else res = l_mathop(log)(x)/l_mathop(log)(base);\r
- }\r
- lua_pushnumber(L, res);\r
- return 1;\r
-}\r
-\r
-#if defined(LUA_COMPAT_LOG10)\r
-static int math_log10 (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-#endif\r
-\r
-static int math_exp (lua_State *L) {\r
- lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));\r
- return 1;\r
-}\r
-\r
-static int math_deg (lua_State *L) {\r
- lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);\r
- return 1;\r
-}\r
-\r
-static int math_rad (lua_State *L) {\r
- lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);\r
- return 1;\r
-}\r
-\r
-static int math_frexp (lua_State *L) {\r
- int e;\r
- lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));\r
- lua_pushinteger(L, e);\r
- return 2;\r
-}\r
-\r
-static int math_ldexp (lua_State *L) {\r
- lua_Number x = luaL_checknumber(L, 1);\r
- int ep = luaL_checkint(L, 2);\r
- lua_pushnumber(L, l_mathop(ldexp)(x, ep));\r
- return 1;\r
-}\r
-\r
-\r
-\r
-static int math_min (lua_State *L) {\r
- int n = lua_gettop(L); /* number of arguments */\r
- lua_Number dmin = luaL_checknumber(L, 1);\r
- int i;\r
- for (i=2; i<=n; i++) {\r
- lua_Number d = luaL_checknumber(L, i);\r
- if (d < dmin)\r
- dmin = d;\r
- }\r
- lua_pushnumber(L, dmin);\r
- return 1;\r
-}\r
-\r
-\r
-static int math_max (lua_State *L) {\r
- int n = lua_gettop(L); /* number of arguments */\r
- lua_Number dmax = luaL_checknumber(L, 1);\r
- int i;\r
- for (i=2; i<=n; i++) {\r
- lua_Number d = luaL_checknumber(L, i);\r
- if (d > dmax)\r
- dmax = d;\r
- }\r
- lua_pushnumber(L, dmax);\r
- return 1;\r
-}\r
-\r
-\r
-static int math_random (lua_State *L) {\r
- /* the `%' avoids the (rare) case of r==1, and is needed also because on\r
- some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */\r
- lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;\r
- switch (lua_gettop(L)) { /* check number of arguments */\r
- case 0: { /* no arguments */\r
- lua_pushnumber(L, r); /* Number between 0 and 1 */\r
- break;\r
- }\r
- case 1: { /* only upper limit */\r
- lua_Number u = luaL_checknumber(L, 1);\r
- luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty");\r
- lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */\r
- break;\r
- }\r
- case 2: { /* lower and upper limits */\r
- lua_Number l = luaL_checknumber(L, 1);\r
- lua_Number u = luaL_checknumber(L, 2);\r
- luaL_argcheck(L, l <= u, 2, "interval is empty");\r
- lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */\r
- break;\r
- }\r
- default: return luaL_error(L, "wrong number of arguments");\r
- }\r
- return 1;\r
-}\r
-\r
-\r
-static int math_randomseed (lua_State *L) {\r
- srand(luaL_checkunsigned(L, 1));\r
- (void)rand(); /* discard first value to avoid undesirable correlations */\r
- return 0;\r
-}\r
-\r
-\r
-static const luaL_Reg mathlib[] = {\r
- {"abs", math_abs},\r
- {"acos", math_acos},\r
- {"asin", math_asin},\r
- {"atan2", math_atan2},\r
- {"atan", math_atan},\r
- {"ceil", math_ceil},\r
- {"cosh", math_cosh},\r
- {"cos", math_cos},\r
- {"deg", math_deg},\r
- {"exp", math_exp},\r
- {"floor", math_floor},\r
- {"fmod", math_fmod},\r
- {"frexp", math_frexp},\r
- {"ldexp", math_ldexp},\r
-#if defined(LUA_COMPAT_LOG10)\r
- {"log10", math_log10},\r
-#endif\r
- {"log", math_log},\r
- {"max", math_max},\r
- {"min", math_min},\r
- {"modf", math_modf},\r
- {"pow", math_pow},\r
- {"rad", math_rad},\r
- {"random", math_random},\r
- {"randomseed", math_randomseed},\r
- {"sinh", math_sinh},\r
- {"sin", math_sin},\r
- {"sqrt", math_sqrt},\r
- {"tanh", math_tanh},\r
- {"tan", math_tan},\r
- {NULL, NULL}\r
-};\r
-\r
-\r
-/*\r
-** Open math library\r
-*/\r
-LUAMOD_API int luaopen_math (lua_State *L) {\r
- luaL_newlib(L, mathlib);\r
- lua_pushnumber(L, PI);\r
- lua_setfield(L, -2, "pi");\r
- lua_pushnumber(L, HUGE_VAL);\r
- lua_setfield(L, -2, "huge");\r
- return 1;\r
-}\r
-\r