Imported from ../lua-2.5.tar.gz.
[lua.git] / clients / lib / mathlib.c
blobd8f8dc137b2e21ff5a312506413525d5a2cbf9c0
1 /*
2 ** mathlib.c
3 ** Mathematics library to LUA
4 */
6 char *rcs_mathlib="$Id: mathlib.c,v 1.18 1996/08/01 14:55:33 roberto Exp $";
8 #include <stdlib.h>
9 #include <math.h>
11 #include "lualib.h"
12 #include "lua.h"
14 #ifndef PI
15 #define PI 3.14159265358979323846
16 #endif
17 #define TODEGREE(a) ((a)*180.0/PI)
18 #define TORAD(a) ((a)*PI/180.0)
20 static void math_abs (void)
22 double d = lua_check_number(1, "abs");
23 if (d < 0) d = -d;
24 lua_pushnumber (d);
28 static void math_sin (void)
30 double d = lua_check_number(1, "sin");
31 lua_pushnumber (sin(TORAD(d)));
36 static void math_cos (void)
38 double d = lua_check_number(1, "cos");
39 lua_pushnumber (cos(TORAD(d)));
44 static void math_tan (void)
46 double d = lua_check_number(1, "tan");
47 lua_pushnumber (tan(TORAD(d)));
51 static void math_asin (void)
53 double d = lua_check_number(1, "asin");
54 lua_pushnumber (TODEGREE(asin(d)));
58 static void math_acos (void)
60 double d = lua_check_number(1, "acos");
61 lua_pushnumber (TODEGREE(acos(d)));
65 static void math_atan (void)
67 double d = lua_check_number(1, "atan");
68 lua_pushnumber (TODEGREE(atan(d)));
72 static void math_atan2 (void)
74 double d1 = lua_check_number(1, "atan2");
75 double d2 = lua_check_number(2, "atan2");
76 lua_pushnumber (TODEGREE(atan2(d1, d2)));
80 static void math_ceil (void)
82 double d = lua_check_number(1, "ceil");
83 lua_pushnumber (ceil(d));
87 static void math_floor (void)
89 double d = lua_check_number(1, "floor");
90 lua_pushnumber (floor(d));
93 static void math_mod (void)
95 float x = lua_check_number(1, "mod");
96 float y = lua_check_number(2, "mod");
97 lua_pushnumber(fmod(x, y));
101 static void math_sqrt (void)
103 double d = lua_check_number(1, "sqrt");
104 lua_pushnumber (sqrt(d));
107 static int old_pow;
109 static void math_pow (void)
111 lua_Object o1 = lua_getparam (1);
112 lua_Object o2 = lua_getparam (2);
113 lua_Object op = lua_getparam(3);
114 if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p')
116 lua_Object old = lua_getref(old_pow);
117 lua_pushobject(o1);
118 lua_pushobject(o2);
119 lua_pushobject(op);
120 if (lua_callfunction(old) != 0)
121 lua_error(NULL);
123 else
125 double d1 = lua_getnumber(o1);
126 double d2 = lua_getnumber(o2);
127 lua_pushnumber (pow(d1,d2));
131 static void math_min (void)
133 int i=1;
134 double dmin = lua_check_number(i, "min");
135 while (lua_getparam(++i) != LUA_NOOBJECT)
137 double d = lua_check_number(i, "min");
138 if (d < dmin) dmin = d;
140 lua_pushnumber (dmin);
143 static void math_max (void)
145 int i=1;
146 double dmax = lua_check_number(i, "max");
147 while (lua_getparam(++i) != LUA_NOOBJECT)
149 double d = lua_check_number(i, "max");
150 if (d > dmax) dmax = d;
152 lua_pushnumber (dmax);
155 static void math_log (void)
157 double d = lua_check_number(1, "log");
158 lua_pushnumber (log(d));
162 static void math_log10 (void)
164 double d = lua_check_number(1, "log10");
165 lua_pushnumber (log10(d));
169 static void math_exp (void)
171 double d = lua_check_number(1, "exp");
172 lua_pushnumber (exp(d));
175 static void math_deg (void)
177 float d = lua_check_number(1, "deg");
178 lua_pushnumber (d*180./PI);
181 static void math_rad (void)
183 float d = lua_check_number(1, "rad");
184 lua_pushnumber (d/180.*PI);
187 static void math_random (void)
189 lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX);
192 static void math_randomseed (void)
194 srand(lua_check_number(1, "randomseed"));
198 static struct lua_reg mathlib[] = {
199 {"abs", math_abs},
200 {"sin", math_sin},
201 {"cos", math_cos},
202 {"tan", math_tan},
203 {"asin", math_asin},
204 {"acos", math_acos},
205 {"atan", math_atan},
206 {"atan2", math_atan2},
207 {"ceil", math_ceil},
208 {"floor", math_floor},
209 {"mod", math_mod},
210 {"sqrt", math_sqrt},
211 {"min", math_min},
212 {"max", math_max},
213 {"log", math_log},
214 {"log10", math_log10},
215 {"exp", math_exp},
216 {"deg", math_deg},
217 {"rad", math_rad},
218 {"random", math_random},
219 {"randomseed", math_randomseed}
223 ** Open math library
225 void mathlib_open (void)
227 luaI_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
228 old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1);