lmathlib.c 19 KB

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