]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/lua/lib/contrib/src/lmathlib.c
update
[l4.git] / l4 / pkg / lua / lib / contrib / src / lmathlib.c
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 */
6
7 #include <stdlib.h>
8 #include <math.h>
9
10 #define lmathlib_c
11 #define LUA_LIB
12
13 #include "lua.h"
14
15 #include "lauxlib.h"
16 #include "lualib.h"
17
18 /* 'luai_vectpow()' as a replacement for 'cpow()'. Defined in the header; we
19  * don't intrude the code libs internal functions.
20  */
21 #ifdef LNUM_COMPLEX
22 # include "lnum.h"    
23 #endif
24
25 #undef PI
26 #ifdef LNUM_FLOAT
27 # define PI (3.14159265358979323846F)
28 #elif defined(M_PI)
29 # define PI M_PI
30 #else
31 # define PI (3.14159265358979323846264338327950288)
32 #endif
33 #define RADIANS_PER_DEGREE (PI/180)
34
35 #undef HUGE
36 #ifdef LNUM_FLOAT
37 # define HUGE HUGE_VALF
38 #elif defined(LNUM_LDOUBLE)
39 # define HUGE HUGE_VALL
40 #else
41 # define HUGE HUGE_VAL
42 #endif
43
44 static int math_abs (lua_State *L) {
45 #ifdef LNUM_COMPLEX
46   lua_pushnumber(L, _LF(cabs) (luaL_checkcomplex(L,1)));
47 #else
48   lua_pushnumber(L, _LF(fabs) (luaL_checknumber(L, 1)));
49 #endif
50   return 1;
51 }
52
53 static int math_sin (lua_State *L) {
54 #ifdef LNUM_COMPLEX
55   lua_pushcomplex(L, _LF(csin) (luaL_checkcomplex(L,1)));
56 #else
57   lua_pushnumber(L, _LF(sin) (luaL_checknumber(L, 1)));
58 #endif
59   return 1;
60 }
61
62 static int math_sinh (lua_State *L) {
63 #ifdef LNUM_COMPLEX
64   lua_pushcomplex(L, _LF(csinh) (luaL_checkcomplex(L,1)));
65 #else
66   lua_pushnumber(L, _LF(sinh) (luaL_checknumber(L, 1)));
67 #endif
68   return 1;
69 }
70
71 static int math_cos (lua_State *L) {
72 #ifdef LNUM_COMPLEX
73   lua_pushcomplex(L, _LF(ccos) (luaL_checkcomplex(L,1)));
74 #else
75   lua_pushnumber(L, _LF(cos) (luaL_checknumber(L, 1)));
76 #endif
77   return 1;
78 }
79
80 static int math_cosh (lua_State *L) {
81 #ifdef LNUM_COMPLEX
82   lua_pushcomplex(L, _LF(ccosh) (luaL_checkcomplex(L,1)));
83 #else
84   lua_pushnumber(L, _LF(cosh) (luaL_checknumber(L, 1)));
85 #endif
86   return 1;
87 }
88
89 static int math_tan (lua_State *L) {
90 #ifdef LNUM_COMPLEX
91   lua_pushcomplex(L, _LF(ctan) (luaL_checkcomplex(L,1)));
92 #else
93   lua_pushnumber(L, _LF(tan) (luaL_checknumber(L, 1)));
94 #endif
95   return 1;
96 }
97
98 static int math_tanh (lua_State *L) {
99 #ifdef LNUM_COMPLEX
100   lua_pushcomplex(L, _LF(ctanh) (luaL_checkcomplex(L,1)));
101 #else
102   lua_pushnumber(L, _LF(tanh) (luaL_checknumber(L, 1)));
103 #endif
104   return 1;
105 }
106
107 static int math_asin (lua_State *L) {
108 #ifdef LNUM_COMPLEX
109   lua_pushcomplex(L, _LF(casin) (luaL_checkcomplex(L,1)));
110 #else
111   lua_pushnumber(L, _LF(asin) (luaL_checknumber(L, 1)));
112 #endif
113   return 1;
114 }
115
116 static int math_acos (lua_State *L) {
117 #ifdef LNUM_COMPLEX
118   lua_pushcomplex(L, _LF(cacos) (luaL_checkcomplex(L,1)));
119 #else
120   lua_pushnumber(L, _LF(acos) (luaL_checknumber(L, 1)));
121 #endif
122   return 1;
123 }
124
125 static int math_atan (lua_State *L) {
126 #ifdef LNUM_COMPLEX
127   lua_pushcomplex(L, _LF(catan) (luaL_checkcomplex(L,1)));
128 #else
129   lua_pushnumber(L, _LF(atan) (luaL_checknumber(L, 1)));
130 #endif
131   return 1;
132 }
133
134 static int math_atan2 (lua_State *L) {
135   /* scalars only */
136   lua_pushnumber(L, _LF(atan2) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
137   return 1;
138 }
139
140 static int math_ceil (lua_State *L) {
141 #ifdef LNUM_COMPLEX
142   lua_Complex v= luaL_checkcomplex(L, 1);
143   lua_pushcomplex(L, _LF(ceil) (_LF(creal)(v)) + _LF(ceil) (_LF(cimag)(v))*I);
144 #else
145   lua_pushnumber(L, _LF(ceil) (luaL_checknumber(L, 1)));
146 #endif
147   return 1;
148 }
149
150 static int math_floor (lua_State *L) {
151 #ifdef LNUM_COMPLEX
152   lua_Complex v= luaL_checkcomplex(L, 1);
153   lua_pushcomplex(L, _LF(floor) (_LF(creal)(v)) + _LF(floor) (_LF(cimag)(v))*I);
154 #else
155   lua_pushnumber(L, _LF(floor) (luaL_checknumber(L, 1)));
156 #endif
157   return 1;
158 }
159
160 static int math_fmod (lua_State *L) {  
161   /* scalars only */
162   lua_pushnumber(L, _LF(fmod) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
163   return 1;
164 }
165
166 static int math_modf (lua_State *L) {
167   /* scalars only */
168   lua_Number ip;
169   lua_Number fp = _LF(modf) (luaL_checknumber(L, 1), &ip);
170   lua_pushnumber(L, ip);
171   lua_pushnumber(L, fp);
172   return 2;
173 }
174
175 static int math_sqrt (lua_State *L) {
176 #ifdef LNUM_COMPLEX
177   lua_pushcomplex(L, _LF(csqrt) (luaL_checkcomplex(L,1)));
178 #else
179   lua_pushnumber(L, _LF(sqrt) (luaL_checknumber(L, 1)));
180 #endif
181   return 1;
182 }
183
184 static int math_pow (lua_State *L) {
185 #ifdef LNUM_COMPLEX
186   /* C99 'cpow' gives somewhat inaccurate results (i.e. (-1)^2 = -1+1.2246467991474e-16i). 
187   * 'luai_vectpow' smoothens such, reusing it is the reason we need to #include "lnum.h".
188   */
189   lua_pushcomplex(L, luai_vectpow(luaL_checkcomplex(L,1), luaL_checkcomplex(L,2)));
190 #else
191   lua_pushnumber(L, _LF(pow) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
192 #endif
193   return 1;
194 }
195
196 static int math_log (lua_State *L) {
197 #ifdef LNUM_COMPLEX
198   lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)));
199 #else
200   lua_pushnumber(L, _LF(log) (luaL_checknumber(L, 1)));
201 #endif
202   return 1;
203 }
204
205 static int math_log10 (lua_State *L) {
206 #ifdef LNUM_COMPLEX
207   /* Not in standard <complex.h> , but easy to calculate: log_a(x) = log_b(x) / log_b(a) 
208   */
209   lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)) / _LF(log) (10));
210 #else
211   lua_pushnumber(L, _LF(log10) (luaL_checknumber(L, 1)));
212 #endif
213   return 1;
214 }
215
216 static int math_exp (lua_State *L) {
217 #ifdef LNUM_COMPLEX
218   lua_pushcomplex(L, _LF(cexp) (luaL_checkcomplex(L,1)));
219 #else
220   lua_pushnumber(L, _LF(exp) (luaL_checknumber(L, 1)));
221 #endif
222   return 1;
223 }
224
225 static int math_deg (lua_State *L) {
226   lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
227   return 1;
228 }
229
230 static int math_rad (lua_State *L) {
231   lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
232   return 1;
233 }
234
235 static int math_frexp (lua_State *L) {
236   int e;
237   lua_pushnumber(L, _LF(frexp) (luaL_checknumber(L, 1), &e));
238   lua_pushinteger(L, e);
239   return 2;
240 }
241
242 static int math_ldexp (lua_State *L) {
243   lua_pushnumber(L, _LF(ldexp) (luaL_checknumber(L, 1), luaL_checkint(L, 2)));
244   return 1;
245 }
246
247
248
249 static int math_min (lua_State *L) {
250   /* scalars only */
251   int n = lua_gettop(L);  /* number of arguments */
252   lua_Number dmin = luaL_checknumber(L, 1);
253   int i;
254   for (i=2; i<=n; i++) {
255     lua_Number d = luaL_checknumber(L, i);
256     if (d < dmin)
257       dmin = d;
258   }
259   lua_pushnumber(L, dmin);
260   return 1;
261 }
262
263
264 static int math_max (lua_State *L) {
265   /* scalars only */
266   int n = lua_gettop(L);  /* number of arguments */
267   lua_Number dmax = luaL_checknumber(L, 1);
268   int i;
269   for (i=2; i<=n; i++) {
270     lua_Number d = luaL_checknumber(L, i);
271     if (d > dmax)
272       dmax = d;
273   }
274   lua_pushnumber(L, dmax);
275   return 1;
276 }
277
278
279 static int math_random (lua_State *L) {
280   /* the `%' avoids the (rare) case of r==1, and is needed also because on
281      some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
282   lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
283   int n= lua_gettop(L);  /* number of arguments */
284   if (n==0) {   /* no arguments: range [0,1) */
285     lua_pushnumber(L, r);
286   } else if (n<=2) {    /* int range [1,u] or [l,u] */
287     int l= n==1 ? 1 : luaL_checkint(L, 1);
288     int u = luaL_checkint(L, n);
289     int tmp;
290     lua_Number d;
291     luaL_argcheck(L, l<=u, n, "interval is empty");
292     d= _LF(floor)(r*(u-l+1));
293     lua_number2int(tmp,d);
294     lua_pushinteger(L, l+tmp);
295   } else {
296     return luaL_error(L, "wrong number of arguments");
297   }
298   return 1;
299 }
300
301
302 static int math_randomseed (lua_State *L) {
303   srand(luaL_checkint(L, 1));
304   return 0;
305 }
306
307 /* 
308 * Lua 5.1 does not have acosh, asinh, atanh for scalars (not ANSI C)
309 */
310 #if __STDC_VERSION__ >= 199901L
311 static int math_acosh (lua_State *L) {
312 # ifdef LNUM_COMPLEX
313   lua_pushcomplex(L, _LF(cacosh) (luaL_checkcomplex(L,1)));
314 # else
315   lua_pushnumber(L, _LF(acosh) (luaL_checknumber(L,1)));
316 # endif
317   return 1;
318 }
319 static int math_asinh (lua_State *L) {
320 # ifdef LNUM_COMPLEX
321   lua_pushcomplex(L, _LF(casinh) (luaL_checkcomplex(L,1)));
322 # else
323   lua_pushnumber(L, _LF(asinh) (luaL_checknumber(L,1)));
324 # endif
325   return 1;
326 }
327 static int math_atanh (lua_State *L) {
328 # ifdef LNUM_COMPLEX
329   lua_pushcomplex(L, _LF(catanh) (luaL_checkcomplex(L,1)));
330 # else
331   lua_pushnumber(L, _LF(atanh) (luaL_checknumber(L,1)));
332 # endif
333   return 1;
334 }
335 #endif
336
337 /* 
338  * C99 complex functions, not covered above.
339 */
340 #ifdef LNUM_COMPLEX
341 static int math_arg (lua_State *L) {
342   lua_pushnumber(L, _LF(carg) (luaL_checkcomplex(L,1)));
343   return 1;
344 }
345
346 static int math_imag (lua_State *L) {
347   lua_pushnumber(L, _LF(cimag) (luaL_checkcomplex(L,1)));
348   return 1;
349 }
350
351 static int math_real (lua_State *L) {
352   lua_pushnumber(L, _LF(creal) (luaL_checkcomplex(L,1)));
353   return 1;
354 }
355
356 static int math_conj (lua_State *L) {
357   lua_pushcomplex(L, _LF(conj) (luaL_checkcomplex(L,1)));
358   return 1;
359 }
360
361 static int math_proj (lua_State *L) {
362   lua_pushcomplex(L, _LF(cproj) (luaL_checkcomplex(L,1)));
363   return 1;
364 }
365 #endif
366
367
368 static const luaL_Reg mathlib[] = {
369   {"abs",   math_abs},
370   {"acos",  math_acos},
371   {"asin",  math_asin},
372   {"atan2", math_atan2},
373   {"atan",  math_atan},
374   {"ceil",  math_ceil},
375   {"cosh",   math_cosh},
376   {"cos",   math_cos},
377   {"deg",   math_deg},
378   {"exp",   math_exp},
379   {"floor", math_floor},
380   {"fmod",   math_fmod},
381   {"frexp", math_frexp},
382   {"ldexp", math_ldexp},
383   {"log10", math_log10},
384   {"log",   math_log},
385   {"max",   math_max},
386   {"min",   math_min},
387   {"modf",   math_modf},
388   {"pow",   math_pow},
389   {"rad",   math_rad},
390   {"random",     math_random},
391   {"randomseed", math_randomseed},
392   {"sinh",   math_sinh},
393   {"sin",   math_sin},
394   {"sqrt",  math_sqrt},
395   {"tanh",   math_tanh},
396   {"tan",   math_tan},
397 #if __STDC_VERSION__ >= 199901L
398   {"acosh",  math_acosh},
399   {"asinh",  math_asinh},
400   {"atanh",  math_atanh},
401 #endif
402 #ifdef LNUM_COMPLEX
403   {"arg",   math_arg},
404   {"imag",  math_imag},
405   {"real",  math_real},
406   {"conj",  math_conj},
407   {"proj",  math_proj},
408 #endif
409   {NULL, NULL}
410 };
411
412
413 /*
414 ** Open math library
415 */
416 LUALIB_API int luaopen_math (lua_State *L) {
417   luaL_register(L, LUA_MATHLIBNAME, mathlib);
418   lua_pushnumber(L, PI);
419   lua_setfield(L, -2, "pi");
420   lua_pushnumber(L, HUGE);
421   lua_setfield(L, -2, "huge");
422   lua_pushinteger(L, LUA_INTEGER_MAX );
423   lua_setfield(L, -2, "hugeint");
424 #if defined(LUA_COMPAT_MOD)
425   lua_getfield(L, -1, "fmod");
426   lua_setfield(L, -2, "mod");
427 #endif
428   return 1;
429 }
430