lmathlib.c 19 KB

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