3636#endif /* } */
3737
3838
39- static int math_abs (lua_State * L ) {
40- if (lua_isinteger (L , 1 )) {
41- lua_Integer n = lua_tointeger (L , 1 );
42- if (n < 0 ) n = (lua_Integer )(0u - (lua_Unsigned )n );
43- lua_pushinteger (L , n );
44- }
45- else
46- lua_pushnumber (L , l_mathop (fabs )(luaL_checknumber (L , 1 )));
47- return 1 ;
48- }
49-
50- static int math_sin (lua_State * L ) {
51- lua_pushnumber (L , l_mathop (sin )(luaL_checknumber (L , 1 )));
52- return 1 ;
53- }
54-
55- static int math_cos (lua_State * L ) {
56- lua_pushnumber (L , l_mathop (cos )(luaL_checknumber (L , 1 )));
57- return 1 ;
58- }
59-
60- static int math_tan (lua_State * L ) {
61- lua_pushnumber (L , l_mathop (tan )(luaL_checknumber (L , 1 )));
62- return 1 ;
63- }
64-
65- static int math_asin (lua_State * L ) {
66- lua_pushnumber (L , l_mathop (asin )(luaL_checknumber (L , 1 )));
67- return 1 ;
68- }
69-
70- static int math_acos (lua_State * L ) {
71- lua_pushnumber (L , l_mathop (acos )(luaL_checknumber (L , 1 )));
72- return 1 ;
73- }
74-
75- static int math_atan (lua_State * L ) {
76- lua_Number y = luaL_checknumber (L , 1 );
77- lua_Number x = luaL_optnumber (L , 2 , 1 );
78- lua_pushnumber (L , l_mathop (atan2 )(y , x ));
79- return 1 ;
80- }
81-
82-
83- static int math_toint (lua_State * L ) {
84- int valid ;
85- lua_Integer n = lua_tointegerx (L , 1 , & valid );
86- if (valid )
87- lua_pushinteger (L , n );
88- else {
89- luaL_checkany (L , 1 );
90- lua_pushnil (L ); /* value is not convertible to integer */
91- }
92- return 1 ;
93- }
94-
39+ extern int math_abs (lua_State * L );
40+ extern int math_sin (lua_State * L );
41+ extern int math_cos (lua_State * L );
42+ extern int math_tan (lua_State * L );
43+ extern int math_asin (lua_State * L );
44+ extern int math_acos (lua_State * L );
45+ extern int math_atan (lua_State * L );
46+ extern int math_toint (lua_State * L );
47+ extern int math_sqrt (lua_State * L );
48+ extern int math_ult (lua_State * L );
49+ extern int math_log (lua_State * L );
50+ extern int math_exp (lua_State * L );
51+ extern int math_deg (lua_State * L );
52+ extern int math_rad (lua_State * L );
53+ extern int math_min (lua_State * L );
54+ extern int math_max (lua_State * L );
55+ extern int math_randomseed (lua_State * L );
9556
9657static void pushnumint (lua_State * L , lua_Number d ) {
9758 lua_Integer n ;
@@ -101,8 +62,23 @@ static void pushnumint (lua_State *L, lua_Number d) {
10162 lua_pushnumber (L , d ); /* result is float */
10263}
10364
65+ extern int math_fmod (lua_State * L ) {
66+ if (lua_isinteger (L , 1 ) && lua_isinteger (L , 2 )) {
67+ lua_Integer d = lua_tointeger (L , 2 );
68+ if ((lua_Unsigned )d + 1u <= 1u ) { /* special cases: -1 or 0 */
69+ luaL_argcheck (L , d != 0 , 2 , "zero" );
70+ lua_pushinteger (L , 0 ); /* avoid overflow with 0x80000... / -1 */
71+ }
72+ else
73+ lua_pushinteger (L , lua_tointeger (L , 1 ) % d );
74+ }
75+ else
76+ lua_pushnumber (L , l_mathop (fmod )(luaL_checknumber (L , 1 ),
77+ luaL_checknumber (L , 2 )));
78+ return 1 ;
79+ }
10480
105- static int math_floor (lua_State * L ) {
81+ extern int math_floor (lua_State * L ) {
10682 if (lua_isinteger (L , 1 ))
10783 lua_settop (L , 1 ); /* integer is its own floor */
10884 else {
@@ -113,7 +89,7 @@ static int math_floor (lua_State *L) {
11389}
11490
11591
116- static int math_ceil (lua_State * L ) {
92+ extern int math_ceil (lua_State * L ) {
11793 if (lua_isinteger (L , 1 ))
11894 lua_settop (L , 1 ); /* integer is its own ceil */
11995 else {
@@ -124,29 +100,12 @@ static int math_ceil (lua_State *L) {
124100}
125101
126102
127- static int math_fmod (lua_State * L ) {
128- if (lua_isinteger (L , 1 ) && lua_isinteger (L , 2 )) {
129- lua_Integer d = lua_tointeger (L , 2 );
130- if ((lua_Unsigned )d + 1u <= 1u ) { /* special cases: -1 or 0 */
131- luaL_argcheck (L , d != 0 , 2 , "zero" );
132- lua_pushinteger (L , 0 ); /* avoid overflow with 0x80000... / -1 */
133- }
134- else
135- lua_pushinteger (L , lua_tointeger (L , 1 ) % d );
136- }
137- else
138- lua_pushnumber (L , l_mathop (fmod )(luaL_checknumber (L , 1 ),
139- luaL_checknumber (L , 2 )));
140- return 1 ;
141- }
142-
143-
144103/*
145104** next function does not use 'modf', avoiding problems with 'double*'
146105** (which is not compatible with 'float*') when lua_Number is not
147106** 'double'.
148107*/
149- static int math_modf (lua_State * L ) {
108+ extern int math_modf (lua_State * L ) {
150109 if (lua_isinteger (L ,1 )) {
151110 lua_settop (L , 1 ); /* number is its own integer part */
152111 lua_pushnumber (L , 0 ); /* no fractional part */
@@ -162,89 +121,12 @@ static int math_modf (lua_State *L) {
162121 return 2 ;
163122}
164123
165-
166- static int math_sqrt (lua_State * L ) {
167- lua_pushnumber (L , l_mathop (sqrt )(luaL_checknumber (L , 1 )));
168- return 1 ;
169- }
170-
171-
172- static int math_ult (lua_State * L ) {
173- lua_Integer a = luaL_checkinteger (L , 1 );
174- lua_Integer b = luaL_checkinteger (L , 2 );
175- lua_pushboolean (L , (lua_Unsigned )a < (lua_Unsigned )b );
176- return 1 ;
177- }
178-
179- static int math_log (lua_State * L ) {
180- lua_Number x = luaL_checknumber (L , 1 );
181- lua_Number res ;
182- if (lua_isnoneornil (L , 2 ))
183- res = l_mathop (log )(x );
184- else {
185- lua_Number base = luaL_checknumber (L , 2 );
186- #if !defined(LUA_USE_C89 )
187- if (base == l_mathop (2.0 ))
188- res = l_mathop (log2 )(x ); else
189- #endif
190- if (base == l_mathop (10.0 ))
191- res = l_mathop (log10 )(x );
192- else
193- res = l_mathop (log )(x )/l_mathop (log )(base );
194- }
195- lua_pushnumber (L , res );
196- return 1 ;
197- }
198-
199- static int math_exp (lua_State * L ) {
200- lua_pushnumber (L , l_mathop (exp )(luaL_checknumber (L , 1 )));
201- return 1 ;
202- }
203-
204- static int math_deg (lua_State * L ) {
205- lua_pushnumber (L , luaL_checknumber (L , 1 ) * (l_mathop (180.0 ) / PI ));
206- return 1 ;
207- }
208-
209- static int math_rad (lua_State * L ) {
210- lua_pushnumber (L , luaL_checknumber (L , 1 ) * (PI / l_mathop (180.0 )));
211- return 1 ;
212- }
213-
214-
215- static int math_min (lua_State * L ) {
216- int n = lua_gettop (L ); /* number of arguments */
217- int imin = 1 ; /* index of current minimum value */
218- int i ;
219- luaL_argcheck (L , n >= 1 , 1 , "value expected" );
220- for (i = 2 ; i <= n ; i ++ ) {
221- if (lua_compare (L , i , imin , LUA_OPLT ))
222- imin = i ;
223- }
224- lua_pushvalue (L , imin );
225- return 1 ;
226- }
227-
228-
229- static int math_max (lua_State * L ) {
230- int n = lua_gettop (L ); /* number of arguments */
231- int imax = 1 ; /* index of current maximum value */
232- int i ;
233- luaL_argcheck (L , n >= 1 , 1 , "value expected" );
234- for (i = 2 ; i <= n ; i ++ ) {
235- if (lua_compare (L , imax , i , LUA_OPLT ))
236- imax = i ;
237- }
238- lua_pushvalue (L , imax );
239- return 1 ;
240- }
241-
242124/*
243125** This function uses 'double' (instead of 'lua_Number') to ensure that
244126** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0'
245127** will keep full precision (ensuring that 'r' is always less than 1.0.)
246128*/
247- static int math_random (lua_State * L ) {
129+ extern int math_random (lua_State * L ) {
248130 lua_Integer low , up ;
249131 double r = (double )l_rand () * (1.0 / ((double )L_RANDMAX + 1.0 ));
250132 switch (lua_gettop (L )) { /* check number of arguments */
@@ -273,15 +155,7 @@ static int math_random (lua_State *L) {
273155 return 1 ;
274156}
275157
276-
277- static int math_randomseed (lua_State * L ) {
278- l_srand ((unsigned int )(lua_Integer )luaL_checknumber (L , 1 ));
279- (void )l_rand (); /* discard first value to avoid undesirable correlations */
280- return 0 ;
281- }
282-
283-
284- static int math_type (lua_State * L ) {
158+ extern int math_type (lua_State * L ) {
285159 if (lua_type (L , 1 ) == LUA_TNUMBER ) {
286160 if (lua_isinteger (L , 1 ))
287161 lua_pushliteral (L , "integer" );
@@ -303,108 +177,13 @@ static int math_type (lua_State *L) {
303177*/
304178#if defined(LUA_COMPAT_MATHLIB )
305179
306- static int math_cosh (lua_State * L ) {
307- lua_pushnumber (L , l_mathop (cosh )(luaL_checknumber (L , 1 )));
308- return 1 ;
309- }
310-
311- static int math_sinh (lua_State * L ) {
312- lua_pushnumber (L , l_mathop (sinh )(luaL_checknumber (L , 1 )));
313- return 1 ;
314- }
315-
316- static int math_tanh (lua_State * L ) {
317- lua_pushnumber (L , l_mathop (tanh )(luaL_checknumber (L , 1 )));
318- return 1 ;
319- }
320-
321- static int math_pow (lua_State * L ) {
322- lua_Number x = luaL_checknumber (L , 1 );
323- lua_Number y = luaL_checknumber (L , 2 );
324- lua_pushnumber (L , l_mathop (pow )(x , y ));
325- return 1 ;
326- }
327-
328- static int math_frexp (lua_State * L ) {
329- int e ;
330- lua_pushnumber (L , l_mathop (frexp )(luaL_checknumber (L , 1 ), & e ));
331- lua_pushinteger (L , e );
332- return 2 ;
333- }
334-
335- static int math_ldexp (lua_State * L ) {
336- lua_Number x = luaL_checknumber (L , 1 );
337- int ep = (int )luaL_checkinteger (L , 2 );
338- lua_pushnumber (L , l_mathop (ldexp )(x , ep ));
339- return 1 ;
340- }
341-
342- static int math_log10 (lua_State * L ) {
343- lua_pushnumber (L , l_mathop (log10 )(luaL_checknumber (L , 1 )));
344- return 1 ;
345- }
180+ static int math_cosh (lua_State * L );
181+ static int math_sinh (lua_State * L );
182+ static int math_tanh (lua_State * L );
183+ static int math_pow (lua_State * L );
184+ static int math_frexp (lua_State * L );
185+ static int math_ldexp (lua_State * L );
186+ static int math_log10 (lua_State * L );
346187
347188#endif
348189/* }================================================================== */
349-
350-
351-
352- static const luaL_Reg mathlib [] = {
353- {"abs" , math_abs },
354- {"acos" , math_acos },
355- {"asin" , math_asin },
356- {"atan" , math_atan },
357- {"ceil" , math_ceil },
358- {"cos" , math_cos },
359- {"deg" , math_deg },
360- {"exp" , math_exp },
361- {"tointeger" , math_toint },
362- {"floor" , math_floor },
363- {"fmod" , math_fmod },
364- {"ult" , math_ult },
365- {"log" , math_log },
366- {"max" , math_max },
367- {"min" , math_min },
368- {"modf" , math_modf },
369- {"rad" , math_rad },
370- {"random" , math_random },
371- {"randomseed" , math_randomseed },
372- {"sin" , math_sin },
373- {"sqrt" , math_sqrt },
374- {"tan" , math_tan },
375- {"type" , math_type },
376- #if defined(LUA_COMPAT_MATHLIB )
377- {"atan2" , math_atan },
378- {"cosh" , math_cosh },
379- {"sinh" , math_sinh },
380- {"tanh" , math_tanh },
381- {"pow" , math_pow },
382- {"frexp" , math_frexp },
383- {"ldexp" , math_ldexp },
384- {"log10" , math_log10 },
385- #endif
386- /* placeholders */
387- {"pi" , NULL },
388- {"huge" , NULL },
389- {"maxinteger" , NULL },
390- {"mininteger" , NULL },
391- {NULL , NULL }
392- };
393-
394-
395- /*
396- ** Open math library
397- */
398- LUAMOD_API int luaopen_math (lua_State * L ) {
399- luaL_newlib (L , mathlib );
400- lua_pushnumber (L , PI );
401- lua_setfield (L , -2 , "pi" );
402- lua_pushnumber (L , (lua_Number )HUGE_VAL );
403- lua_setfield (L , -2 , "huge" );
404- lua_pushinteger (L , LUA_MAXINTEGER );
405- lua_setfield (L , -2 , "maxinteger" );
406- lua_pushinteger (L , LUA_MININTEGER );
407- lua_setfield (L , -2 , "mininteger" );
408- return 1 ;
409- }
410-
0 commit comments