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
23 #define PI (3.14159265358979323846)
24 #define RADIANS_PER_DEGREE (PI/180.0)
25 #define DEGREES_PER_RADIAN (180.0/PI)
28 static int math_abs (lua_State
*L
) {
29 /* Was: lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); */
30 lua_Number n
= luaL_checknumber(L
, 1);
31 lua_pushnumber(L
, n
< 0 ? -n
: n
);
36 static int math_sin (lua_State
*L
) {
37 lua_pushnumber(L
, sin(luaL_checknumber(L
, 1)));
41 static int math_sinh (lua_State
*L
) {
42 lua_pushnumber(L
, sinh(luaL_checknumber(L
, 1)));
46 static int math_cos (lua_State
*L
) {
47 lua_pushnumber(L
, cos(luaL_checknumber(L
, 1)));
51 static int math_cosh (lua_State
*L
) {
52 lua_pushnumber(L
, cosh(luaL_checknumber(L
, 1)));
56 static int math_tan (lua_State
*L
) {
57 lua_pushnumber(L
, tan(luaL_checknumber(L
, 1)));
61 static int math_tanh (lua_State
*L
) {
62 lua_pushnumber(L
, tanh(luaL_checknumber(L
, 1)));
66 static int math_asin (lua_State
*L
) {
67 lua_pushnumber(L
, asin(luaL_checknumber(L
, 1)));
71 static int math_acos (lua_State
*L
) {
72 lua_pushnumber(L
, acos(luaL_checknumber(L
, 1)));
76 static int math_atan (lua_State
*L
) {
77 lua_pushnumber(L
, atan(luaL_checknumber(L
, 1)));
81 static int math_atan2 (lua_State
*L
) {
82 lua_pushnumber(L
, atan2(luaL_checknumber(L
, 1), luaL_checknumber(L
, 2)));
87 static int math_ceil (lua_State
*L
) {
88 /* Doesn't change anything in fixed point arithmetic */
89 lua_pushnumber(L
, luaL_checknumber(L
, 1));
93 static int math_floor (lua_State
*L
) {
94 /* Doesn't change anything in fixed point arithmetic */
95 lua_pushnumber(L
, luaL_checknumber(L
, 1));
99 static int math_fmod (lua_State
*L
) {
100 /* Was: lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); */
101 lua_pushnumber(L
, luaL_checknumber(L
, 1) % luaL_checknumber(L
, 2));
106 static int math_modf (lua_State
*L
) {
108 double fp
= modf(luaL_checknumber(L
, 1), &ip
);
109 lua_pushnumber(L
, ip
);
110 lua_pushnumber(L
, fp
);
114 static int math_sqrt (lua_State
*L
) {
115 lua_pushnumber(L
, sqrt(luaL_checknumber(L
, 1)));
119 static int math_pow (lua_State
*L
) {
120 lua_pushnumber(L
, pow(luaL_checknumber(L
, 1), luaL_checknumber(L
, 2)));
124 static int math_log (lua_State
*L
) {
125 lua_pushnumber(L
, log(luaL_checknumber(L
, 1)));
129 static int math_log10 (lua_State
*L
) {
130 lua_pushnumber(L
, log10(luaL_checknumber(L
, 1)));
134 static int math_exp (lua_State
*L
) {
135 lua_pushnumber(L
, exp(luaL_checknumber(L
, 1)));
139 static int math_deg (lua_State
*L
) {
140 lua_pushnumber(L
, luaL_checknumber(L
, 1)*DEGREES_PER_RADIAN
);
143 static int math_rad (lua_State
*L
) {
144 lua_pushnumber(L
, (luaL_checknumber(L
, 1)*100)/(DEGREES_PER_RADIAN
*100));
149 static int math_frexp (lua_State
*L
) {
151 lua_pushnumber(L
, frexp(luaL_checknumber(L
, 1), &e
));
152 lua_pushinteger(L
, e
);
156 static int math_ldexp (lua_State
*L
) {
157 lua_pushnumber(L
, ldexp(luaL_checknumber(L
, 1), luaL_checkint(L
, 2)));
163 static int math_min (lua_State
*L
) {
164 int n
= lua_gettop(L
); /* number of arguments */
165 lua_Number dmin
= luaL_checknumber(L
, 1);
167 for (i
=2; i
<=n
; i
++) {
168 lua_Number d
= luaL_checknumber(L
, i
);
172 lua_pushnumber(L
, dmin
);
177 static int math_max (lua_State
*L
) {
178 int n
= lua_gettop(L
); /* number of arguments */
179 lua_Number dmax
= luaL_checknumber(L
, 1);
181 for (i
=2; i
<=n
; i
++) {
182 lua_Number d
= luaL_checknumber(L
, i
);
186 lua_pushnumber(L
, dmax
);
191 static int math_random (lua_State
*L
) {
192 /* We're not SunOS */
193 lua_Number r
= (lua_Number
)(rb
->rand());
194 switch (lua_gettop(L
)) { /* check number of arguments */
195 case 0: { /* no arguments */
196 lua_pushnumber(L
, r
); /* Number between 0 and RAND_MAX */
199 case 1: { /* only upper limit */
200 int u
= luaL_checkint(L
, 1);
201 luaL_argcheck(L
, 1<=u
, 1, "interval is empty");
202 lua_pushnumber(L
, r
%u
+1); /* int between 1 and `u' */
205 case 2: { /* lower and upper limits */
206 int l
= luaL_checkint(L
, 1);
207 int u
= luaL_checkint(L
, 2);
208 luaL_argcheck(L
, l
<=u
, 2, "interval is empty");
209 lua_pushnumber(L
, r
%(u
-l
+1)+l
); /* int between `l' and `u' */
212 default: return luaL_error(L
, "wrong number of arguments");
218 static int math_randomseed (lua_State
*L
) {
219 rb
->srand(luaL_checkint(L
, 1));
224 static const luaL_Reg mathlib
[] = {
229 {"atan2", math_atan2
},
241 {"floor", math_floor
},
244 {"frexp", math_frexp
},
245 {"ldexp", math_ldexp
},
246 {"log10", math_log10
},
256 {"random", math_random
},
257 {"randomseed", math_randomseed
},
272 LUALIB_API
int luaopen_math (lua_State
*L
) {
273 luaL_register(L
, LUA_MATHLIBNAME
, mathlib
);
274 #if 0 /* No use in adding floating point constants when there's no FP */
275 lua_pushnumber(L
, PI
);
276 lua_setfield(L
, -2, "pi");
277 lua_pushnumber(L
, HUGE_VAL
);
278 lua_setfield(L
, -2, "huge");
279 #if defined(LUA_COMPAT_MOD)
280 lua_getfield(L
, -1, "fmod");
281 lua_setfield(L
, -2, "mod");