2 ** $Id: lmathlib.c,v 1.83 2013/03/07 18:21:32 roberto Exp $
3 ** Standard mathematical library
4 ** See Copyright Notice in lua.h
21 #define PI ((lua_Number)(3.1415926535897932384626433832795))
22 #define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0))
26 static int math_abs (lua_State
*L
) {
27 lua_pushnumber(L
, l_mathop(fabs
)(luaL_checknumber(L
, 1)));
31 static int math_sin (lua_State
*L
) {
32 lua_pushnumber(L
, l_mathop(sin
)(luaL_checknumber(L
, 1)));
36 static int math_sinh (lua_State
*L
) {
37 lua_pushnumber(L
, l_mathop(sinh
)(luaL_checknumber(L
, 1)));
41 static int math_cos (lua_State
*L
) {
42 lua_pushnumber(L
, l_mathop(cos
)(luaL_checknumber(L
, 1)));
46 static int math_cosh (lua_State
*L
) {
47 lua_pushnumber(L
, l_mathop(cosh
)(luaL_checknumber(L
, 1)));
51 static int math_tan (lua_State
*L
) {
52 lua_pushnumber(L
, l_mathop(tan
)(luaL_checknumber(L
, 1)));
56 static int math_tanh (lua_State
*L
) {
57 lua_pushnumber(L
, l_mathop(tanh
)(luaL_checknumber(L
, 1)));
61 static int math_asin (lua_State
*L
) {
62 lua_pushnumber(L
, l_mathop(asin
)(luaL_checknumber(L
, 1)));
66 static int math_acos (lua_State
*L
) {
67 lua_pushnumber(L
, l_mathop(acos
)(luaL_checknumber(L
, 1)));
71 static int math_atan (lua_State
*L
) {
72 lua_pushnumber(L
, l_mathop(atan
)(luaL_checknumber(L
, 1)));
76 static int math_atan2 (lua_State
*L
) {
77 lua_pushnumber(L
, l_mathop(atan2
)(luaL_checknumber(L
, 1),
78 luaL_checknumber(L
, 2)));
82 static int math_ceil (lua_State
*L
) {
83 lua_pushnumber(L
, l_mathop(ceil
)(luaL_checknumber(L
, 1)));
87 static int math_floor (lua_State
*L
) {
88 lua_pushnumber(L
, l_mathop(floor
)(luaL_checknumber(L
, 1)));
92 static int math_fmod (lua_State
*L
) {
93 lua_pushnumber(L
, l_mathop(fmod
)(luaL_checknumber(L
, 1),
94 luaL_checknumber(L
, 2)));
98 static int math_modf (lua_State
*L
) {
100 lua_Number fp
= l_mathop(modf
)(luaL_checknumber(L
, 1), &ip
);
101 lua_pushnumber(L
, ip
);
102 lua_pushnumber(L
, fp
);
106 static int math_sqrt (lua_State
*L
) {
107 lua_pushnumber(L
, l_mathop(sqrt
)(luaL_checknumber(L
, 1)));
111 static int math_pow (lua_State
*L
) {
112 lua_Number x
= luaL_checknumber(L
, 1);
113 lua_Number y
= luaL_checknumber(L
, 2);
114 lua_pushnumber(L
, l_mathop(pow
)(x
, y
));
118 static int math_log (lua_State
*L
) {
119 lua_Number x
= luaL_checknumber(L
, 1);
121 if (lua_isnoneornil(L
, 2))
122 res
= l_mathop(log
)(x
);
124 lua_Number base
= luaL_checknumber(L
, 2);
125 if (base
== (lua_Number
)10.0) res
= l_mathop(log10
)(x
);
126 else res
= l_mathop(log
)(x
)/l_mathop(log
)(base
);
128 lua_pushnumber(L
, res
);
132 #if defined(LUA_COMPAT_LOG10)
133 static int math_log10 (lua_State
*L
) {
134 lua_pushnumber(L
, l_mathop(log10
)(luaL_checknumber(L
, 1)));
139 static int math_exp (lua_State
*L
) {
140 lua_pushnumber(L
, l_mathop(exp
)(luaL_checknumber(L
, 1)));
144 static int math_deg (lua_State
*L
) {
145 lua_pushnumber(L
, luaL_checknumber(L
, 1)/RADIANS_PER_DEGREE
);
149 static int math_rad (lua_State
*L
) {
150 lua_pushnumber(L
, luaL_checknumber(L
, 1)*RADIANS_PER_DEGREE
);
154 static int math_frexp (lua_State
*L
) {
156 lua_pushnumber(L
, l_mathop(frexp
)(luaL_checknumber(L
, 1), &e
));
157 lua_pushinteger(L
, e
);
161 static int math_ldexp (lua_State
*L
) {
162 lua_Number x
= luaL_checknumber(L
, 1);
163 int ep
= luaL_checkint(L
, 2);
164 lua_pushnumber(L
, l_mathop(ldexp
)(x
, ep
));
170 static int math_min (lua_State
*L
) {
171 int n
= lua_gettop(L
); /* number of arguments */
172 lua_Number dmin
= luaL_checknumber(L
, 1);
174 for (i
=2; i
<=n
; i
++) {
175 lua_Number d
= luaL_checknumber(L
, i
);
179 lua_pushnumber(L
, dmin
);
184 static int math_max (lua_State
*L
) {
185 int n
= lua_gettop(L
); /* number of arguments */
186 lua_Number dmax
= luaL_checknumber(L
, 1);
188 for (i
=2; i
<=n
; i
++) {
189 lua_Number d
= luaL_checknumber(L
, i
);
193 lua_pushnumber(L
, dmax
);
198 static int math_random (lua_State
*L
) {
199 /* the `%' avoids the (rare) case of r==1, and is needed also because on
200 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
201 lua_Number r
= (lua_Number
)(rand()%RAND_MAX
) / (lua_Number
)RAND_MAX
;
202 switch (lua_gettop(L
)) { /* check number of arguments */
203 case 0: { /* no arguments */
204 lua_pushnumber(L
, r
); /* Number between 0 and 1 */
207 case 1: { /* only upper limit */
208 lua_Number u
= luaL_checknumber(L
, 1);
209 luaL_argcheck(L
, (lua_Number
)1.0 <= u
, 1, "interval is empty");
210 lua_pushnumber(L
, l_mathop(floor
)(r
*u
) + (lua_Number
)(1.0)); /* [1, u] */
213 case 2: { /* lower and upper limits */
214 lua_Number l
= luaL_checknumber(L
, 1);
215 lua_Number u
= luaL_checknumber(L
, 2);
216 luaL_argcheck(L
, l
<= u
, 2, "interval is empty");
217 lua_pushnumber(L
, l_mathop(floor
)(r
*(u
-l
+1)) + l
); /* [l, u] */
220 default: return luaL_error(L
, "wrong number of arguments");
226 static int math_randomseed (lua_State
*L
) {
227 srand(luaL_checkunsigned(L
, 1));
228 (void)rand(); /* discard first value to avoid undesirable correlations */
233 static const luaL_Reg mathlib
[] = {
237 {"atan2", math_atan2
},
244 {"floor", math_floor
},
246 {"frexp", math_frexp
},
247 {"ldexp", math_ldexp
},
248 #if defined(LUA_COMPAT_LOG10)
249 {"log10", math_log10
},
257 {"random", math_random
},
258 {"randomseed", math_randomseed
},
271 LUAMOD_API
int luaopen_math (lua_State
*L
) {
272 luaL_newlib(L
, mathlib
);
273 lua_pushnumber(L
, PI
);
274 lua_setfield(L
, -2, "pi");
275 lua_pushnumber(L
, HUGE_VAL
);
276 lua_setfield(L
, -2, "huge");