Imported from ../lua-3.0.tar.gz.
[lua.git] / clients / lib / mathlib.c
blobe929d6aafc45f030e33380b157038470074a1499
1 /*
2 ** mathlib.c
3 ** Mathematics library to LUA
4 */
6 char *rcs_mathlib="$Id: mathlib.c,v 1.25 1997/06/19 18:03:04 roberto Exp $";
8 #include <stdlib.h>
9 #include <math.h>
11 #include "lualib.h"
12 #include "auxlib.h"
13 #include "lua.h"
15 #ifndef PI
16 #define PI 3.14159265358979323846
17 #endif
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);
24 if (d < 0) d = -d;
25 lua_pushnumber (d);
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)
118 int i=1;
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)
130 int i=1;
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[] = {
184 {"abs", math_abs},
185 {"sin", math_sin},
186 {"cos", math_cos},
187 {"tan", math_tan},
188 {"asin", math_asin},
189 {"acos", math_acos},
190 {"atan", math_atan},
191 {"atan2", math_atan2},
192 {"ceil", math_ceil},
193 {"floor", math_floor},
194 {"mod", math_mod},
195 {"sqrt", math_sqrt},
196 {"min", math_min},
197 {"max", math_max},
198 {"log", math_log},
199 {"log10", math_log10},
200 {"exp", math_exp},
201 {"deg", math_deg},
202 {"rad", math_rad},
203 {"random", math_random},
204 {"randomseed", math_randomseed}
208 ** Open math library
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");