3 ** Mathematics library to LUA
6 char *rcs_mathlib
="$Id: mathlib.c,v 1.25 1997/06/19 18:03:04 roberto Exp $";
16 #define PI 3.14159265358979323846
18 #define TODEGREE(a) ((a)*180.0/PI)
19 #define TORAD(a) ((a)*PI/180.0)
21 static void math_abs (void)
23 double d
= luaL_check_number(1);
29 static void math_sin (void)
31 double d
= luaL_check_number(1);
32 lua_pushnumber (sin(TORAD(d
)));
37 static void math_cos (void)
39 double d
= luaL_check_number(1);
40 lua_pushnumber (cos(TORAD(d
)));
45 static void math_tan (void)
47 double d
= luaL_check_number(1);
48 lua_pushnumber (tan(TORAD(d
)));
52 static void math_asin (void)
54 double d
= luaL_check_number(1);
55 lua_pushnumber (TODEGREE(asin(d
)));
59 static void math_acos (void)
61 double d
= luaL_check_number(1);
62 lua_pushnumber (TODEGREE(acos(d
)));
66 static void math_atan (void)
68 double d
= luaL_check_number(1);
69 lua_pushnumber (TODEGREE(atan(d
)));
73 static void math_atan2 (void)
75 double d1
= luaL_check_number(1);
76 double d2
= luaL_check_number(2);
77 lua_pushnumber (TODEGREE(atan2(d1
, d2
)));
81 static void math_ceil (void)
83 double d
= luaL_check_number(1);
84 lua_pushnumber (ceil(d
));
88 static void math_floor (void)
90 double d
= luaL_check_number(1);
91 lua_pushnumber (floor(d
));
94 static void math_mod (void)
96 float x
= luaL_check_number(1);
97 float y
= luaL_check_number(2);
98 lua_pushnumber(fmod(x
, y
));
102 static void math_sqrt (void)
104 double d
= luaL_check_number(1);
105 lua_pushnumber (sqrt(d
));
109 static void math_pow (void)
111 double d1
= luaL_check_number(1);
112 double d2
= luaL_check_number(2);
113 lua_pushnumber(pow(d1
,d2
));
116 static void math_min (void)
119 double dmin
= luaL_check_number(i
);
120 while (lua_getparam(++i
) != LUA_NOOBJECT
)
122 double d
= luaL_check_number(i
);
123 if (d
< dmin
) dmin
= d
;
125 lua_pushnumber (dmin
);
128 static void math_max (void)
131 double dmax
= luaL_check_number(i
);
132 while (lua_getparam(++i
) != LUA_NOOBJECT
)
134 double d
= luaL_check_number(i
);
135 if (d
> dmax
) dmax
= d
;
137 lua_pushnumber (dmax
);
140 static void math_log (void)
142 double d
= luaL_check_number(1);
143 lua_pushnumber (log(d
));
147 static void math_log10 (void)
149 double d
= luaL_check_number(1);
150 lua_pushnumber (log10(d
));
154 static void math_exp (void)
156 double d
= luaL_check_number(1);
157 lua_pushnumber (exp(d
));
160 static void math_deg (void)
162 float d
= luaL_check_number(1);
163 lua_pushnumber (d
*180./PI
);
166 static void math_rad (void)
168 float d
= luaL_check_number(1);
169 lua_pushnumber (d
/180.*PI
);
172 static void math_random (void)
174 lua_pushnumber((double)(rand()%RAND_MAX
) / (double)RAND_MAX
);
177 static void math_randomseed (void)
179 srand(luaL_check_number(1));
183 static struct luaL_reg mathlib
[] = {
191 {"atan2", math_atan2
},
193 {"floor", math_floor
},
199 {"log10", math_log10
},
203 {"random", math_random
},
204 {"randomseed", math_randomseed
}
210 void mathlib_open (void)
212 luaL_openlib(mathlib
, (sizeof(mathlib
)/sizeof(mathlib
[0])));
213 lua_pushcfunction(math_pow
);
214 lua_pushnumber(0); /* to get its tag */
215 lua_settagmethod(lua_tag(lua_pop()), "pow");