Build doom on clipv2 and clip+
[kugel-rb.git] / apps / plugins / lua / lmathlib.c
blob99a104050c2543d149a7c76c4671afc0421e4341
1 /*
2 ** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $
3 ** Standard mathematical library
4 ** See Copyright Notice in lua.h
5 */
8 #if 0
9 #include <stdlib.h>
10 #include <math.h>
11 #endif
13 #define lmathlib_c
14 #define LUA_LIB
16 #include "lua.h"
18 #include "lauxlib.h"
19 #include "lualib.h"
22 #undef PI
23 #define PI (3.14159265358979323846)
24 #define RADIANS_PER_DEGREE (PI/180.0)
25 #define DEGREES_PER_RADIAN (180.0/PI)
28 static int math_abs (lua_State *L) {
29 /* Was: lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); */
30 lua_Number n = luaL_checknumber(L, 1);
31 lua_pushnumber(L, n < 0 ? -n : n);
32 return 1;
35 #if 0
36 static int math_sin (lua_State *L) {
37 lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
38 return 1;
41 static int math_sinh (lua_State *L) {
42 lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
43 return 1;
46 static int math_cos (lua_State *L) {
47 lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
48 return 1;
51 static int math_cosh (lua_State *L) {
52 lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
53 return 1;
56 static int math_tan (lua_State *L) {
57 lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
58 return 1;
61 static int math_tanh (lua_State *L) {
62 lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
63 return 1;
66 static int math_asin (lua_State *L) {
67 lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
68 return 1;
71 static int math_acos (lua_State *L) {
72 lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
73 return 1;
76 static int math_atan (lua_State *L) {
77 lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
78 return 1;
81 static int math_atan2 (lua_State *L) {
82 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
83 return 1;
85 #endif
87 static int math_ceil (lua_State *L) {
88 /* Doesn't change anything in fixed point arithmetic */
89 lua_pushnumber(L, luaL_checknumber(L, 1));
90 return 1;
93 static int math_floor (lua_State *L) {
94 /* Doesn't change anything in fixed point arithmetic */
95 lua_pushnumber(L, luaL_checknumber(L, 1));
96 return 1;
99 static int math_fmod (lua_State *L) {
100 /* Was: lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); */
101 lua_pushnumber(L, luaL_checknumber(L, 1) % luaL_checknumber(L, 2));
102 return 1;
105 #if 0
106 static int math_modf (lua_State *L) {
107 double ip;
108 double fp = modf(luaL_checknumber(L, 1), &ip);
109 lua_pushnumber(L, ip);
110 lua_pushnumber(L, fp);
111 return 2;
114 static int math_sqrt (lua_State *L) {
115 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
116 return 1;
119 static int math_pow (lua_State *L) {
120 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
121 return 1;
124 static int math_log (lua_State *L) {
125 lua_pushnumber(L, log(luaL_checknumber(L, 1)));
126 return 1;
129 static int math_log10 (lua_State *L) {
130 lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
131 return 1;
134 static int math_exp (lua_State *L) {
135 lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
136 return 1;
138 #endif
139 static int math_deg (lua_State *L) {
140 lua_pushnumber(L, luaL_checknumber(L, 1)*DEGREES_PER_RADIAN);
141 return 1;
143 static int math_rad (lua_State *L) {
144 lua_pushnumber(L, (luaL_checknumber(L, 1)*100)/(DEGREES_PER_RADIAN*100));
145 return 1;
148 #if 0
149 static int math_frexp (lua_State *L) {
150 int e;
151 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
152 lua_pushinteger(L, e);
153 return 2;
156 static int math_ldexp (lua_State *L) {
157 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
158 return 1;
160 #endif
163 static int math_min (lua_State *L) {
164 int n = lua_gettop(L); /* number of arguments */
165 lua_Number dmin = luaL_checknumber(L, 1);
166 int i;
167 for (i=2; i<=n; i++) {
168 lua_Number d = luaL_checknumber(L, i);
169 if (d < dmin)
170 dmin = d;
172 lua_pushnumber(L, dmin);
173 return 1;
177 static int math_max (lua_State *L) {
178 int n = lua_gettop(L); /* number of arguments */
179 lua_Number dmax = luaL_checknumber(L, 1);
180 int i;
181 for (i=2; i<=n; i++) {
182 lua_Number d = luaL_checknumber(L, i);
183 if (d > dmax)
184 dmax = d;
186 lua_pushnumber(L, dmax);
187 return 1;
191 static int math_random (lua_State *L) {
192 /* We're not SunOS */
193 lua_Number r = (lua_Number)(rb->rand());
194 switch (lua_gettop(L)) { /* check number of arguments */
195 case 0: { /* no arguments */
196 lua_pushnumber(L, r); /* Number between 0 and RAND_MAX */
197 break;
199 case 1: { /* only upper limit */
200 int u = luaL_checkint(L, 1);
201 luaL_argcheck(L, 1<=u, 1, "interval is empty");
202 lua_pushnumber(L, r%u+1); /* int between 1 and `u' */
203 break;
205 case 2: { /* lower and upper limits */
206 int l = luaL_checkint(L, 1);
207 int u = luaL_checkint(L, 2);
208 luaL_argcheck(L, l<=u, 2, "interval is empty");
209 lua_pushnumber(L, r%(u-l+1)+l); /* int between `l' and `u' */
210 break;
212 default: return luaL_error(L, "wrong number of arguments");
214 return 1;
218 static int math_randomseed (lua_State *L) {
219 rb->srand(luaL_checkint(L, 1));
220 return 0;
224 static const luaL_Reg mathlib[] = {
225 {"abs", math_abs},
226 #if 0
227 {"acos", math_acos},
228 {"asin", math_asin},
229 {"atan2", math_atan2},
230 {"atan", math_atan},
231 #endif
232 {"ceil", math_ceil},
233 #if 0
234 {"cosh", math_cosh},
235 {"cos", math_cos},
236 #endif
237 {"deg", math_deg},
238 #if 0
239 {"exp", math_exp},
240 #endif
241 {"floor", math_floor},
242 {"fmod", math_fmod},
243 #if 0
244 {"frexp", math_frexp},
245 {"ldexp", math_ldexp},
246 {"log10", math_log10},
247 {"log", math_log},
248 #endif
249 {"max", math_max},
250 {"min", math_min},
251 #if 0
252 {"modf", math_modf},
253 {"pow", math_pow},
254 #endif
255 {"rad", math_rad},
256 {"random", math_random},
257 {"randomseed", math_randomseed},
258 #if 0
259 {"sinh", math_sinh},
260 {"sin", math_sin},
261 {"sqrt", math_sqrt},
262 {"tanh", math_tanh},
263 {"tan", math_tan},
264 #endif
265 {NULL, NULL}
270 ** Open math library
272 LUALIB_API int luaopen_math (lua_State *L) {
273 luaL_register(L, LUA_MATHLIBNAME, mathlib);
274 #if 0 /* No use in adding floating point constants when there's no FP */
275 lua_pushnumber(L, PI);
276 lua_setfield(L, -2, "pi");
277 lua_pushnumber(L, HUGE_VAL);
278 lua_setfield(L, -2, "huge");
279 #if defined(LUA_COMPAT_MOD)
280 lua_getfield(L, -1, "fmod");
281 lua_setfield(L, -2, "mod");
282 #endif
283 #endif
284 return 1;