lmathlib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. ** $Id: lmathlib.c,v 1.119 2016/12/22 13:08:50 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 <limits.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include "lua.h"
  13. #include "lauxlib.h"
  14. #include "lualib.h"
  15. #undef PI
  16. #define PI (l_mathop(3.141592653589793238462643383279502884))
  17. static int math_abs (lua_State *L) {
  18. if (lua_isinteger(L, 1)) {
  19. lua_Integer n = lua_tointeger(L, 1);
  20. if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
  21. lua_pushinteger(L, n);
  22. }
  23. else
  24. lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
  25. return 1;
  26. }
  27. static int math_sin (lua_State *L) {
  28. lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
  29. return 1;
  30. }
  31. static int math_cos (lua_State *L) {
  32. lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
  33. return 1;
  34. }
  35. static int math_tan (lua_State *L) {
  36. lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
  37. return 1;
  38. }
  39. static int math_asin (lua_State *L) {
  40. lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
  41. return 1;
  42. }
  43. static int math_acos (lua_State *L) {
  44. lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
  45. return 1;
  46. }
  47. static int math_atan (lua_State *L) {
  48. lua_Number y = luaL_checknumber(L, 1);
  49. lua_Number x = luaL_optnumber(L, 2, 1);
  50. lua_pushnumber(L, l_mathop(atan2)(y, x));
  51. return 1;
  52. }
  53. static int math_toint (lua_State *L) {
  54. int valid;
  55. lua_Integer n = lua_tointegerx(L, 1, &valid);
  56. if (valid)
  57. lua_pushinteger(L, n);
  58. else {
  59. luaL_checkany(L, 1);
  60. lua_pushnil(L); /* value is not convertible to integer */
  61. }
  62. return 1;
  63. }
  64. static void pushnumint (lua_State *L, lua_Number d) {
  65. lua_Integer n;
  66. if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
  67. lua_pushinteger(L, n); /* result is integer */
  68. else
  69. lua_pushnumber(L, d); /* result is float */
  70. }
  71. static int math_floor (lua_State *L) {
  72. if (lua_isinteger(L, 1))
  73. lua_settop(L, 1); /* integer is its own floor */
  74. else {
  75. lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
  76. pushnumint(L, d);
  77. }
  78. return 1;
  79. }
  80. static int math_ceil (lua_State *L) {
  81. if (lua_isinteger(L, 1))
  82. lua_settop(L, 1); /* integer is its own ceil */
  83. else {
  84. lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
  85. pushnumint(L, d);
  86. }
  87. return 1;
  88. }
  89. static int math_fmod (lua_State *L) {
  90. if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
  91. lua_Integer d = lua_tointeger(L, 2);
  92. if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
  93. luaL_argcheck(L, d != 0, 2, "zero");
  94. lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
  95. }
  96. else
  97. lua_pushinteger(L, lua_tointeger(L, 1) % d);
  98. }
  99. else
  100. lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
  101. luaL_checknumber(L, 2)));
  102. return 1;
  103. }
  104. /*
  105. ** next function does not use 'modf', avoiding problems with 'double*'
  106. ** (which is not compatible with 'float*') when lua_Number is not
  107. ** 'double'.
  108. */
  109. static int math_modf (lua_State *L) {
  110. if (lua_isinteger(L ,1)) {
  111. lua_settop(L, 1); /* number is its own integer part */
  112. lua_pushnumber(L, 0); /* no fractional part */
  113. }
  114. else {
  115. lua_Number n = luaL_checknumber(L, 1);
  116. /* integer part (rounds toward zero) */
  117. lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
  118. pushnumint(L, ip);
  119. /* fractional part (test needed for inf/-inf) */
  120. lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
  121. }
  122. return 2;
  123. }
  124. static int math_sqrt (lua_State *L) {
  125. lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
  126. return 1;
  127. }
  128. static int math_ult (lua_State *L) {
  129. lua_Integer a = luaL_checkinteger(L, 1);
  130. lua_Integer b = luaL_checkinteger(L, 2);
  131. lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
  132. return 1;
  133. }
  134. static int math_log (lua_State *L) {
  135. lua_Number x = luaL_checknumber(L, 1);
  136. lua_Number res;
  137. if (lua_isnoneornil(L, 2))
  138. res = l_mathop(log)(x);
  139. else {
  140. lua_Number base = luaL_checknumber(L, 2);
  141. #if !defined(LUA_USE_C89)
  142. if (base == l_mathop(2.0))
  143. res = l_mathop(log2)(x); else
  144. #endif
  145. if (base == l_mathop(10.0))
  146. res = l_mathop(log10)(x);
  147. else
  148. res = l_mathop(log)(x)/l_mathop(log)(base);
  149. }
  150. lua_pushnumber(L, res);
  151. return 1;
  152. }
  153. static int math_exp (lua_State *L) {
  154. lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
  155. return 1;
  156. }
  157. static int math_deg (lua_State *L) {
  158. lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
  159. return 1;
  160. }
  161. static int math_rad (lua_State *L) {
  162. lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
  163. return 1;
  164. }
  165. static int math_min (lua_State *L) {
  166. int n = lua_gettop(L); /* number of arguments */
  167. int imin = 1; /* index of current minimum value */
  168. int i;
  169. luaL_argcheck(L, n >= 1, 1, "value expected");
  170. for (i = 2; i <= n; i++) {
  171. if (lua_compare(L, i, imin, LUA_OPLT))
  172. imin = i;
  173. }
  174. lua_pushvalue(L, imin);
  175. return 1;
  176. }
  177. static int math_max (lua_State *L) {
  178. int n = lua_gettop(L); /* number of arguments */
  179. int imax = 1; /* index of current maximum value */
  180. int i;
  181. luaL_argcheck(L, n >= 1, 1, "value expected");
  182. for (i = 2; i <= n; i++) {
  183. if (lua_compare(L, imax, i, LUA_OPLT))
  184. imax = i;
  185. }
  186. lua_pushvalue(L, imax);
  187. return 1;
  188. }
  189. static int math_type (lua_State *L) {
  190. if (lua_type(L, 1) == LUA_TNUMBER) {
  191. if (lua_isinteger(L, 1))
  192. lua_pushliteral(L, "integer");
  193. else
  194. lua_pushliteral(L, "float");
  195. }
  196. else {
  197. luaL_checkany(L, 1);
  198. lua_pushnil(L);
  199. }
  200. return 1;
  201. }
  202. /*
  203. ** {==================================================================
  204. ** Pseudo-Random Number Generator based on 'xorshift128+'.
  205. ** ===================================================================
  206. */
  207. #define twotomin53 (1.0 / 9007199254740992.0) /* 2^-53 */
  208. #if defined(LLONG_MAX) && !defined(LUA_DEBUG) /* { */
  209. /*
  210. ** Assume long long.
  211. */
  212. /* a 64-bit value */
  213. typedef unsigned long long I;
  214. static I xorshift128plus (I *state) {
  215. I x = state[0];
  216. I y = state[1];
  217. state[0] = y;
  218. x ^= x << 23;
  219. state[1] = x ^ y ^ (x >> 18) ^ (y >> 5);
  220. return state[1] + y;
  221. }
  222. #define mask53 (~(~0ll << 53))
  223. /*
  224. ** Convert 53 bits from a random integer into a double in the
  225. ** interval [0,1).
  226. */
  227. static double I2d (I x) {
  228. return (x & mask53) * twotomin53;
  229. }
  230. /* convert an 'I' to a lua_Integer */
  231. #define I2Int(x) ((lua_Integer)(x))
  232. /* convert a lua_Integer to an 'I' */
  233. #define Int2I(x) ((I)(x))
  234. #else /* }{ */
  235. /*
  236. ** No long long; Use two 32-bit integers to represent a 64-bit quantity.
  237. */
  238. #if LUAI_BITSINT >= 32
  239. typedef unsigned int lu_int32;
  240. #else
  241. typedef unsigned long lu_int32;
  242. #endif
  243. /* a 64-bit value */
  244. typedef struct I {
  245. lu_int32 x1, x2;
  246. } I;
  247. /*
  248. ** basic operations on 'I' values
  249. */
  250. static I pack (int x1, int x2) {
  251. I result;
  252. result.x1 = x1;
  253. result.x2 = x2;
  254. return result;
  255. }
  256. static I Ishl (I i, int n) {
  257. return pack((i.x1 << n) | (i.x2 >> (32 - n)), i.x2 << n);
  258. }
  259. static I Ishr (I i, int n) {
  260. return pack(i.x1 >> n, (i.x2 >> n) | (i.x1 << (32 - n)));
  261. }
  262. static I Ixor (I i1, I i2) {
  263. return pack(i1.x1 ^ i2.x1, i1.x2 ^ i2.x2);
  264. }
  265. static I Iadd (I i1, I i2) {
  266. I result = pack(i1.x1 + i2.x1, i1.x2 + i2.x2);
  267. if (result.x2 < i1.x2) /* carry? */
  268. result.x1++;
  269. return result;
  270. }
  271. /*
  272. ** implementation of 'xorshift128+' algorithm on 'I' values
  273. */
  274. static I xorshift128plus (I *state) {
  275. I x = state[0];
  276. I y = state[1];
  277. state[0] = y;
  278. x = Ixor(x, Ishl(x, 23)); /* x ^= x << 23; */
  279. /* s[1] = x ^ y ^ (x >> 18) ^ (y >> 5); */
  280. state[1] = Ixor(Ixor(Ixor(x, y), Ishr(x, 18)), Ishr(y, 5));
  281. return Iadd(state[1], y); /* return state[1] + y; */
  282. }
  283. /*
  284. ** Converts an 'I' into a double, getting its lower half plus 21
  285. ** (53 - 32) bits from its higher half and joining them into a double.
  286. */
  287. #define mask32 0xffffffff
  288. #define mask21 (~(~0 << 21))
  289. #define twoto32 4294967296.0 /* 2^32 */
  290. static double I2d (I x) {
  291. return ((x.x1 & mask21) * twoto32 + (x.x2 & mask32)) * twotomin53;
  292. }
  293. static lua_Integer I2Int (I x) {
  294. return (((lua_Integer)x.x1 << 31) << 1) | x.x2;
  295. }
  296. static I Int2I (lua_Integer n) {
  297. return pack(n, (n >> 31) >> 1);
  298. }
  299. #endif /* } */
  300. /*
  301. ** A state uses two 'I' values.
  302. */
  303. typedef struct {
  304. I s[2];
  305. } RanState;
  306. static int math_random (lua_State *L) {
  307. lua_Integer low, up;
  308. double r;
  309. RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
  310. I rv = xorshift128plus(state->s); /* next pseudo-random value */
  311. switch (lua_gettop(L)) { /* check number of arguments */
  312. case 0: { /* no arguments */
  313. lua_pushnumber(L, (lua_Number)I2d(rv)); /* float between 0 and 1 */
  314. return 1;
  315. }
  316. case 1: { /* only upper limit */
  317. low = 1;
  318. up = luaL_checkinteger(L, 1);
  319. if (up == 0) { /* single 0 as argument? */
  320. lua_pushinteger(L, I2Int(rv)); /* full random integer */
  321. return 1;
  322. }
  323. break;
  324. }
  325. case 2: { /* lower and upper limits */
  326. low = luaL_checkinteger(L, 1);
  327. up = luaL_checkinteger(L, 2);
  328. break;
  329. }
  330. default: return luaL_error(L, "wrong number of arguments");
  331. }
  332. /* random integer in the interval [low, up] */
  333. luaL_argcheck(L, low <= up, 1, "interval is empty");
  334. luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1,
  335. "interval too large");
  336. r = I2d(rv); /* convert random value to a double */
  337. r *= (double)(up - low) + 1.0; /* scale it */
  338. lua_pushinteger(L, (lua_Integer)r + low);
  339. return 1;
  340. }
  341. static void setseed (I *state, lua_Integer n) {
  342. int i;
  343. state[0] = Int2I(n);
  344. state[1] = Int2I(~n);
  345. for (i = 0; i < 16; i++)
  346. xorshift128plus(state); /* discard initial values */
  347. }
  348. static int math_randomseed (lua_State *L) {
  349. RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
  350. lua_Integer n = luaL_checkinteger(L, 1);
  351. setseed(state->s, n);
  352. return 0;
  353. }
  354. static const luaL_Reg randfuncs[] = {
  355. {"random", math_random},
  356. {"randomseed", math_randomseed},
  357. {NULL, NULL}
  358. };
  359. static void setrandfunc (lua_State *L) {
  360. RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
  361. setseed(state->s, 0);
  362. luaL_setfuncs(L, randfuncs, 1);
  363. }
  364. /* }================================================================== */
  365. /*
  366. ** {==================================================================
  367. ** Deprecated functions (for compatibility only)
  368. ** ===================================================================
  369. */
  370. #if defined(LUA_COMPAT_MATHLIB)
  371. static int math_cosh (lua_State *L) {
  372. lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
  373. return 1;
  374. }
  375. static int math_sinh (lua_State *L) {
  376. lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
  377. return 1;
  378. }
  379. static int math_tanh (lua_State *L) {
  380. lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
  381. return 1;
  382. }
  383. static int math_pow (lua_State *L) {
  384. lua_Number x = luaL_checknumber(L, 1);
  385. lua_Number y = luaL_checknumber(L, 2);
  386. lua_pushnumber(L, l_mathop(pow)(x, y));
  387. return 1;
  388. }
  389. static int math_frexp (lua_State *L) {
  390. int e;
  391. lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
  392. lua_pushinteger(L, e);
  393. return 2;
  394. }
  395. static int math_ldexp (lua_State *L) {
  396. lua_Number x = luaL_checknumber(L, 1);
  397. int ep = (int)luaL_checkinteger(L, 2);
  398. lua_pushnumber(L, l_mathop(ldexp)(x, ep));
  399. return 1;
  400. }
  401. static int math_log10 (lua_State *L) {
  402. lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
  403. return 1;
  404. }
  405. #endif
  406. /* }================================================================== */
  407. static const luaL_Reg mathlib[] = {
  408. {"abs", math_abs},
  409. {"acos", math_acos},
  410. {"asin", math_asin},
  411. {"atan", math_atan},
  412. {"ceil", math_ceil},
  413. {"cos", math_cos},
  414. {"deg", math_deg},
  415. {"exp", math_exp},
  416. {"tointeger", math_toint},
  417. {"floor", math_floor},
  418. {"fmod", math_fmod},
  419. {"ult", math_ult},
  420. {"log", math_log},
  421. {"max", math_max},
  422. {"min", math_min},
  423. {"modf", math_modf},
  424. {"rad", math_rad},
  425. {"sin", math_sin},
  426. {"sqrt", math_sqrt},
  427. {"tan", math_tan},
  428. {"type", math_type},
  429. #if defined(LUA_COMPAT_MATHLIB)
  430. {"atan2", math_atan},
  431. {"cosh", math_cosh},
  432. {"sinh", math_sinh},
  433. {"tanh", math_tanh},
  434. {"pow", math_pow},
  435. {"frexp", math_frexp},
  436. {"ldexp", math_ldexp},
  437. {"log10", math_log10},
  438. #endif
  439. /* placeholders */
  440. {"random", NULL},
  441. {"randomseed", NULL},
  442. {"pi", NULL},
  443. {"huge", NULL},
  444. {"maxinteger", NULL},
  445. {"mininteger", NULL},
  446. {NULL, NULL}
  447. };
  448. /*
  449. ** Open math library
  450. */
  451. LUAMOD_API int luaopen_math (lua_State *L) {
  452. luaL_newlib(L, mathlib);
  453. lua_pushnumber(L, PI);
  454. lua_setfield(L, -2, "pi");
  455. lua_pushnumber(L, (lua_Number)HUGE_VAL);
  456. lua_setfield(L, -2, "huge");
  457. lua_pushinteger(L, LUA_MAXINTEGER);
  458. lua_setfield(L, -2, "maxinteger");
  459. lua_pushinteger(L, LUA_MININTEGER);
  460. lua_setfield(L, -2, "mininteger");
  461. setrandfunc(L);
  462. return 1;
  463. }