xref: /minix3/external/mit/lua/dist/src/lmathlib.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: lmathlib.c,v 1.4 2015/10/08 13:21:00 mbalmer Exp $	*/
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc ** Id: lmathlib.c,v 1.115 2015/03/12 14:04:04 roberto Exp
511be35a1SLionel Sambuc ** Standard mathematical library
611be35a1SLionel Sambuc ** See Copyright Notice in lua.h
711be35a1SLionel Sambuc */
811be35a1SLionel Sambuc 
9*0a6a1f1dSLionel Sambuc #define lmathlib_c
10*0a6a1f1dSLionel Sambuc #define LUA_LIB
11*0a6a1f1dSLionel Sambuc 
12*0a6a1f1dSLionel Sambuc #include "lprefix.h"
13*0a6a1f1dSLionel Sambuc 
1411be35a1SLionel Sambuc 
1511be35a1SLionel Sambuc #include <stdlib.h>
1611be35a1SLionel Sambuc #include <math.h>
1711be35a1SLionel Sambuc 
1811be35a1SLionel Sambuc #include "lua.h"
1911be35a1SLionel Sambuc 
2011be35a1SLionel Sambuc #include "lauxlib.h"
2111be35a1SLionel Sambuc #include "lualib.h"
2211be35a1SLionel Sambuc 
2311be35a1SLionel Sambuc 
2411be35a1SLionel Sambuc #undef PI
25*0a6a1f1dSLionel Sambuc #define PI	(l_mathop(3.141592653589793238462643383279502884))
2611be35a1SLionel Sambuc 
2711be35a1SLionel Sambuc 
28*0a6a1f1dSLionel Sambuc #if !defined(l_rand)		/* { */
29*0a6a1f1dSLionel Sambuc #if defined(LUA_USE_POSIX)
30*0a6a1f1dSLionel Sambuc #define l_rand()	random()
31*0a6a1f1dSLionel Sambuc #define l_srand(x)	srandom(x)
32*0a6a1f1dSLionel Sambuc #define L_RANDMAX	2147483647	/* (2^31 - 1), following POSIX */
33*0a6a1f1dSLionel Sambuc #else
34*0a6a1f1dSLionel Sambuc #define l_rand()	rand()
35*0a6a1f1dSLionel Sambuc #define l_srand(x)	srand(x)
36*0a6a1f1dSLionel Sambuc #define L_RANDMAX	RAND_MAX
37*0a6a1f1dSLionel Sambuc #endif
38*0a6a1f1dSLionel Sambuc #endif				/* } */
39*0a6a1f1dSLionel Sambuc 
4011be35a1SLionel Sambuc 
math_abs(lua_State * L)4111be35a1SLionel Sambuc static int math_abs (lua_State *L) {
42*0a6a1f1dSLionel Sambuc   if (lua_isinteger(L, 1)) {
43*0a6a1f1dSLionel Sambuc     lua_Integer n = lua_tointeger(L, 1);
44*0a6a1f1dSLionel Sambuc     if (n < 0) n = (lua_Integer)(0u - n);
45*0a6a1f1dSLionel Sambuc     lua_pushinteger(L, n);
46*0a6a1f1dSLionel Sambuc   }
47*0a6a1f1dSLionel Sambuc   else
48*0a6a1f1dSLionel Sambuc     lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
4911be35a1SLionel Sambuc   return 1;
5011be35a1SLionel Sambuc }
5111be35a1SLionel Sambuc 
math_sin(lua_State * L)5211be35a1SLionel Sambuc static int math_sin (lua_State *L) {
53*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
5411be35a1SLionel Sambuc   return 1;
5511be35a1SLionel Sambuc }
5611be35a1SLionel Sambuc 
math_cos(lua_State * L)5711be35a1SLionel Sambuc static int math_cos (lua_State *L) {
58*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
5911be35a1SLionel Sambuc   return 1;
6011be35a1SLionel Sambuc }
6111be35a1SLionel Sambuc 
math_tan(lua_State * L)6211be35a1SLionel Sambuc static int math_tan (lua_State *L) {
63*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
6411be35a1SLionel Sambuc   return 1;
6511be35a1SLionel Sambuc }
6611be35a1SLionel Sambuc 
math_asin(lua_State * L)6711be35a1SLionel Sambuc static int math_asin (lua_State *L) {
68*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
6911be35a1SLionel Sambuc   return 1;
7011be35a1SLionel Sambuc }
7111be35a1SLionel Sambuc 
math_acos(lua_State * L)7211be35a1SLionel Sambuc static int math_acos (lua_State *L) {
73*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
7411be35a1SLionel Sambuc   return 1;
7511be35a1SLionel Sambuc }
7611be35a1SLionel Sambuc 
math_atan(lua_State * L)7711be35a1SLionel Sambuc static int math_atan (lua_State *L) {
78*0a6a1f1dSLionel Sambuc   lua_Number y = luaL_checknumber(L, 1);
79*0a6a1f1dSLionel Sambuc   lua_Number x = luaL_optnumber(L, 2, 1);
80*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(atan2)(y, x));
8111be35a1SLionel Sambuc   return 1;
8211be35a1SLionel Sambuc }
8311be35a1SLionel Sambuc 
84*0a6a1f1dSLionel Sambuc 
math_toint(lua_State * L)85*0a6a1f1dSLionel Sambuc static int math_toint (lua_State *L) {
86*0a6a1f1dSLionel Sambuc   int valid;
87*0a6a1f1dSLionel Sambuc   lua_Integer n = lua_tointegerx(L, 1, &valid);
88*0a6a1f1dSLionel Sambuc   if (valid)
89*0a6a1f1dSLionel Sambuc     lua_pushinteger(L, n);
90*0a6a1f1dSLionel Sambuc   else {
91*0a6a1f1dSLionel Sambuc     luaL_checkany(L, 1);
92*0a6a1f1dSLionel Sambuc     lua_pushnil(L);  /* value is not convertible to integer */
93*0a6a1f1dSLionel Sambuc   }
9411be35a1SLionel Sambuc   return 1;
9511be35a1SLionel Sambuc }
9611be35a1SLionel Sambuc 
97*0a6a1f1dSLionel Sambuc 
pushnumint(lua_State * L,lua_Number d)98*0a6a1f1dSLionel Sambuc static void pushnumint (lua_State *L, lua_Number d) {
99*0a6a1f1dSLionel Sambuc   lua_Integer n;
100*0a6a1f1dSLionel Sambuc   if (lua_numbertointeger(d, &n))  /* does 'd' fit in an integer? */
101*0a6a1f1dSLionel Sambuc     lua_pushinteger(L, n);  /* result is integer */
102*0a6a1f1dSLionel Sambuc   else
103*0a6a1f1dSLionel Sambuc     lua_pushnumber(L, d);  /* result is float */
10411be35a1SLionel Sambuc }
10511be35a1SLionel Sambuc 
106*0a6a1f1dSLionel Sambuc 
math_floor(lua_State * L)10711be35a1SLionel Sambuc static int math_floor (lua_State *L) {
108*0a6a1f1dSLionel Sambuc   if (lua_isinteger(L, 1))
109*0a6a1f1dSLionel Sambuc     lua_settop(L, 1);  /* integer is its own floor */
110*0a6a1f1dSLionel Sambuc   else {
111*0a6a1f1dSLionel Sambuc     lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
112*0a6a1f1dSLionel Sambuc     pushnumint(L, d);
113*0a6a1f1dSLionel Sambuc   }
11411be35a1SLionel Sambuc   return 1;
11511be35a1SLionel Sambuc }
11611be35a1SLionel Sambuc 
117*0a6a1f1dSLionel Sambuc 
math_ceil(lua_State * L)118*0a6a1f1dSLionel Sambuc static int math_ceil (lua_State *L) {
119*0a6a1f1dSLionel Sambuc   if (lua_isinteger(L, 1))
120*0a6a1f1dSLionel Sambuc     lua_settop(L, 1);  /* integer is its own ceil */
121*0a6a1f1dSLionel Sambuc   else {
122*0a6a1f1dSLionel Sambuc     lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
123*0a6a1f1dSLionel Sambuc     pushnumint(L, d);
124*0a6a1f1dSLionel Sambuc   }
125*0a6a1f1dSLionel Sambuc   return 1;
126*0a6a1f1dSLionel Sambuc }
127*0a6a1f1dSLionel Sambuc 
128*0a6a1f1dSLionel Sambuc 
math_fmod(lua_State * L)12911be35a1SLionel Sambuc static int math_fmod (lua_State *L) {
130*0a6a1f1dSLionel Sambuc   if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
131*0a6a1f1dSLionel Sambuc     lua_Integer d = lua_tointeger(L, 2);
132*0a6a1f1dSLionel Sambuc     if ((lua_Unsigned)d + 1u <= 1u) {  /* special cases: -1 or 0 */
133*0a6a1f1dSLionel Sambuc       luaL_argcheck(L, d != 0, 2, "zero");
134*0a6a1f1dSLionel Sambuc       lua_pushinteger(L, 0);  /* avoid overflow with 0x80000... / -1 */
135*0a6a1f1dSLionel Sambuc     }
136*0a6a1f1dSLionel Sambuc     else
137*0a6a1f1dSLionel Sambuc       lua_pushinteger(L, lua_tointeger(L, 1) % d);
138*0a6a1f1dSLionel Sambuc   }
139*0a6a1f1dSLionel Sambuc   else
140*0a6a1f1dSLionel Sambuc     lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
141*0a6a1f1dSLionel Sambuc                                      luaL_checknumber(L, 2)));
14211be35a1SLionel Sambuc   return 1;
14311be35a1SLionel Sambuc }
14411be35a1SLionel Sambuc 
145*0a6a1f1dSLionel Sambuc 
146*0a6a1f1dSLionel Sambuc /*
147*0a6a1f1dSLionel Sambuc ** next function does not use 'modf', avoiding problems with 'double*'
148*0a6a1f1dSLionel Sambuc ** (which is not compatible with 'float*') when lua_Number is not
149*0a6a1f1dSLionel Sambuc ** 'double'.
150*0a6a1f1dSLionel Sambuc */
math_modf(lua_State * L)15111be35a1SLionel Sambuc static int math_modf (lua_State *L) {
152*0a6a1f1dSLionel Sambuc   if (lua_isinteger(L ,1)) {
153*0a6a1f1dSLionel Sambuc     lua_settop(L, 1);  /* number is its own integer part */
154*0a6a1f1dSLionel Sambuc     lua_pushnumber(L, 0);  /* no fractional part */
155*0a6a1f1dSLionel Sambuc   }
156*0a6a1f1dSLionel Sambuc   else {
157*0a6a1f1dSLionel Sambuc     lua_Number n = luaL_checknumber(L, 1);
158*0a6a1f1dSLionel Sambuc     /* integer part (rounds toward zero) */
159*0a6a1f1dSLionel Sambuc     lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
160*0a6a1f1dSLionel Sambuc     pushnumint(L, ip);
161*0a6a1f1dSLionel Sambuc     /* fractional part (test needed for inf/-inf) */
162*0a6a1f1dSLionel Sambuc     lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
163*0a6a1f1dSLionel Sambuc   }
16411be35a1SLionel Sambuc   return 2;
16511be35a1SLionel Sambuc }
16611be35a1SLionel Sambuc 
167*0a6a1f1dSLionel Sambuc 
math_sqrt(lua_State * L)16811be35a1SLionel Sambuc static int math_sqrt (lua_State *L) {
169*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
17011be35a1SLionel Sambuc   return 1;
17111be35a1SLionel Sambuc }
17211be35a1SLionel Sambuc 
173*0a6a1f1dSLionel Sambuc 
math_ult(lua_State * L)174*0a6a1f1dSLionel Sambuc static int math_ult (lua_State *L) {
175*0a6a1f1dSLionel Sambuc   lua_Integer a = luaL_checkinteger(L, 1);
176*0a6a1f1dSLionel Sambuc   lua_Integer b = luaL_checkinteger(L, 2);
177*0a6a1f1dSLionel Sambuc   lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
17811be35a1SLionel Sambuc   return 1;
17911be35a1SLionel Sambuc }
18011be35a1SLionel Sambuc 
math_log(lua_State * L)18111be35a1SLionel Sambuc static int math_log (lua_State *L) {
182*0a6a1f1dSLionel Sambuc   lua_Number x = luaL_checknumber(L, 1);
183*0a6a1f1dSLionel Sambuc   lua_Number res;
184*0a6a1f1dSLionel Sambuc   if (lua_isnoneornil(L, 2))
185*0a6a1f1dSLionel Sambuc     res = l_mathop(log)(x);
186*0a6a1f1dSLionel Sambuc   else {
187*0a6a1f1dSLionel Sambuc     lua_Number base = luaL_checknumber(L, 2);
188*0a6a1f1dSLionel Sambuc #if !defined(LUA_USE_C89)
189*0a6a1f1dSLionel Sambuc     if (base == 2.0) res = l_mathop(log2)(x); else
190*0a6a1f1dSLionel Sambuc #endif
191*0a6a1f1dSLionel Sambuc     if (base == 10.0) res = l_mathop(log10)(x);
192*0a6a1f1dSLionel Sambuc     else res = l_mathop(log)(x)/l_mathop(log)(base);
19311be35a1SLionel Sambuc   }
194*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, res);
19511be35a1SLionel Sambuc   return 1;
19611be35a1SLionel Sambuc }
19711be35a1SLionel Sambuc 
math_exp(lua_State * L)19811be35a1SLionel Sambuc static int math_exp (lua_State *L) {
199*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
20011be35a1SLionel Sambuc   return 1;
20111be35a1SLionel Sambuc }
20211be35a1SLionel Sambuc 
math_deg(lua_State * L)20311be35a1SLionel Sambuc static int math_deg (lua_State *L) {
204*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
20511be35a1SLionel Sambuc   return 1;
20611be35a1SLionel Sambuc }
20711be35a1SLionel Sambuc 
math_rad(lua_State * L)20811be35a1SLionel Sambuc static int math_rad (lua_State *L) {
209*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
21011be35a1SLionel Sambuc   return 1;
21111be35a1SLionel Sambuc }
21211be35a1SLionel Sambuc 
21311be35a1SLionel Sambuc 
math_min(lua_State * L)21411be35a1SLionel Sambuc static int math_min (lua_State *L) {
21511be35a1SLionel Sambuc   int n = lua_gettop(L);  /* number of arguments */
216*0a6a1f1dSLionel Sambuc   int imin = 1;  /* index of current minimum value */
21711be35a1SLionel Sambuc   int i;
218*0a6a1f1dSLionel Sambuc   luaL_argcheck(L, n >= 1, 1, "value expected");
21911be35a1SLionel Sambuc   for (i = 2; i <= n; i++) {
220*0a6a1f1dSLionel Sambuc     if (lua_compare(L, i, imin, LUA_OPLT))
221*0a6a1f1dSLionel Sambuc       imin = i;
22211be35a1SLionel Sambuc   }
223*0a6a1f1dSLionel Sambuc   lua_pushvalue(L, imin);
22411be35a1SLionel Sambuc   return 1;
22511be35a1SLionel Sambuc }
22611be35a1SLionel Sambuc 
22711be35a1SLionel Sambuc 
math_max(lua_State * L)22811be35a1SLionel Sambuc static int math_max (lua_State *L) {
22911be35a1SLionel Sambuc   int n = lua_gettop(L);  /* number of arguments */
230*0a6a1f1dSLionel Sambuc   int imax = 1;  /* index of current maximum value */
23111be35a1SLionel Sambuc   int i;
232*0a6a1f1dSLionel Sambuc   luaL_argcheck(L, n >= 1, 1, "value expected");
23311be35a1SLionel Sambuc   for (i = 2; i <= n; i++) {
234*0a6a1f1dSLionel Sambuc     if (lua_compare(L, imax, i, LUA_OPLT))
235*0a6a1f1dSLionel Sambuc       imax = i;
23611be35a1SLionel Sambuc   }
237*0a6a1f1dSLionel Sambuc   lua_pushvalue(L, imax);
23811be35a1SLionel Sambuc   return 1;
23911be35a1SLionel Sambuc }
24011be35a1SLionel Sambuc 
241*0a6a1f1dSLionel Sambuc /*
242*0a6a1f1dSLionel Sambuc ** This function uses 'double' (instead of 'lua_Number') to ensure that
243*0a6a1f1dSLionel Sambuc ** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0'
244*0a6a1f1dSLionel Sambuc ** will keep full precision (ensuring that 'r' is always less than 1.0.)
245*0a6a1f1dSLionel Sambuc */
math_random(lua_State * L)24611be35a1SLionel Sambuc static int math_random (lua_State *L) {
247*0a6a1f1dSLionel Sambuc   lua_Integer low, up;
248*0a6a1f1dSLionel Sambuc   double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0));
24911be35a1SLionel Sambuc   switch (lua_gettop(L)) {  /* check number of arguments */
25011be35a1SLionel Sambuc     case 0: {  /* no arguments */
251*0a6a1f1dSLionel Sambuc       lua_pushnumber(L, (lua_Number)r);  /* Number between 0 and 1 */
252*0a6a1f1dSLionel Sambuc       return 1;
25311be35a1SLionel Sambuc     }
25411be35a1SLionel Sambuc     case 1: {  /* only upper limit */
255*0a6a1f1dSLionel Sambuc       low = 1;
256*0a6a1f1dSLionel Sambuc       up = luaL_checkinteger(L, 1);
25711be35a1SLionel Sambuc       break;
25811be35a1SLionel Sambuc     }
25911be35a1SLionel Sambuc     case 2: {  /* lower and upper limits */
260*0a6a1f1dSLionel Sambuc       low = luaL_checkinteger(L, 1);
261*0a6a1f1dSLionel Sambuc       up = luaL_checkinteger(L, 2);
26211be35a1SLionel Sambuc       break;
26311be35a1SLionel Sambuc     }
26411be35a1SLionel Sambuc     default: return luaL_error(L, "wrong number of arguments");
26511be35a1SLionel Sambuc   }
266*0a6a1f1dSLionel Sambuc   /* random integer in the interval [low, up] */
267*0a6a1f1dSLionel Sambuc   luaL_argcheck(L, low <= up, 1, "interval is empty");
268*0a6a1f1dSLionel Sambuc   luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1,
269*0a6a1f1dSLionel Sambuc                    "interval too large");
270*0a6a1f1dSLionel Sambuc   r *= (double)(up - low) + 1.0;
271*0a6a1f1dSLionel Sambuc   lua_pushinteger(L, (lua_Integer)r + low);
27211be35a1SLionel Sambuc   return 1;
27311be35a1SLionel Sambuc }
27411be35a1SLionel Sambuc 
27511be35a1SLionel Sambuc 
math_randomseed(lua_State * L)27611be35a1SLionel Sambuc static int math_randomseed (lua_State *L) {
277*0a6a1f1dSLionel Sambuc   l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1));
278*0a6a1f1dSLionel Sambuc   (void)rand(); /* discard first value to avoid undesirable correlations */
27911be35a1SLionel Sambuc   return 0;
28011be35a1SLionel Sambuc }
28111be35a1SLionel Sambuc 
28211be35a1SLionel Sambuc 
math_type(lua_State * L)283*0a6a1f1dSLionel Sambuc static int math_type (lua_State *L) {
284*0a6a1f1dSLionel Sambuc   if (lua_type(L, 1) == LUA_TNUMBER) {
285*0a6a1f1dSLionel Sambuc       if (lua_isinteger(L, 1))
286*0a6a1f1dSLionel Sambuc         lua_pushliteral(L, "integer");
287*0a6a1f1dSLionel Sambuc       else
288*0a6a1f1dSLionel Sambuc         lua_pushliteral(L, "float");
289*0a6a1f1dSLionel Sambuc   }
290*0a6a1f1dSLionel Sambuc   else {
291*0a6a1f1dSLionel Sambuc     luaL_checkany(L, 1);
292*0a6a1f1dSLionel Sambuc     lua_pushnil(L);
293*0a6a1f1dSLionel Sambuc   }
294*0a6a1f1dSLionel Sambuc   return 1;
295*0a6a1f1dSLionel Sambuc }
296*0a6a1f1dSLionel Sambuc 
297*0a6a1f1dSLionel Sambuc 
298*0a6a1f1dSLionel Sambuc /*
299*0a6a1f1dSLionel Sambuc ** {==================================================================
300*0a6a1f1dSLionel Sambuc ** Deprecated functions (for compatibility only)
301*0a6a1f1dSLionel Sambuc ** ===================================================================
302*0a6a1f1dSLionel Sambuc */
303*0a6a1f1dSLionel Sambuc #if defined(LUA_COMPAT_MATHLIB)
304*0a6a1f1dSLionel Sambuc 
math_cosh(lua_State * L)305*0a6a1f1dSLionel Sambuc static int math_cosh (lua_State *L) {
306*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
307*0a6a1f1dSLionel Sambuc   return 1;
308*0a6a1f1dSLionel Sambuc }
309*0a6a1f1dSLionel Sambuc 
math_sinh(lua_State * L)310*0a6a1f1dSLionel Sambuc static int math_sinh (lua_State *L) {
311*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
312*0a6a1f1dSLionel Sambuc   return 1;
313*0a6a1f1dSLionel Sambuc }
314*0a6a1f1dSLionel Sambuc 
math_tanh(lua_State * L)315*0a6a1f1dSLionel Sambuc static int math_tanh (lua_State *L) {
316*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
317*0a6a1f1dSLionel Sambuc   return 1;
318*0a6a1f1dSLionel Sambuc }
319*0a6a1f1dSLionel Sambuc 
math_pow(lua_State * L)320*0a6a1f1dSLionel Sambuc static int math_pow (lua_State *L) {
321*0a6a1f1dSLionel Sambuc   lua_Number x = luaL_checknumber(L, 1);
322*0a6a1f1dSLionel Sambuc   lua_Number y = luaL_checknumber(L, 2);
323*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(pow)(x, y));
324*0a6a1f1dSLionel Sambuc   return 1;
325*0a6a1f1dSLionel Sambuc }
326*0a6a1f1dSLionel Sambuc 
math_frexp(lua_State * L)327*0a6a1f1dSLionel Sambuc static int math_frexp (lua_State *L) {
328*0a6a1f1dSLionel Sambuc   int e;
329*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
330*0a6a1f1dSLionel Sambuc   lua_pushinteger(L, e);
331*0a6a1f1dSLionel Sambuc   return 2;
332*0a6a1f1dSLionel Sambuc }
333*0a6a1f1dSLionel Sambuc 
math_ldexp(lua_State * L)334*0a6a1f1dSLionel Sambuc static int math_ldexp (lua_State *L) {
335*0a6a1f1dSLionel Sambuc   lua_Number x = luaL_checknumber(L, 1);
336*0a6a1f1dSLionel Sambuc   int ep = (int)luaL_checkinteger(L, 2);
337*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(ldexp)(x, ep));
338*0a6a1f1dSLionel Sambuc   return 1;
339*0a6a1f1dSLionel Sambuc }
340*0a6a1f1dSLionel Sambuc 
math_log10(lua_State * L)341*0a6a1f1dSLionel Sambuc static int math_log10 (lua_State *L) {
342*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
343*0a6a1f1dSLionel Sambuc   return 1;
344*0a6a1f1dSLionel Sambuc }
345*0a6a1f1dSLionel Sambuc 
346*0a6a1f1dSLionel Sambuc #endif
347*0a6a1f1dSLionel Sambuc /* }================================================================== */
348*0a6a1f1dSLionel Sambuc 
349*0a6a1f1dSLionel Sambuc 
350*0a6a1f1dSLionel Sambuc 
35111be35a1SLionel Sambuc static const luaL_Reg mathlib[] = {
35211be35a1SLionel Sambuc   {"abs",   math_abs},
35311be35a1SLionel Sambuc   {"acos",  math_acos},
35411be35a1SLionel Sambuc   {"asin",  math_asin},
35511be35a1SLionel Sambuc   {"atan",  math_atan},
35611be35a1SLionel Sambuc   {"ceil",  math_ceil},
35711be35a1SLionel Sambuc   {"cos",   math_cos},
35811be35a1SLionel Sambuc   {"deg",   math_deg},
35911be35a1SLionel Sambuc   {"exp",   math_exp},
360*0a6a1f1dSLionel Sambuc   {"tointeger", math_toint},
36111be35a1SLionel Sambuc   {"floor", math_floor},
36211be35a1SLionel Sambuc   {"fmod",   math_fmod},
363*0a6a1f1dSLionel Sambuc   {"ult",   math_ult},
36411be35a1SLionel Sambuc   {"log",   math_log},
36511be35a1SLionel Sambuc   {"max",   math_max},
36611be35a1SLionel Sambuc   {"min",   math_min},
36711be35a1SLionel Sambuc   {"modf",   math_modf},
36811be35a1SLionel Sambuc   {"rad",   math_rad},
36911be35a1SLionel Sambuc   {"random",     math_random},
37011be35a1SLionel Sambuc   {"randomseed", math_randomseed},
37111be35a1SLionel Sambuc   {"sin",   math_sin},
37211be35a1SLionel Sambuc   {"sqrt",  math_sqrt},
37311be35a1SLionel Sambuc   {"tan",   math_tan},
374*0a6a1f1dSLionel Sambuc   {"type", math_type},
375*0a6a1f1dSLionel Sambuc #if defined(LUA_COMPAT_MATHLIB)
376*0a6a1f1dSLionel Sambuc   {"atan2", math_atan},
377*0a6a1f1dSLionel Sambuc   {"cosh",   math_cosh},
378*0a6a1f1dSLionel Sambuc   {"sinh",   math_sinh},
379*0a6a1f1dSLionel Sambuc   {"tanh",   math_tanh},
380*0a6a1f1dSLionel Sambuc   {"pow",   math_pow},
381*0a6a1f1dSLionel Sambuc   {"frexp", math_frexp},
382*0a6a1f1dSLionel Sambuc   {"ldexp", math_ldexp},
383*0a6a1f1dSLionel Sambuc   {"log10", math_log10},
384*0a6a1f1dSLionel Sambuc #endif
385*0a6a1f1dSLionel Sambuc   /* placeholders */
386*0a6a1f1dSLionel Sambuc   {"pi", NULL},
387*0a6a1f1dSLionel Sambuc   {"huge", NULL},
388*0a6a1f1dSLionel Sambuc   {"maxinteger", NULL},
389*0a6a1f1dSLionel Sambuc   {"mininteger", NULL},
39011be35a1SLionel Sambuc   {NULL, NULL}
39111be35a1SLionel Sambuc };
39211be35a1SLionel Sambuc 
39311be35a1SLionel Sambuc 
39411be35a1SLionel Sambuc /*
39511be35a1SLionel Sambuc ** Open math library
39611be35a1SLionel Sambuc */
luaopen_math(lua_State * L)397*0a6a1f1dSLionel Sambuc LUAMOD_API int luaopen_math (lua_State *L) {
398*0a6a1f1dSLionel Sambuc   luaL_newlib(L, mathlib);
39911be35a1SLionel Sambuc   lua_pushnumber(L, PI);
40011be35a1SLionel Sambuc   lua_setfield(L, -2, "pi");
401*0a6a1f1dSLionel Sambuc   lua_pushnumber(L, (lua_Number)HUGE_VAL);
40211be35a1SLionel Sambuc   lua_setfield(L, -2, "huge");
403*0a6a1f1dSLionel Sambuc   lua_pushinteger(L, LUA_MAXINTEGER);
404*0a6a1f1dSLionel Sambuc   lua_setfield(L, -2, "maxinteger");
405*0a6a1f1dSLionel Sambuc   lua_pushinteger(L, LUA_MININTEGER);
406*0a6a1f1dSLionel Sambuc   lua_setfield(L, -2, "mininteger");
40711be35a1SLionel Sambuc   return 1;
40811be35a1SLionel Sambuc }
40911be35a1SLionel Sambuc 
410