lua

A copy of the Lua development repository
Log | Files | Refs | README

lmathlib.c (19312B)


      1 /*
      2 ** $Id: lmathlib.c $
      3 ** Standard mathematical library
      4 ** See Copyright Notice in lua.h
      5 */
      6 
      7 #define lmathlib_c
      8 #define LUA_LIB
      9 
     10 #include "lprefix.h"
     11 
     12 
     13 #include <float.h>
     14 #include <limits.h>
     15 #include <math.h>
     16 #include <stdlib.h>
     17 #include <time.h>
     18 
     19 #include "lua.h"
     20 
     21 #include "lauxlib.h"
     22 #include "lualib.h"
     23 #include "llimits.h"
     24 
     25 
     26 #undef PI
     27 #define PI	(l_mathop(3.141592653589793238462643383279502884))
     28 
     29 
     30 static int math_abs (lua_State *L) {
     31   if (lua_isinteger(L, 1)) {
     32     lua_Integer n = lua_tointeger(L, 1);
     33     if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
     34     lua_pushinteger(L, n);
     35   }
     36   else
     37     lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
     38   return 1;
     39 }
     40 
     41 static int math_sin (lua_State *L) {
     42   lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
     43   return 1;
     44 }
     45 
     46 static int math_cos (lua_State *L) {
     47   lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
     48   return 1;
     49 }
     50 
     51 static int math_tan (lua_State *L) {
     52   lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
     53   return 1;
     54 }
     55 
     56 static int math_asin (lua_State *L) {
     57   lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
     58   return 1;
     59 }
     60 
     61 static int math_acos (lua_State *L) {
     62   lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
     63   return 1;
     64 }
     65 
     66 static int math_atan (lua_State *L) {
     67   lua_Number y = luaL_checknumber(L, 1);
     68   lua_Number x = luaL_optnumber(L, 2, 1);
     69   lua_pushnumber(L, l_mathop(atan2)(y, x));
     70   return 1;
     71 }
     72 
     73 
     74 static int math_toint (lua_State *L) {
     75   int valid;
     76   lua_Integer n = lua_tointegerx(L, 1, &valid);
     77   if (l_likely(valid))
     78     lua_pushinteger(L, n);
     79   else {
     80     luaL_checkany(L, 1);
     81     luaL_pushfail(L);  /* value is not convertible to integer */
     82   }
     83   return 1;
     84 }
     85 
     86 
     87 static void pushnumint (lua_State *L, lua_Number d) {
     88   lua_Integer n;
     89   if (lua_numbertointeger(d, &n))  /* does 'd' fit in an integer? */
     90     lua_pushinteger(L, n);  /* result is integer */
     91   else
     92     lua_pushnumber(L, d);  /* result is float */
     93 }
     94 
     95 
     96 static int math_floor (lua_State *L) {
     97   if (lua_isinteger(L, 1))
     98     lua_settop(L, 1);  /* integer is its own floor */
     99   else {
    100     lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
    101     pushnumint(L, d);
    102   }
    103   return 1;
    104 }
    105 
    106 
    107 static int math_ceil (lua_State *L) {
    108   if (lua_isinteger(L, 1))
    109     lua_settop(L, 1);  /* integer is its own ceiling */
    110   else {
    111     lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
    112     pushnumint(L, d);
    113   }
    114   return 1;
    115 }
    116 
    117 
    118 static int math_fmod (lua_State *L) {
    119   if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
    120     lua_Integer d = lua_tointeger(L, 2);
    121     if ((lua_Unsigned)d + 1u <= 1u) {  /* special cases: -1 or 0 */
    122       luaL_argcheck(L, d != 0, 2, "zero");
    123       lua_pushinteger(L, 0);  /* avoid overflow with 0x80000... / -1 */
    124     }
    125     else
    126       lua_pushinteger(L, lua_tointeger(L, 1) % d);
    127   }
    128   else
    129     lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
    130                                      luaL_checknumber(L, 2)));
    131   return 1;
    132 }
    133 
    134 
    135 /*
    136 ** next function does not use 'modf', avoiding problems with 'double*'
    137 ** (which is not compatible with 'float*') when lua_Number is not
    138 ** 'double'.
    139 */
    140 static int math_modf (lua_State *L) {
    141   if (lua_isinteger(L ,1)) {
    142     lua_settop(L, 1);  /* number is its own integer part */
    143     lua_pushnumber(L, 0);  /* no fractional part */
    144   }
    145   else {
    146     lua_Number n = luaL_checknumber(L, 1);
    147     /* integer part (rounds toward zero) */
    148     lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
    149     pushnumint(L, ip);
    150     /* fractional part (test needed for inf/-inf) */
    151     lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
    152   }
    153   return 2;
    154 }
    155 
    156 
    157 static int math_sqrt (lua_State *L) {
    158   lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
    159   return 1;
    160 }
    161 
    162 
    163 static int math_ult (lua_State *L) {
    164   lua_Integer a = luaL_checkinteger(L, 1);
    165   lua_Integer b = luaL_checkinteger(L, 2);
    166   lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
    167   return 1;
    168 }
    169 
    170 static int math_log (lua_State *L) {
    171   lua_Number x = luaL_checknumber(L, 1);
    172   lua_Number res;
    173   if (lua_isnoneornil(L, 2))
    174     res = l_mathop(log)(x);
    175   else {
    176     lua_Number base = luaL_checknumber(L, 2);
    177 #if !defined(LUA_USE_C89)
    178     if (base == l_mathop(2.0))
    179       res = l_mathop(log2)(x);
    180     else
    181 #endif
    182     if (base == l_mathop(10.0))
    183       res = l_mathop(log10)(x);
    184     else
    185       res = l_mathop(log)(x)/l_mathop(log)(base);
    186   }
    187   lua_pushnumber(L, res);
    188   return 1;
    189 }
    190 
    191 static int math_exp (lua_State *L) {
    192   lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
    193   return 1;
    194 }
    195 
    196 static int math_deg (lua_State *L) {
    197   lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
    198   return 1;
    199 }
    200 
    201 static int math_rad (lua_State *L) {
    202   lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
    203   return 1;
    204 }
    205 
    206 
    207 static int math_min (lua_State *L) {
    208   int n = lua_gettop(L);  /* number of arguments */
    209   int imin = 1;  /* index of current minimum value */
    210   int i;
    211   luaL_argcheck(L, n >= 1, 1, "value expected");
    212   for (i = 2; i <= n; i++) {
    213     if (lua_compare(L, i, imin, LUA_OPLT))
    214       imin = i;
    215   }
    216   lua_pushvalue(L, imin);
    217   return 1;
    218 }
    219 
    220 
    221 static int math_max (lua_State *L) {
    222   int n = lua_gettop(L);  /* number of arguments */
    223   int imax = 1;  /* index of current maximum value */
    224   int i;
    225   luaL_argcheck(L, n >= 1, 1, "value expected");
    226   for (i = 2; i <= n; i++) {
    227     if (lua_compare(L, imax, i, LUA_OPLT))
    228       imax = i;
    229   }
    230   lua_pushvalue(L, imax);
    231   return 1;
    232 }
    233 
    234 
    235 static int math_type (lua_State *L) {
    236   if (lua_type(L, 1) == LUA_TNUMBER)
    237     lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
    238   else {
    239     luaL_checkany(L, 1);
    240     luaL_pushfail(L);
    241   }
    242   return 1;
    243 }
    244 
    245 
    246 
    247 /*
    248 ** {==================================================================
    249 ** Pseudo-Random Number Generator based on 'xoshiro256**'.
    250 ** ===================================================================
    251 */
    252 
    253 /*
    254 ** This code uses lots of shifts. ANSI C does not allow shifts greater
    255 ** than or equal to the width of the type being shifted, so some shifts
    256 ** are written in convoluted ways to match that restriction. For
    257 ** preprocessor tests, it assumes a width of 32 bits, so the maximum
    258 ** shift there is 31 bits.
    259 */
    260 
    261 
    262 /* number of binary digits in the mantissa of a float */
    263 #define FIGS	l_floatatt(MANT_DIG)
    264 
    265 #if FIGS > 64
    266 /* there are only 64 random bits; use them all */
    267 #undef FIGS
    268 #define FIGS	64
    269 #endif
    270 
    271 
    272 /*
    273 ** LUA_RAND32 forces the use of 32-bit integers in the implementation
    274 ** of the PRN generator (mainly for testing).
    275 */
    276 #if !defined(LUA_RAND32) && !defined(Rand64)
    277 
    278 /* try to find an integer type with at least 64 bits */
    279 
    280 #if ((ULONG_MAX >> 31) >> 31) >= 3
    281 
    282 /* 'long' has at least 64 bits */
    283 #define Rand64		unsigned long
    284 #define SRand64		long
    285 
    286 #elif !defined(LUA_USE_C89) && defined(LLONG_MAX)
    287 
    288 /* there is a 'long long' type (which must have at least 64 bits) */
    289 #define Rand64		unsigned long long
    290 #define SRand64		long long
    291 
    292 #elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3
    293 
    294 /* 'lua_Unsigned' has at least 64 bits */
    295 #define Rand64		lua_Unsigned
    296 #define SRand64		lua_Integer
    297 
    298 #endif
    299 
    300 #endif
    301 
    302 
    303 #if defined(Rand64)  /* { */
    304 
    305 /*
    306 ** Standard implementation, using 64-bit integers.
    307 ** If 'Rand64' has more than 64 bits, the extra bits do not interfere
    308 ** with the 64 initial bits, except in a right shift. Moreover, the
    309 ** final result has to discard the extra bits.
    310 */
    311 
    312 /* avoid using extra bits when needed */
    313 #define trim64(x)	((x) & 0xffffffffffffffffu)
    314 
    315 
    316 /* rotate left 'x' by 'n' bits */
    317 static Rand64 rotl (Rand64 x, int n) {
    318   return (x << n) | (trim64(x) >> (64 - n));
    319 }
    320 
    321 static Rand64 nextrand (Rand64 *state) {
    322   Rand64 state0 = state[0];
    323   Rand64 state1 = state[1];
    324   Rand64 state2 = state[2] ^ state0;
    325   Rand64 state3 = state[3] ^ state1;
    326   Rand64 res = rotl(state1 * 5, 7) * 9;
    327   state[0] = state0 ^ state3;
    328   state[1] = state1 ^ state2;
    329   state[2] = state2 ^ (state1 << 17);
    330   state[3] = rotl(state3, 45);
    331   return res;
    332 }
    333 
    334 
    335 /*
    336 ** Convert bits from a random integer into a float in the
    337 ** interval [0,1), getting the higher FIG bits from the
    338 ** random unsigned integer and converting that to a float.
    339 ** Some old Microsoft compilers cannot cast an unsigned long
    340 ** to a floating-point number, so we use a signed long as an
    341 ** intermediary. When lua_Number is float or double, the shift ensures
    342 ** that 'sx' is non negative; in that case, a good compiler will remove
    343 ** the correction.
    344 */
    345 
    346 /* must throw out the extra (64 - FIGS) bits */
    347 #define shift64_FIG	(64 - FIGS)
    348 
    349 /* 2^(-FIGS) == 2^-1 / 2^(FIGS-1) */
    350 #define scaleFIG	(l_mathop(0.5) / ((Rand64)1 << (FIGS - 1)))
    351 
    352 static lua_Number I2d (Rand64 x) {
    353   SRand64 sx = (SRand64)(trim64(x) >> shift64_FIG);
    354   lua_Number res = (lua_Number)(sx) * scaleFIG;
    355   if (sx < 0)
    356     res += l_mathop(1.0);  /* correct the two's complement if negative */
    357   lua_assert(0 <= res && res < 1);
    358   return res;
    359 }
    360 
    361 /* convert a 'Rand64' to a 'lua_Unsigned' */
    362 #define I2UInt(x)	((lua_Unsigned)trim64(x))
    363 
    364 /* convert a 'lua_Unsigned' to a 'Rand64' */
    365 #define Int2I(x)	((Rand64)(x))
    366 
    367 
    368 #else	/* no 'Rand64'   }{ */
    369 
    370 /*
    371 ** Use two 32-bit integers to represent a 64-bit quantity.
    372 */
    373 typedef struct Rand64 {
    374   l_uint32 h;  /* higher half */
    375   l_uint32 l;  /* lower half */
    376 } Rand64;
    377 
    378 
    379 /*
    380 ** If 'l_uint32' has more than 32 bits, the extra bits do not interfere
    381 ** with the 32 initial bits, except in a right shift and comparisons.
    382 ** Moreover, the final result has to discard the extra bits.
    383 */
    384 
    385 /* avoid using extra bits when needed */
    386 #define trim32(x)	((x) & 0xffffffffu)
    387 
    388 
    389 /*
    390 ** basic operations on 'Rand64' values
    391 */
    392 
    393 /* build a new Rand64 value */
    394 static Rand64 packI (l_uint32 h, l_uint32 l) {
    395   Rand64 result;
    396   result.h = h;
    397   result.l = l;
    398   return result;
    399 }
    400 
    401 /* return i << n */
    402 static Rand64 Ishl (Rand64 i, int n) {
    403   lua_assert(n > 0 && n < 32);
    404   return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n);
    405 }
    406 
    407 /* i1 ^= i2 */
    408 static void Ixor (Rand64 *i1, Rand64 i2) {
    409   i1->h ^= i2.h;
    410   i1->l ^= i2.l;
    411 }
    412 
    413 /* return i1 + i2 */
    414 static Rand64 Iadd (Rand64 i1, Rand64 i2) {
    415   Rand64 result = packI(i1.h + i2.h, i1.l + i2.l);
    416   if (trim32(result.l) < trim32(i1.l))  /* carry? */
    417     result.h++;
    418   return result;
    419 }
    420 
    421 /* return i * 5 */
    422 static Rand64 times5 (Rand64 i) {
    423   return Iadd(Ishl(i, 2), i);  /* i * 5 == (i << 2) + i */
    424 }
    425 
    426 /* return i * 9 */
    427 static Rand64 times9 (Rand64 i) {
    428   return Iadd(Ishl(i, 3), i);  /* i * 9 == (i << 3) + i */
    429 }
    430 
    431 /* return 'i' rotated left 'n' bits */
    432 static Rand64 rotl (Rand64 i, int n) {
    433   lua_assert(n > 0 && n < 32);
    434   return packI((i.h << n) | (trim32(i.l) >> (32 - n)),
    435                (trim32(i.h) >> (32 - n)) | (i.l << n));
    436 }
    437 
    438 /* for offsets larger than 32, rotate right by 64 - offset */
    439 static Rand64 rotl1 (Rand64 i, int n) {
    440   lua_assert(n > 32 && n < 64);
    441   n = 64 - n;
    442   return packI((trim32(i.h) >> n) | (i.l << (32 - n)),
    443                (i.h << (32 - n)) | (trim32(i.l) >> n));
    444 }
    445 
    446 /*
    447 ** implementation of 'xoshiro256**' algorithm on 'Rand64' values
    448 */
    449 static Rand64 nextrand (Rand64 *state) {
    450   Rand64 res = times9(rotl(times5(state[1]), 7));
    451   Rand64 t = Ishl(state[1], 17);
    452   Ixor(&state[2], state[0]);
    453   Ixor(&state[3], state[1]);
    454   Ixor(&state[1], state[2]);
    455   Ixor(&state[0], state[3]);
    456   Ixor(&state[2], t);
    457   state[3] = rotl1(state[3], 45);
    458   return res;
    459 }
    460 
    461 
    462 /*
    463 ** Converts a 'Rand64' into a float.
    464 */
    465 
    466 /* an unsigned 1 with proper type */
    467 #define UONE		((l_uint32)1)
    468 
    469 
    470 #if FIGS <= 32
    471 
    472 /* 2^(-FIGS) */
    473 #define scaleFIG       (l_mathop(0.5) / (UONE << (FIGS - 1)))
    474 
    475 /*
    476 ** get up to 32 bits from higher half, shifting right to
    477 ** throw out the extra bits.
    478 */
    479 static lua_Number I2d (Rand64 x) {
    480   lua_Number h = (lua_Number)(trim32(x.h) >> (32 - FIGS));
    481   return h * scaleFIG;
    482 }
    483 
    484 #else	/* 32 < FIGS <= 64 */
    485 
    486 /* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */
    487 #define scaleFIG  \
    488     (l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33)))
    489 
    490 /*
    491 ** use FIGS - 32 bits from lower half, throwing out the other
    492 ** (32 - (FIGS - 32)) = (64 - FIGS) bits
    493 */
    494 #define shiftLOW	(64 - FIGS)
    495 
    496 /*
    497 ** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32)
    498 */
    499 #define shiftHI		((lua_Number)(UONE << (FIGS - 33)) * l_mathop(2.0))
    500 
    501 
    502 static lua_Number I2d (Rand64 x) {
    503   lua_Number h = (lua_Number)trim32(x.h) * shiftHI;
    504   lua_Number l = (lua_Number)(trim32(x.l) >> shiftLOW);
    505   return (h + l) * scaleFIG;
    506 }
    507 
    508 #endif
    509 
    510 
    511 /* convert a 'Rand64' to a 'lua_Unsigned' */
    512 static lua_Unsigned I2UInt (Rand64 x) {
    513   return (((lua_Unsigned)trim32(x.h) << 31) << 1) | (lua_Unsigned)trim32(x.l);
    514 }
    515 
    516 /* convert a 'lua_Unsigned' to a 'Rand64' */
    517 static Rand64 Int2I (lua_Unsigned n) {
    518   return packI((l_uint32)((n >> 31) >> 1), (l_uint32)n);
    519 }
    520 
    521 #endif  /* } */
    522 
    523 
    524 /*
    525 ** A state uses four 'Rand64' values.
    526 */
    527 typedef struct {
    528   Rand64 s[4];
    529 } RanState;
    530 
    531 
    532 /*
    533 ** Project the random integer 'ran' into the interval [0, n].
    534 ** Because 'ran' has 2^B possible values, the projection can only be
    535 ** uniform when the size of the interval is a power of 2 (exact
    536 ** division). Otherwise, to get a uniform projection into [0, n], we
    537 ** first compute 'lim', the smallest Mersenne number not smaller than
    538 ** 'n'. We then project 'ran' into the interval [0, lim].  If the result
    539 ** is inside [0, n], we are done. Otherwise, we try with another 'ran',
    540 ** until we have a result inside the interval.
    541 */
    542 static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
    543                              RanState *state) {
    544   if ((n & (n + 1)) == 0)  /* is 'n + 1' a power of 2? */
    545     return ran & n;  /* no bias */
    546   else {
    547     lua_Unsigned lim = n;
    548     /* compute the smallest (2^b - 1) not smaller than 'n' */
    549     lim |= (lim >> 1);
    550     lim |= (lim >> 2);
    551     lim |= (lim >> 4);
    552     lim |= (lim >> 8);
    553     lim |= (lim >> 16);
    554 #if (LUA_MAXUNSIGNED >> 31) >= 3
    555     lim |= (lim >> 32);  /* integer type has more than 32 bits */
    556 #endif
    557     lua_assert((lim & (lim + 1)) == 0  /* 'lim + 1' is a power of 2, */
    558       && lim >= n  /* not smaller than 'n', */
    559       && (lim >> 1) < n);  /* and it is the smallest one */
    560     while ((ran &= lim) > n)  /* project 'ran' into [0..lim] */
    561       ran = I2UInt(nextrand(state->s));  /* not inside [0..n]? try again */
    562     return ran;
    563   }
    564 }
    565 
    566 
    567 static int math_random (lua_State *L) {
    568   lua_Integer low, up;
    569   lua_Unsigned p;
    570   RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
    571   Rand64 rv = nextrand(state->s);  /* next pseudo-random value */
    572   switch (lua_gettop(L)) {  /* check number of arguments */
    573     case 0: {  /* no arguments */
    574       lua_pushnumber(L, I2d(rv));  /* float between 0 and 1 */
    575       return 1;
    576     }
    577     case 1: {  /* only upper limit */
    578       low = 1;
    579       up = luaL_checkinteger(L, 1);
    580       if (up == 0) {  /* single 0 as argument? */
    581         lua_pushinteger(L, l_castU2S(I2UInt(rv)));  /* full random integer */
    582         return 1;
    583       }
    584       break;
    585     }
    586     case 2: {  /* lower and upper limits */
    587       low = luaL_checkinteger(L, 1);
    588       up = luaL_checkinteger(L, 2);
    589       break;
    590     }
    591     default: return luaL_error(L, "wrong number of arguments");
    592   }
    593   /* random integer in the interval [low, up] */
    594   luaL_argcheck(L, low <= up, 1, "interval is empty");
    595   /* project random integer into the interval [0, up - low] */
    596   p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);
    597   lua_pushinteger(L, l_castU2S(p) + low);
    598   return 1;
    599 }
    600 
    601 
    602 static void setseed (lua_State *L, Rand64 *state,
    603                      lua_Unsigned n1, lua_Unsigned n2) {
    604   int i;
    605   state[0] = Int2I(n1);
    606   state[1] = Int2I(0xff);  /* avoid a zero state */
    607   state[2] = Int2I(n2);
    608   state[3] = Int2I(0);
    609   for (i = 0; i < 16; i++)
    610     nextrand(state);  /* discard initial values to "spread" seed */
    611   lua_pushinteger(L, l_castU2S(n1));
    612   lua_pushinteger(L, l_castU2S(n2));
    613 }
    614 
    615 
    616 static int math_randomseed (lua_State *L) {
    617   RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
    618   lua_Unsigned n1, n2;
    619   if (lua_isnone(L, 1)) {
    620     n1 = luaL_makeseed(L);  /* "random" seed */
    621     n2 = I2UInt(nextrand(state->s));  /* in case seed is not that random... */
    622   }
    623   else {
    624     n1 = l_castS2U(luaL_checkinteger(L, 1));
    625     n2 = l_castS2U(luaL_optinteger(L, 2, 0));
    626   }
    627   setseed(L, state->s, n1, n2);
    628   return 2;  /* return seeds */
    629 }
    630 
    631 
    632 static const luaL_Reg randfuncs[] = {
    633   {"random", math_random},
    634   {"randomseed", math_randomseed},
    635   {NULL, NULL}
    636 };
    637 
    638 
    639 /*
    640 ** Register the random functions and initialize their state.
    641 */
    642 static void setrandfunc (lua_State *L) {
    643   RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
    644   setseed(L, state->s, luaL_makeseed(L), 0);  /* initialize with random seed */
    645   lua_pop(L, 2);  /* remove pushed seeds */
    646   luaL_setfuncs(L, randfuncs, 1);
    647 }
    648 
    649 /* }================================================================== */
    650 
    651 
    652 /*
    653 ** {==================================================================
    654 ** Deprecated functions (for compatibility only)
    655 ** ===================================================================
    656 */
    657 #if defined(LUA_COMPAT_MATHLIB)
    658 
    659 static int math_cosh (lua_State *L) {
    660   lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
    661   return 1;
    662 }
    663 
    664 static int math_sinh (lua_State *L) {
    665   lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
    666   return 1;
    667 }
    668 
    669 static int math_tanh (lua_State *L) {
    670   lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
    671   return 1;
    672 }
    673 
    674 static int math_pow (lua_State *L) {
    675   lua_Number x = luaL_checknumber(L, 1);
    676   lua_Number y = luaL_checknumber(L, 2);
    677   lua_pushnumber(L, l_mathop(pow)(x, y));
    678   return 1;
    679 }
    680 
    681 static int math_frexp (lua_State *L) {
    682   int e;
    683   lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
    684   lua_pushinteger(L, e);
    685   return 2;
    686 }
    687 
    688 static int math_ldexp (lua_State *L) {
    689   lua_Number x = luaL_checknumber(L, 1);
    690   int ep = (int)luaL_checkinteger(L, 2);
    691   lua_pushnumber(L, l_mathop(ldexp)(x, ep));
    692   return 1;
    693 }
    694 
    695 static int math_log10 (lua_State *L) {
    696   lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
    697   return 1;
    698 }
    699 
    700 #endif
    701 /* }================================================================== */
    702 
    703 
    704 
    705 static const luaL_Reg mathlib[] = {
    706   {"abs",   math_abs},
    707   {"acos",  math_acos},
    708   {"asin",  math_asin},
    709   {"atan",  math_atan},
    710   {"ceil",  math_ceil},
    711   {"cos",   math_cos},
    712   {"deg",   math_deg},
    713   {"exp",   math_exp},
    714   {"tointeger", math_toint},
    715   {"floor", math_floor},
    716   {"fmod",   math_fmod},
    717   {"ult",   math_ult},
    718   {"log",   math_log},
    719   {"max",   math_max},
    720   {"min",   math_min},
    721   {"modf",   math_modf},
    722   {"rad",   math_rad},
    723   {"sin",   math_sin},
    724   {"sqrt",  math_sqrt},
    725   {"tan",   math_tan},
    726   {"type", math_type},
    727 #if defined(LUA_COMPAT_MATHLIB)
    728   {"atan2", math_atan},
    729   {"cosh",   math_cosh},
    730   {"sinh",   math_sinh},
    731   {"tanh",   math_tanh},
    732   {"pow",   math_pow},
    733   {"frexp", math_frexp},
    734   {"ldexp", math_ldexp},
    735   {"log10", math_log10},
    736 #endif
    737   /* placeholders */
    738   {"random", NULL},
    739   {"randomseed", NULL},
    740   {"pi", NULL},
    741   {"huge", NULL},
    742   {"maxinteger", NULL},
    743   {"mininteger", NULL},
    744   {NULL, NULL}
    745 };
    746 
    747 
    748 /*
    749 ** Open math library
    750 */
    751 LUAMOD_API int luaopen_math (lua_State *L) {
    752   luaL_newlib(L, mathlib);
    753   lua_pushnumber(L, PI);
    754   lua_setfield(L, -2, "pi");
    755   lua_pushnumber(L, (lua_Number)HUGE_VAL);
    756   lua_setfield(L, -2, "huge");
    757   lua_pushinteger(L, LUA_MAXINTEGER);
    758   lua_setfield(L, -2, "maxinteger");
    759   lua_pushinteger(L, LUA_MININTEGER);
    760   lua_setfield(L, -2, "mininteger");
    761   setrandfunc(L);
    762   return 1;
    763 }
    764