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