lmathlib.c 18 KB

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