lmathlib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. ** $Id: lmathlib.c,v 1.131 2018/04/06 17:52:42 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. /*
  212. ** LUA_RAND32 forces the use of 32-bit integers in the implementation
  213. ** of the PRN generator (mainly for testing).
  214. */
  215. #if !defined(LUA_RAND32) && !defined(Rand64)
  216. /* try to find an integer type with at least 64 bits */
  217. #if (LONG_MAX >> 31 >> 31) >= 1
  218. /* 'long' has at least 64 bits */
  219. #define Rand64 unsigned long
  220. #elif !defined(LUA_USE_C89) && defined(LLONG_MAX)
  221. /* there is a 'long long' type (which must have at least 64 bits) */
  222. #define Rand64 unsigned long long
  223. #elif (LUA_MAXINTEGER >> 31 >> 31) >= 1
  224. /* 'lua_Integer' has at least 64 bits */
  225. #define Rand64 LUA_UNSIGNED
  226. #endif
  227. #endif
  228. #if defined(Rand64) /* { */
  229. /*
  230. ** Standard implementation, using 64-bit integers.
  231. ** If 'Rand64' has more than 64 bits, the extra bits do not interfere
  232. ** with the 64 initial bits, except in a right shift. Moreover, the
  233. ** final result has to discard the extra bits.
  234. */
  235. /* avoid using extra bits when needed */
  236. #define trim64(x) ((x) & 0xffffffffffffffffu)
  237. /* rotate left 'x' by 'n' bits */
  238. static Rand64 rotl (Rand64 x, int n) {
  239. return (x << n) | (trim64(x) >> (64 - n));
  240. }
  241. static Rand64 nextrand (Rand64 *state) {
  242. Rand64 res = rotl(state[0] * 5, 7) * 9;
  243. Rand64 t = state[1] << 17;
  244. state[2] ^= state[0];
  245. state[3] ^= state[1];
  246. state[1] ^= state[2];
  247. state[0] ^= state[3];
  248. state[2] ^= t;
  249. state[3] = rotl(state[3], 45);
  250. return res;
  251. }
  252. /* must take care to not shift stuff by more than 63 slots */
  253. /*
  254. ** Convert bits from a random integer into a float in the
  255. ** interval [0,1).
  256. */
  257. #define maskFIG (~(~1LLU << (FIGS - 1))) /* use FIGS bits */
  258. #define shiftFIG (l_mathop(0.5) / (1LLU << (FIGS - 1))) /* 2^(-FIGS) */
  259. static lua_Number I2d (Rand64 x) {
  260. return (lua_Number)(x & maskFIG) * shiftFIG;
  261. }
  262. /* convert a 'Rand64' to a 'lua_Unsigned' */
  263. #define I2UInt(x) ((lua_Unsigned)trim64(x))
  264. /* convert a 'lua_Unsigned' to a 'Rand64' */
  265. #define Int2I(x) ((Rand64)(x))
  266. #else /* no 'Rand64' }{ */
  267. /* get an integer with at least 32 bits */
  268. #if (INT_MAX >> 30) >= 1
  269. typedef unsigned int lu_int32;
  270. #else
  271. typedef unsigned long lu_int32;
  272. #endif
  273. /*
  274. ** Use two 32-bit integers to represent a 64-bit quantity.
  275. */
  276. typedef struct Rand64 {
  277. lu_int32 h; /* higher half */
  278. lu_int32 l; /* lower half */
  279. } Rand64;
  280. /*
  281. ** If 'lu_int32' has more than 32 bits, the extra bits do not interfere
  282. ** with the 32 initial bits, except in a right shift and comparisons.
  283. ** Moreover, the final result has to discard the extra bits.
  284. */
  285. /* avoid using extra bits when needed */
  286. #define trim32(x) ((x) & 0xffffffffu)
  287. /*
  288. ** basic operations on 'Rand64' values
  289. */
  290. /* build a new Rand64 value */
  291. static Rand64 packI (lu_int32 h, lu_int32 l) {
  292. Rand64 result;
  293. result.h = h;
  294. result.l = l;
  295. return result;
  296. }
  297. /* return i << n */
  298. static Rand64 Ishl (Rand64 i, int n) {
  299. lua_assert(n > 0 && n < 32);
  300. return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n);
  301. }
  302. /* i1 ^= i2 */
  303. static void Ixor (Rand64 *i1, Rand64 i2) {
  304. i1->h ^= i2.h;
  305. i1->l ^= i2.l;
  306. }
  307. /* return i1 + i2 */
  308. static Rand64 Iadd (Rand64 i1, Rand64 i2) {
  309. Rand64 result = packI(i1.h + i2.h, i1.l + i2.l);
  310. if (trim32(result.l) < trim32(i1.l)) /* carry? */
  311. result.h++;
  312. return result;
  313. }
  314. /* return i * 5 */
  315. static Rand64 times5 (Rand64 i) {
  316. return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */
  317. }
  318. /* return i * 9 */
  319. static Rand64 times9 (Rand64 i) {
  320. return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */
  321. }
  322. /* return 'i' rotated left 'n' bits */
  323. static Rand64 rotl (Rand64 i, int n) {
  324. lua_assert(n > 0 && n < 32);
  325. return packI((i.h << n) | (trim32(i.l) >> (32 - n)),
  326. (trim32(i.h) >> (32 - n)) | (i.l << n));
  327. }
  328. /* for offsets larger than 32, rotate right by 64 - offset */
  329. static Rand64 rotl1 (Rand64 i, int n) {
  330. lua_assert(n > 32 && n < 64);
  331. n = 64 - n;
  332. return packI((trim32(i.h) >> n) | (i.l << (32 - n)),
  333. (i.h << (32 - n)) | (trim32(i.l) >> n));
  334. }
  335. /*
  336. ** implementation of 'xoshiro256**' algorithm on 'Rand64' values
  337. */
  338. static Rand64 nextrand (Rand64 *state) {
  339. Rand64 res = times9(rotl(times5(state[0]), 7));
  340. Rand64 t = Ishl(state[1], 17);
  341. Ixor(&state[2], state[0]);
  342. Ixor(&state[3], state[1]);
  343. Ixor(&state[1], state[2]);
  344. Ixor(&state[0], state[3]);
  345. Ixor(&state[2], t);
  346. state[3] = rotl1(state[3], 45);
  347. return res;
  348. }
  349. /*
  350. ** Converts a 'Rand64' into a float.
  351. */
  352. /* an unsigned 1 with proper type */
  353. #define UONE ((lu_int32)1)
  354. #if FIGS <= 32
  355. #define maskHI 0 /* do not need bits from higher half */
  356. #define maskLOW (~(~UONE << (FIGS - 1))) /* use FIGS bits */
  357. #define shiftFIG (l_mathop(0.5) / (UONE << (FIGS - 1))) /* 2^(-FIGS) */
  358. #else /* 32 < FIGS <= 64 */
  359. /* must take care to not shift stuff by more than 31 slots */
  360. /* use FIGS - 32 bits from higher half */
  361. #define maskHI (~(~UONE << (FIGS - 33)))
  362. /* use 32 bits from lower half */
  363. #define maskLOW (~(~UONE << 31))
  364. /* 2^(-FIGS) == (1 / 2^33) / 2^(FIGS-33) */
  365. #define shiftFIG ((lua_Number)(1.0 / 8589934592.0) / (UONE << (FIGS - 33)))
  366. #endif
  367. #define twoto32 l_mathop(4294967296.0) /* 2^32 */
  368. static lua_Number I2d (Rand64 x) {
  369. lua_Number h = (lua_Number)(x.h & maskHI);
  370. lua_Number l = (lua_Number)(x.l & maskLOW);
  371. return (h * twoto32 + l) * shiftFIG;
  372. }
  373. /* convert a 'Rand64' to a 'lua_Unsigned' */
  374. static lua_Unsigned I2UInt (Rand64 x) {
  375. return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l);
  376. }
  377. /* convert a 'lua_Unsigned' to a 'Rand64' */
  378. static Rand64 Int2I (lua_Unsigned n) {
  379. return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n);
  380. }
  381. #endif /* } */
  382. /*
  383. ** A state uses four 'Rand64' values.
  384. */
  385. typedef struct {
  386. Rand64 s[4];
  387. } RanState;
  388. /*
  389. ** Project the random integer 'ran' into the interval [0, n].
  390. ** Because 'ran' has 2^B possible values, the projection can only be
  391. ** uniform when the size of the interval is a power of 2 (exact
  392. ** division). To get a uniform projection into [0, n], we first compute
  393. ** 'lim', the smallest Mersenne number not smaller than 'n'. We then
  394. ** project 'ran' into the interval [0, lim]. If the result is inside
  395. ** [0, n], we are done. Otherwise, we try with another 'ran', until we
  396. ** have a result inside the interval.
  397. */
  398. static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
  399. RanState *state) {
  400. lua_Unsigned lim = n;
  401. if ((lim & (lim + 1)) > 0) { /* 'lim + 1' is not a power of 2? */
  402. /* compute the smallest (2^b - 1) not smaller than 'n' */
  403. lim |= (lim >> 1);
  404. lim |= (lim >> 2);
  405. lim |= (lim >> 4);
  406. lim |= (lim >> 8);
  407. lim |= (lim >> 16);
  408. #if (LUA_MAXINTEGER >> 30 >> 1) > 0
  409. lim |= (lim >> 32); /* integer type has more than 32 bits */
  410. #endif
  411. }
  412. lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */
  413. && lim >= n /* not smaller than 'n', */
  414. && (lim == 0 || (lim >> 1) < n)); /* and it is the smallest one */
  415. while ((ran &= lim) > n) /* project 'ran' into [0..lim] */
  416. ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */
  417. return ran;
  418. }
  419. static int math_random (lua_State *L) {
  420. lua_Integer low, up;
  421. lua_Unsigned p;
  422. RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
  423. Rand64 rv = nextrand(state->s); /* next pseudo-random value */
  424. switch (lua_gettop(L)) { /* check number of arguments */
  425. case 0: { /* no arguments */
  426. lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */
  427. return 1;
  428. }
  429. case 1: { /* only upper limit */
  430. low = 1;
  431. up = luaL_checkinteger(L, 1);
  432. if (up == 0) { /* single 0 as argument? */
  433. lua_pushinteger(L, I2UInt(rv)); /* full random integer */
  434. return 1;
  435. }
  436. break;
  437. }
  438. case 2: { /* lower and upper limits */
  439. low = luaL_checkinteger(L, 1);
  440. up = luaL_checkinteger(L, 2);
  441. break;
  442. }
  443. default: return luaL_error(L, "wrong number of arguments");
  444. }
  445. /* random integer in the interval [low, up] */
  446. luaL_argcheck(L, low <= up, 1, "interval is empty");
  447. /* project random integer into the interval [0, up - low] */
  448. p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);
  449. lua_pushinteger(L, p + (lua_Unsigned)low);
  450. return 1;
  451. }
  452. static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) {
  453. int i;
  454. state[0] = Int2I(n1);
  455. state[1] = Int2I(0xff); /* avoid a zero state */
  456. state[2] = Int2I(n2);
  457. state[3] = Int2I(0);
  458. for (i = 0; i < 16; i++)
  459. nextrand(state); /* discard initial values to "spread" seed */
  460. }
  461. static int math_randomseed (lua_State *L) {
  462. RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
  463. lua_Integer n1 = luaL_checkinteger(L, 1);
  464. lua_Integer n2 = luaL_optinteger(L, 2, 0);
  465. setseed(state->s, n1, n2);
  466. return 0;
  467. }
  468. static const luaL_Reg randfuncs[] = {
  469. {"random", math_random},
  470. {"randomseed", math_randomseed},
  471. {NULL, NULL}
  472. };
  473. static void setrandfunc (lua_State *L) {
  474. RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
  475. setseed(state->s, 0, 0);
  476. luaL_setfuncs(L, randfuncs, 1);
  477. }
  478. /* }================================================================== */
  479. /*
  480. ** {==================================================================
  481. ** Deprecated functions (for compatibility only)
  482. ** ===================================================================
  483. */
  484. #if defined(LUA_COMPAT_MATHLIB)
  485. static int math_cosh (lua_State *L) {
  486. lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
  487. return 1;
  488. }
  489. static int math_sinh (lua_State *L) {
  490. lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
  491. return 1;
  492. }
  493. static int math_tanh (lua_State *L) {
  494. lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
  495. return 1;
  496. }
  497. static int math_pow (lua_State *L) {
  498. lua_Number x = luaL_checknumber(L, 1);
  499. lua_Number y = luaL_checknumber(L, 2);
  500. lua_pushnumber(L, l_mathop(pow)(x, y));
  501. return 1;
  502. }
  503. static int math_frexp (lua_State *L) {
  504. int e;
  505. lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
  506. lua_pushinteger(L, e);
  507. return 2;
  508. }
  509. static int math_ldexp (lua_State *L) {
  510. lua_Number x = luaL_checknumber(L, 1);
  511. int ep = (int)luaL_checkinteger(L, 2);
  512. lua_pushnumber(L, l_mathop(ldexp)(x, ep));
  513. return 1;
  514. }
  515. static int math_log10 (lua_State *L) {
  516. lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
  517. return 1;
  518. }
  519. #endif
  520. /* }================================================================== */
  521. static const luaL_Reg mathlib[] = {
  522. {"abs", math_abs},
  523. {"acos", math_acos},
  524. {"asin", math_asin},
  525. {"atan", math_atan},
  526. {"ceil", math_ceil},
  527. {"cos", math_cos},
  528. {"deg", math_deg},
  529. {"exp", math_exp},
  530. {"tointeger", math_toint},
  531. {"floor", math_floor},
  532. {"fmod", math_fmod},
  533. {"ult", math_ult},
  534. {"log", math_log},
  535. {"max", math_max},
  536. {"min", math_min},
  537. {"modf", math_modf},
  538. {"rad", math_rad},
  539. {"sin", math_sin},
  540. {"sqrt", math_sqrt},
  541. {"tan", math_tan},
  542. {"type", math_type},
  543. #if defined(LUA_COMPAT_MATHLIB)
  544. {"atan2", math_atan},
  545. {"cosh", math_cosh},
  546. {"sinh", math_sinh},
  547. {"tanh", math_tanh},
  548. {"pow", math_pow},
  549. {"frexp", math_frexp},
  550. {"ldexp", math_ldexp},
  551. {"log10", math_log10},
  552. #endif
  553. /* placeholders */
  554. {"random", NULL},
  555. {"randomseed", NULL},
  556. {"pi", NULL},
  557. {"huge", NULL},
  558. {"maxinteger", NULL},
  559. {"mininteger", NULL},
  560. {NULL, NULL}
  561. };
  562. /*
  563. ** Open math library
  564. */
  565. LUAMOD_API int luaopen_math (lua_State *L) {
  566. luaL_newlib(L, mathlib);
  567. lua_pushnumber(L, PI);
  568. lua_setfield(L, -2, "pi");
  569. lua_pushnumber(L, (lua_Number)HUGE_VAL);
  570. lua_setfield(L, -2, "huge");
  571. lua_pushinteger(L, LUA_MAXINTEGER);
  572. lua_setfield(L, -2, "maxinteger");
  573. lua_pushinteger(L, LUA_MININTEGER);
  574. lua_setfield(L, -2, "mininteger");
  575. setrandfunc(L);
  576. return 1;
  577. }