lmathlib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. ** $Id: lmathlib.c,v 1.130 2018/04/06 15:41:29 roberto Exp roberto $
  3. ** Standard mathematical library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lmathlib_c
  7. #define LUA_LIB
  8. #include "lprefix.h"
  9. #include <float.h>
  10. #include <limits.h>
  11. #include <math.h>
  12. #include <stdlib.h>
  13. #include "lua.h"
  14. #include "lauxlib.h"
  15. #include "lualib.h"
  16. #undef PI
  17. #define PI (l_mathop(3.141592653589793238462643383279502884))
  18. static int math_abs (lua_State *L) {
  19. if (lua_isinteger(L, 1)) {
  20. lua_Integer n = lua_tointeger(L, 1);
  21. if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
  22. lua_pushinteger(L, n);
  23. }
  24. else
  25. lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
  26. return 1;
  27. }
  28. static int math_sin (lua_State *L) {
  29. lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
  30. return 1;
  31. }
  32. static int math_cos (lua_State *L) {
  33. lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
  34. return 1;
  35. }
  36. static int math_tan (lua_State *L) {
  37. lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
  38. return 1;
  39. }
  40. static int math_asin (lua_State *L) {
  41. lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
  42. return 1;
  43. }
  44. static int math_acos (lua_State *L) {
  45. lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
  46. return 1;
  47. }
  48. static int math_atan (lua_State *L) {
  49. lua_Number y = luaL_checknumber(L, 1);
  50. lua_Number x = luaL_optnumber(L, 2, 1);
  51. lua_pushnumber(L, l_mathop(atan2)(y, x));
  52. return 1;
  53. }
  54. static int math_toint (lua_State *L) {
  55. int valid;
  56. lua_Integer n = lua_tointegerx(L, 1, &valid);
  57. if (valid)
  58. lua_pushinteger(L, n);
  59. else {
  60. luaL_checkany(L, 1);
  61. lua_pushnil(L); /* value is not convertible to integer */
  62. }
  63. return 1;
  64. }
  65. static void pushnumint (lua_State *L, lua_Number d) {
  66. lua_Integer n;
  67. if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
  68. lua_pushinteger(L, n); /* result is integer */
  69. else
  70. lua_pushnumber(L, d); /* result is float */
  71. }
  72. static int math_floor (lua_State *L) {
  73. if (lua_isinteger(L, 1))
  74. lua_settop(L, 1); /* integer is its own floor */
  75. else {
  76. lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
  77. pushnumint(L, d);
  78. }
  79. return 1;
  80. }
  81. static int math_ceil (lua_State *L) {
  82. if (lua_isinteger(L, 1))
  83. lua_settop(L, 1); /* integer is its own ceil */
  84. else {
  85. lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
  86. pushnumint(L, d);
  87. }
  88. return 1;
  89. }
  90. static int math_fmod (lua_State *L) {
  91. if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
  92. lua_Integer d = lua_tointeger(L, 2);
  93. if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
  94. luaL_argcheck(L, d != 0, 2, "zero");
  95. lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
  96. }
  97. else
  98. lua_pushinteger(L, lua_tointeger(L, 1) % d);
  99. }
  100. else
  101. lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
  102. luaL_checknumber(L, 2)));
  103. return 1;
  104. }
  105. /*
  106. ** next function does not use 'modf', avoiding problems with 'double*'
  107. ** (which is not compatible with 'float*') when lua_Number is not
  108. ** 'double'.
  109. */
  110. static int math_modf (lua_State *L) {
  111. if (lua_isinteger(L ,1)) {
  112. lua_settop(L, 1); /* number is its own integer part */
  113. lua_pushnumber(L, 0); /* no fractional part */
  114. }
  115. else {
  116. lua_Number n = luaL_checknumber(L, 1);
  117. /* integer part (rounds toward zero) */
  118. lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
  119. pushnumint(L, ip);
  120. /* fractional part (test needed for inf/-inf) */
  121. lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
  122. }
  123. return 2;
  124. }
  125. static int math_sqrt (lua_State *L) {
  126. lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
  127. return 1;
  128. }
  129. static int math_ult (lua_State *L) {
  130. lua_Integer a = luaL_checkinteger(L, 1);
  131. lua_Integer b = luaL_checkinteger(L, 2);
  132. lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
  133. return 1;
  134. }
  135. static int math_log (lua_State *L) {
  136. lua_Number x = luaL_checknumber(L, 1);
  137. lua_Number res;
  138. if (lua_isnoneornil(L, 2))
  139. res = l_mathop(log)(x);
  140. else {
  141. lua_Number base = luaL_checknumber(L, 2);
  142. #if !defined(LUA_USE_C89)
  143. if (base == l_mathop(2.0))
  144. res = l_mathop(log2)(x); else
  145. #endif
  146. if (base == l_mathop(10.0))
  147. res = l_mathop(log10)(x);
  148. else
  149. res = l_mathop(log)(x)/l_mathop(log)(base);
  150. }
  151. lua_pushnumber(L, res);
  152. return 1;
  153. }
  154. static int math_exp (lua_State *L) {
  155. lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
  156. return 1;
  157. }
  158. static int math_deg (lua_State *L) {
  159. lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
  160. return 1;
  161. }
  162. static int math_rad (lua_State *L) {
  163. lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
  164. return 1;
  165. }
  166. static int math_min (lua_State *L) {
  167. int n = lua_gettop(L); /* number of arguments */
  168. int imin = 1; /* index of current minimum value */
  169. int i;
  170. luaL_argcheck(L, n >= 1, 1, "value expected");
  171. for (i = 2; i <= n; i++) {
  172. if (lua_compare(L, i, imin, LUA_OPLT))
  173. imin = i;
  174. }
  175. lua_pushvalue(L, imin);
  176. return 1;
  177. }
  178. static int math_max (lua_State *L) {
  179. int n = lua_gettop(L); /* number of arguments */
  180. int imax = 1; /* index of current maximum value */
  181. int i;
  182. luaL_argcheck(L, n >= 1, 1, "value expected");
  183. for (i = 2; i <= n; i++) {
  184. if (lua_compare(L, imax, i, LUA_OPLT))
  185. imax = i;
  186. }
  187. lua_pushvalue(L, imax);
  188. return 1;
  189. }
  190. static int math_type (lua_State *L) {
  191. if (lua_type(L, 1) == LUA_TNUMBER)
  192. lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
  193. else {
  194. luaL_checkany(L, 1);
  195. lua_pushnil(L);
  196. }
  197. return 1;
  198. }
  199. /*
  200. ** {==================================================================
  201. ** Pseudo-Random Number Generator based on 'xoshiro256**'.
  202. ** ===================================================================
  203. */
  204. /* number of binary digits in the mantissa of a float */
  205. #define FIGS l_mathlim(MANT_DIG)
  206. #if FIGS > 64
  207. /* there are only 64 random bits; use them all */
  208. #undef FIGS
  209. #define FIGS 64
  210. #endif
  211. #if (!defined(LUA_USE_C89) && defined(LLONG_MAX) && !defined(LUA_DEBUG)) \
  212. || defined(Rand64) /* { */
  213. /*
  214. ** Assume long long.
  215. */
  216. #if !defined(Rand64)
  217. /* a 64-bit value */
  218. typedef unsigned long long Rand64;
  219. #endif
  220. /*
  221. ** If 'Rand64' has more than 64 bits, the extra bits do not interfere
  222. ** with the 64 initial bits, except in a right shift. Otherwise, we just
  223. ** have to make sure we never use them.
  224. */
  225. /* avoid using extra bits when needed */
  226. #define trim64(x) ((x) & 0xffffffffffffffffU)
  227. /* rotate left 'x' by 'n' bits */
  228. static Rand64 rotl (Rand64 x, int n) {
  229. return (x << n) | (trim64(x) >> (64 - n));
  230. }
  231. static Rand64 nextrand (Rand64 *state) {
  232. Rand64 res = rotl(state[0] * 5, 7) * 9;
  233. Rand64 t = state[1] << 17;
  234. state[2] ^= state[0];
  235. state[3] ^= state[1];
  236. state[1] ^= state[2];
  237. state[0] ^= state[3];
  238. state[2] ^= t;
  239. state[3] = rotl(state[3], 45);
  240. return res;
  241. }
  242. /* must take care to not shift stuff by more than 63 slots */
  243. /*
  244. ** Convert bits from a random integer into a float in the
  245. ** interval [0,1).
  246. */
  247. #define maskFIG (~(~1LLU << (FIGS - 1))) /* use FIGS bits */
  248. #define shiftFIG (l_mathop(0.5) / (1LLU << (FIGS - 1))) /* 2^(-FIGS) */
  249. static lua_Number I2d (Rand64 x) {
  250. return (lua_Number)(x & maskFIG) * shiftFIG;
  251. }
  252. /* convert a 'Rand64' to a 'lua_Unsigned' */
  253. #define I2UInt(x) ((lua_Unsigned)trim64(x))
  254. /* convert a 'lua_Unsigned' to an 'Rand64' */
  255. #define Int2I(x) ((Rand64)(x))
  256. #else /* no long long }{ */
  257. /*
  258. ** Use two 32-bit integers to represent a 64-bit quantity.
  259. */
  260. #if LUAI_BITSINT >= 32
  261. typedef unsigned int lu_int32;
  262. #else
  263. typedef unsigned long lu_int32;
  264. #endif
  265. /* a 64-bit value */
  266. typedef struct Rand64 {
  267. lu_int32 h; /* higher half */
  268. lu_int32 l; /* lower half */
  269. } Rand64;
  270. /*
  271. ** If 'lu_int32' has more than 32 bits, the extra bits do not interfere
  272. ** with the 32 initial bits, except in a right shift and comparisons.
  273. ** Otherwise, we just have to make sure we never use them.
  274. */
  275. /* avoid using extra bits when needed */
  276. #define trim32(x) ((x) & 0xffffffffU)
  277. /*
  278. ** basic operations on 'Rand64' values
  279. */
  280. static Rand64 packI (lu_int32 h, lu_int32 l) {
  281. Rand64 result;
  282. result.h = h;
  283. result.l = l;
  284. return result;
  285. }
  286. static Rand64 Ishl (Rand64 i, int n) {
  287. lua_assert(n > 0 && n < 32);
  288. return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n);
  289. }
  290. static void Ixor (Rand64 *i1, Rand64 i2) {
  291. i1->h ^= i2.h;
  292. i1->l ^= i2.l;
  293. }
  294. static Rand64 Iadd (Rand64 i1, Rand64 i2) {
  295. Rand64 result = packI(i1.h + i2.h, i1.l + i2.l);
  296. if (trim32(result.l) < trim32(i1.l)) /* carry? */
  297. result.h++;
  298. return result;
  299. }
  300. static Rand64 times5 (Rand64 i) {
  301. return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */
  302. }
  303. static Rand64 times9 (Rand64 i) {
  304. return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */
  305. }
  306. static Rand64 rotl (Rand64 i, int n) {
  307. lua_assert(n > 0 && n < 32);
  308. return packI((i.h << n) | (trim32(i.l) >> (32 - n)),
  309. (trim32(i.h) >> (32 - n)) | (i.l << n));
  310. }
  311. /* for offsets larger than 32, rotate right by 64 - offset */
  312. static Rand64 rotl1 (Rand64 i, int n) {
  313. lua_assert(n > 32 && n < 64);
  314. n = 64 - n;
  315. return packI((trim32(i.h) >> n) | (i.l << (32 - n)),
  316. (i.h << (32 - n)) | (trim32(i.l) >> n));
  317. }
  318. /*
  319. ** implementation of 'xoshiro256**' algorithm on 'Rand64' values
  320. */
  321. static Rand64 nextrand (Rand64 *state) {
  322. Rand64 res = times9(rotl(times5(state[0]), 7));
  323. Rand64 t = Ishl(state[1], 17);
  324. Ixor(&state[2], state[0]);
  325. Ixor(&state[3], state[1]);
  326. Ixor(&state[1], state[2]);
  327. Ixor(&state[0], state[3]);
  328. Ixor(&state[2], t);
  329. state[3] = rotl1(state[3], 45);
  330. return res;
  331. }
  332. /*
  333. ** Converts an 'Rand64' into a float.
  334. */
  335. /* an unsigned 1 with proper type */
  336. #define UONE ((lu_int32)1)
  337. #if FIGS <= 32
  338. #define maskHI 0 /* do not need bits from higher half */
  339. #define maskLOW (~(~UONE << (FIGS - 1))) /* use FIGS bits */
  340. #define shiftFIG (l_mathop(0.5) / (UONE << (FIGS - 1))) /* 2^(-FIGS) */
  341. #else /* 32 < FIGS <= 64 */
  342. /* must take care to not shift stuff by more than 31 slots */
  343. /* use FIGS - 32 bits from higher half */
  344. #define maskHI (~(~UONE << (FIGS - 33)))
  345. /* use 32 bits from lower half */
  346. #define maskLOW (~(~UONE << 31))
  347. /* 2^(-FIGS) == (1 / 2^33) / 2^(FIGS-33) */
  348. #define shiftFIG ((lua_Number)(1.0 / 8589934592.0) / (UONE << (FIGS - 33)))
  349. #endif
  350. #define twoto32 l_mathop(4294967296.0) /* 2^32 */
  351. static lua_Number I2d (Rand64 x) {
  352. lua_Number h = (lua_Number)(x.h & maskHI);
  353. lua_Number l = (lua_Number)(x.l & maskLOW);
  354. return (h * twoto32 + l) * shiftFIG;
  355. }
  356. static lua_Unsigned I2UInt (Rand64 x) {
  357. return ((lua_Unsigned)x.h << 31 << 1) | (lua_Unsigned)trim32(x.l);
  358. }
  359. static Rand64 Int2I (lua_Unsigned n) {
  360. return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n);
  361. }
  362. #endif /* } */
  363. /*
  364. ** A state uses four 'Rand64' values.
  365. */
  366. typedef struct {
  367. Rand64 s[4];
  368. } RanState;
  369. /*
  370. ** Project the random integer 'ran' into the interval [0, n].
  371. ** Because 'ran' has 2^B possible values, the projection can only be
  372. ** uniform when the size of the interval is a power of 2 (exact
  373. ** division). To get a uniform projection into [0, n], we first compute
  374. ** 'lim', the smallest Mersenne number not smaller than 'n'. We then
  375. ** project 'ran' into the interval [0, lim]. If the result is inside
  376. ** [0, n], we are done. Otherwise, we try with another 'ran' until we
  377. ** have a result inside the interval.
  378. */
  379. static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
  380. RanState *state) {
  381. lua_Unsigned lim = n;
  382. if ((lim & (lim + 1)) > 0) { /* 'lim + 1' is not a power of 2? */
  383. /* compute the smallest (2^b - 1) not smaller than 'n' */
  384. lim |= (lim >> 1);
  385. lim |= (lim >> 2);
  386. lim |= (lim >> 4);
  387. lim |= (lim >> 8);
  388. lim |= (lim >> 16);
  389. #if (LUA_MAXINTEGER >> 30 >> 2) > 0
  390. lim |= (lim >> 32); /* integer type has more than 32 bits */
  391. #endif
  392. }
  393. lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2 */
  394. && lim >= n /* not smaller than 'n' */
  395. && (lim == 0 || (lim >> 1) < n)); /* it is the smallest one */
  396. while ((ran &= lim) > n)
  397. ran = I2UInt(nextrand(state->s));
  398. return ran;
  399. }
  400. static int math_random (lua_State *L) {
  401. lua_Integer low, up;
  402. lua_Unsigned p;
  403. RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
  404. Rand64 rv = nextrand(state->s); /* next pseudo-random value */
  405. switch (lua_gettop(L)) { /* check number of arguments */
  406. case 0: { /* no arguments */
  407. lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */
  408. return 1;
  409. }
  410. case 1: { /* only upper limit */
  411. low = 1;
  412. up = luaL_checkinteger(L, 1);
  413. if (up == 0) { /* single 0 as argument? */
  414. lua_pushinteger(L, I2UInt(rv)); /* full random integer */
  415. return 1;
  416. }
  417. break;
  418. }
  419. case 2: { /* lower and upper limits */
  420. low = luaL_checkinteger(L, 1);
  421. up = luaL_checkinteger(L, 2);
  422. break;
  423. }
  424. default: return luaL_error(L, "wrong number of arguments");
  425. }
  426. /* random integer in the interval [low, up] */
  427. luaL_argcheck(L, low <= up, 1, "interval is empty");
  428. /* project random integer into the interval [0, up - low] */
  429. p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);
  430. lua_pushinteger(L, p + (lua_Unsigned)low);
  431. return 1;
  432. }
  433. static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) {
  434. int i;
  435. state[0] = Int2I(n1);
  436. state[1] = Int2I(0xff); /* avoid a zero state */
  437. state[2] = Int2I(n2);
  438. state[3] = Int2I(0);
  439. for (i = 0; i < 16; i++)
  440. nextrand(state); /* discard initial values to "spread" seed */
  441. }
  442. static int math_randomseed (lua_State *L) {
  443. RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
  444. lua_Integer n1 = luaL_checkinteger(L, 1);
  445. lua_Integer n2 = luaL_optinteger(L, 2, 0);
  446. setseed(state->s, n1, n2);
  447. return 0;
  448. }
  449. static const luaL_Reg randfuncs[] = {
  450. {"random", math_random},
  451. {"randomseed", math_randomseed},
  452. {NULL, NULL}
  453. };
  454. static void setrandfunc (lua_State *L) {
  455. RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
  456. setseed(state->s, 0, 0);
  457. luaL_setfuncs(L, randfuncs, 1);
  458. }
  459. /* }================================================================== */
  460. /*
  461. ** {==================================================================
  462. ** Deprecated functions (for compatibility only)
  463. ** ===================================================================
  464. */
  465. #if defined(LUA_COMPAT_MATHLIB)
  466. static int math_cosh (lua_State *L) {
  467. lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
  468. return 1;
  469. }
  470. static int math_sinh (lua_State *L) {
  471. lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
  472. return 1;
  473. }
  474. static int math_tanh (lua_State *L) {
  475. lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
  476. return 1;
  477. }
  478. static int math_pow (lua_State *L) {
  479. lua_Number x = luaL_checknumber(L, 1);
  480. lua_Number y = luaL_checknumber(L, 2);
  481. lua_pushnumber(L, l_mathop(pow)(x, y));
  482. return 1;
  483. }
  484. static int math_frexp (lua_State *L) {
  485. int e;
  486. lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
  487. lua_pushinteger(L, e);
  488. return 2;
  489. }
  490. static int math_ldexp (lua_State *L) {
  491. lua_Number x = luaL_checknumber(L, 1);
  492. int ep = (int)luaL_checkinteger(L, 2);
  493. lua_pushnumber(L, l_mathop(ldexp)(x, ep));
  494. return 1;
  495. }
  496. static int math_log10 (lua_State *L) {
  497. lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
  498. return 1;
  499. }
  500. #endif
  501. /* }================================================================== */
  502. static const luaL_Reg mathlib[] = {
  503. {"abs", math_abs},
  504. {"acos", math_acos},
  505. {"asin", math_asin},
  506. {"atan", math_atan},
  507. {"ceil", math_ceil},
  508. {"cos", math_cos},
  509. {"deg", math_deg},
  510. {"exp", math_exp},
  511. {"tointeger", math_toint},
  512. {"floor", math_floor},
  513. {"fmod", math_fmod},
  514. {"ult", math_ult},
  515. {"log", math_log},
  516. {"max", math_max},
  517. {"min", math_min},
  518. {"modf", math_modf},
  519. {"rad", math_rad},
  520. {"sin", math_sin},
  521. {"sqrt", math_sqrt},
  522. {"tan", math_tan},
  523. {"type", math_type},
  524. #if defined(LUA_COMPAT_MATHLIB)
  525. {"atan2", math_atan},
  526. {"cosh", math_cosh},
  527. {"sinh", math_sinh},
  528. {"tanh", math_tanh},
  529. {"pow", math_pow},
  530. {"frexp", math_frexp},
  531. {"ldexp", math_ldexp},
  532. {"log10", math_log10},
  533. #endif
  534. /* placeholders */
  535. {"random", NULL},
  536. {"randomseed", NULL},
  537. {"pi", NULL},
  538. {"huge", NULL},
  539. {"maxinteger", NULL},
  540. {"mininteger", NULL},
  541. {NULL, NULL}
  542. };
  543. /*
  544. ** Open math library
  545. */
  546. LUAMOD_API int luaopen_math (lua_State *L) {
  547. luaL_newlib(L, mathlib);
  548. lua_pushnumber(L, PI);
  549. lua_setfield(L, -2, "pi");
  550. lua_pushnumber(L, (lua_Number)HUGE_VAL);
  551. lua_setfield(L, -2, "huge");
  552. lua_pushinteger(L, LUA_MAXINTEGER);
  553. lua_setfield(L, -2, "maxinteger");
  554. lua_pushinteger(L, LUA_MININTEGER);
  555. lua_setfield(L, -2, "mininteger");
  556. setrandfunc(L);
  557. return 1;
  558. }