1 /* $NetBSD: lmathlib.c,v 1.1.1.2 2012/03/15 00:08:05 alnsn Exp $ */
4 ** $Id: lmathlib.c,v 1.1.1.2 2012/03/15 00:08:05 alnsn Exp $
5 ** Standard mathematical library
6 ** See Copyright Notice in lua.h
23 #define PI (3.14159265358979323846)
24 #define RADIANS_PER_DEGREE (PI/180.0)
28 static int math_abs (lua_State
*L
) {
29 lua_pushnumber(L
, fabs(luaL_checknumber(L
, 1)));
33 static int math_sin (lua_State
*L
) {
34 lua_pushnumber(L
, sin(luaL_checknumber(L
, 1)));
38 static int math_sinh (lua_State
*L
) {
39 lua_pushnumber(L
, sinh(luaL_checknumber(L
, 1)));
43 static int math_cos (lua_State
*L
) {
44 lua_pushnumber(L
, cos(luaL_checknumber(L
, 1)));
48 static int math_cosh (lua_State
*L
) {
49 lua_pushnumber(L
, cosh(luaL_checknumber(L
, 1)));
53 static int math_tan (lua_State
*L
) {
54 lua_pushnumber(L
, tan(luaL_checknumber(L
, 1)));
58 static int math_tanh (lua_State
*L
) {
59 lua_pushnumber(L
, tanh(luaL_checknumber(L
, 1)));
63 static int math_asin (lua_State
*L
) {
64 lua_pushnumber(L
, asin(luaL_checknumber(L
, 1)));
68 static int math_acos (lua_State
*L
) {
69 lua_pushnumber(L
, acos(luaL_checknumber(L
, 1)));
73 static int math_atan (lua_State
*L
) {
74 lua_pushnumber(L
, atan(luaL_checknumber(L
, 1)));
78 static int math_atan2 (lua_State
*L
) {
79 lua_pushnumber(L
, atan2(luaL_checknumber(L
, 1), luaL_checknumber(L
, 2)));
83 static int math_ceil (lua_State
*L
) {
84 lua_pushnumber(L
, ceil(luaL_checknumber(L
, 1)));
88 static int math_floor (lua_State
*L
) {
89 lua_pushnumber(L
, floor(luaL_checknumber(L
, 1)));
93 static int math_fmod (lua_State
*L
) {
94 lua_pushnumber(L
, fmod(luaL_checknumber(L
, 1), luaL_checknumber(L
, 2)));
98 static int math_modf (lua_State
*L
) {
100 double fp
= 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
, sqrt(luaL_checknumber(L
, 1)));
111 static int math_pow (lua_State
*L
) {
112 lua_pushnumber(L
, pow(luaL_checknumber(L
, 1), luaL_checknumber(L
, 2)));
116 static int math_log (lua_State
*L
) {
117 lua_pushnumber(L
, log(luaL_checknumber(L
, 1)));
121 static int math_log10 (lua_State
*L
) {
122 lua_pushnumber(L
, log10(luaL_checknumber(L
, 1)));
126 static int math_exp (lua_State
*L
) {
127 lua_pushnumber(L
, exp(luaL_checknumber(L
, 1)));
131 static int math_deg (lua_State
*L
) {
132 lua_pushnumber(L
, luaL_checknumber(L
, 1)/RADIANS_PER_DEGREE
);
136 static int math_rad (lua_State
*L
) {
137 lua_pushnumber(L
, luaL_checknumber(L
, 1)*RADIANS_PER_DEGREE
);
141 static int math_frexp (lua_State
*L
) {
143 lua_pushnumber(L
, frexp(luaL_checknumber(L
, 1), &e
));
144 lua_pushinteger(L
, e
);
148 static int math_ldexp (lua_State
*L
) {
149 lua_pushnumber(L
, ldexp(luaL_checknumber(L
, 1), luaL_checkint(L
, 2)));
155 static int math_min (lua_State
*L
) {
156 int n
= lua_gettop(L
); /* number of arguments */
157 lua_Number dmin
= luaL_checknumber(L
, 1);
159 for (i
=2; i
<=n
; i
++) {
160 lua_Number d
= luaL_checknumber(L
, i
);
164 lua_pushnumber(L
, dmin
);
169 static int math_max (lua_State
*L
) {
170 int n
= lua_gettop(L
); /* number of arguments */
171 lua_Number dmax
= luaL_checknumber(L
, 1);
173 for (i
=2; i
<=n
; i
++) {
174 lua_Number d
= luaL_checknumber(L
, i
);
178 lua_pushnumber(L
, dmax
);
183 static int math_random (lua_State
*L
) {
184 /* the `%' avoids the (rare) case of r==1, and is needed also because on
185 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
186 lua_Number r
= (lua_Number
)(rand()%RAND_MAX
) / (lua_Number
)RAND_MAX
;
187 switch (lua_gettop(L
)) { /* check number of arguments */
188 case 0: { /* no arguments */
189 lua_pushnumber(L
, r
); /* Number between 0 and 1 */
192 case 1: { /* only upper limit */
193 int u
= luaL_checkint(L
, 1);
194 luaL_argcheck(L
, 1<=u
, 1, "interval is empty");
195 lua_pushnumber(L
, floor(r
*u
)+1); /* int between 1 and `u' */
198 case 2: { /* lower and upper limits */
199 int l
= luaL_checkint(L
, 1);
200 int u
= luaL_checkint(L
, 2);
201 luaL_argcheck(L
, l
<=u
, 2, "interval is empty");
202 lua_pushnumber(L
, floor(r
*(u
-l
+1))+l
); /* int between `l' and `u' */
205 default: return luaL_error(L
, "wrong number of arguments");
211 static int math_randomseed (lua_State
*L
) {
212 srand(luaL_checkint(L
, 1));
217 static const luaL_Reg mathlib
[] = {
221 {"atan2", math_atan2
},
228 {"floor", math_floor
},
230 {"frexp", math_frexp
},
231 {"ldexp", math_ldexp
},
232 {"log10", math_log10
},
239 {"random", math_random
},
240 {"randomseed", math_randomseed
},
253 LUALIB_API
int luaopen_math (lua_State
*L
) {
254 luaL_register(L
, LUA_MATHLIBNAME
, mathlib
);
255 lua_pushnumber(L
, PI
);
256 lua_setfield(L
, -2, "pi");
257 lua_pushnumber(L
, HUGE_VAL
);
258 lua_setfield(L
, -2, "huge");
259 #if defined(LUA_COMPAT_MOD)
260 lua_getfield(L
, -1, "fmod");
261 lua_setfield(L
, -2, "mod");