2 ** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $
3 ** Standard mathematical library
4 ** See Copyright Notice in lua.h
21 #define PI (3.14159265358979323846)
22 #define RADIANS_PER_DEGREE (PI/180.0)
26 static int math_abs (lua_State *L) {
27 lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
31 static int math_sin (lua_State *L) {
32 lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
36 static int math_sinh (lua_State *L) {
37 lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
41 static int math_cos (lua_State *L) {
42 lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
46 static int math_cosh (lua_State *L) {
47 lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
51 static int math_tan (lua_State *L) {
52 lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
56 static int math_tanh (lua_State *L) {
57 lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
61 static int math_asin (lua_State *L) {
62 lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
66 static int math_acos (lua_State *L) {
67 lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
71 static int math_atan (lua_State *L) {
72 lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
76 static int math_atan2 (lua_State *L) {
77 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
81 static int math_ceil (lua_State *L) {
82 lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
86 static int math_floor (lua_State *L) {
87 lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
91 static int math_fmod (lua_State *L) {
92 lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
96 static int math_modf (lua_State *L) {
98 double fp = modf(luaL_checknumber(L, 1), &ip);
99 lua_pushnumber(L, ip);
100 lua_pushnumber(L, fp);
104 static int math_sqrt (lua_State *L) {
105 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
109 static int math_pow (lua_State *L) {
110 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
114 static int math_log (lua_State *L) {
115 lua_pushnumber(L, log(luaL_checknumber(L, 1)));
119 static int math_log10 (lua_State *L) {
120 lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
124 static int math_exp (lua_State *L) {
125 lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
129 static int math_deg (lua_State *L) {
130 lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
134 static int math_rad (lua_State *L) {
135 lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
139 static int math_frexp (lua_State *L) {
141 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
142 lua_pushinteger(L, e);
146 static int math_ldexp (lua_State *L) {
147 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
153 static int math_min (lua_State *L) {
154 int n = lua_gettop(L); /* number of arguments */
155 lua_Number dmin = luaL_checknumber(L, 1);
157 for (i=2; i<=n; i++) {
158 lua_Number d = luaL_checknumber(L, i);
162 lua_pushnumber(L, dmin);
167 static int math_max (lua_State *L) {
168 int n = lua_gettop(L); /* number of arguments */
169 lua_Number dmax = luaL_checknumber(L, 1);
171 for (i=2; i<=n; i++) {
172 lua_Number d = luaL_checknumber(L, i);
176 lua_pushnumber(L, dmax);
181 static int math_random (lua_State *L) {
182 /* the `%' avoids the (rare) case of r==1, and is needed also because on
183 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
184 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
185 switch (lua_gettop(L)) { /* check number of arguments */
186 case 0: { /* no arguments */
187 lua_pushnumber(L, r); /* Number between 0 and 1 */
190 case 1: { /* only upper limit */
191 int u = luaL_checkint(L, 1);
192 luaL_argcheck(L, 1<=u, 1, "interval is empty");
193 lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
196 case 2: { /* lower and upper limits */
197 int l = luaL_checkint(L, 1);
198 int u = luaL_checkint(L, 2);
199 luaL_argcheck(L, l<=u, 2, "interval is empty");
200 lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
203 default: return luaL_error(L, "wrong number of arguments");
209 static int math_randomseed (lua_State *L) {
210 srand(luaL_checkint(L, 1));
215 static const luaL_Reg mathlib[] = {
219 {"atan2", math_atan2},
226 {"floor", math_floor},
228 {"frexp", math_frexp},
229 {"ldexp", math_ldexp},
230 {"log10", math_log10},
237 {"random", math_random},
238 {"randomseed", math_randomseed},
251 LUALIB_API int luaopen_math (lua_State *L) {
252 luaL_register(L, LUA_MATHLIBNAME, mathlib);
253 lua_pushnumber(L, PI);
254 lua_setfield(L, -2, "pi");
255 lua_pushnumber(L, HUGE_VAL);
256 lua_setfield(L, -2, "huge");
257 #if defined(LUA_COMPAT_MOD)
258 lua_getfield(L, -1, "fmod");
259 lua_setfield(L, -2, "mod");