2
0

lmathlib.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. ** $Id: lmathlib.c,v 1.116 2015/06/26 19:30:32 roberto Exp $
  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 <stdlib.h>
  10. #include <math.h>
  11. #include "lua.h"
  12. #include "lauxlib.h"
  13. #include "lualib.h"
  14. #undef PI
  15. #define PI (l_mathop(3.141592653589793238462643383279502884))
  16. #if !defined(l_rand) /* { */
  17. #if defined(LUA_USE_POSIX)
  18. #define l_rand() random()
  19. #define l_srand(x) srandom(x)
  20. #define L_RANDMAX 2147483647 /* (2^31 - 1), following POSIX */
  21. #else
  22. #define l_rand() rand()
  23. #define l_srand(x) srand(x)
  24. #define L_RANDMAX RAND_MAX
  25. #endif
  26. #endif /* } */
  27. static int math_abs (lua_State *L) {
  28. if (lua_isinteger(L, 1)) {
  29. lua_Integer n = lua_tointeger(L, 1);
  30. if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
  31. lua_pushinteger(L, n);
  32. }
  33. else
  34. lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
  35. return 1;
  36. }
  37. static int math_sin (lua_State *L) {
  38. lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
  39. return 1;
  40. }
  41. static int math_cos (lua_State *L) {
  42. lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
  43. return 1;
  44. }
  45. static int math_tan (lua_State *L) {
  46. lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
  47. return 1;
  48. }
  49. static int math_asin (lua_State *L) {
  50. lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
  51. return 1;
  52. }
  53. static int math_acos (lua_State *L) {
  54. lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
  55. return 1;
  56. }
  57. static int math_atan (lua_State *L) {
  58. lua_Number y = luaL_checknumber(L, 1);
  59. lua_Number x = luaL_optnumber(L, 2, 1);
  60. lua_pushnumber(L, l_mathop(atan2)(y, x));
  61. return 1;
  62. }
  63. static int math_toint (lua_State *L) {
  64. int valid;
  65. lua_Integer n = lua_tointegerx(L, 1, &valid);
  66. if (valid)
  67. lua_pushinteger(L, n);
  68. else {
  69. luaL_checkany(L, 1);
  70. lua_pushnil(L); /* value is not convertible to integer */
  71. }
  72. return 1;
  73. }
  74. static void pushnumint (lua_State *L, lua_Number d) {
  75. lua_Integer n;
  76. if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
  77. lua_pushinteger(L, n); /* result is integer */
  78. else
  79. lua_pushnumber(L, d); /* result is float */
  80. }
  81. static int math_floor (lua_State *L) {
  82. if (lua_isinteger(L, 1))
  83. lua_settop(L, 1); /* integer is its own floor */
  84. else {
  85. lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
  86. pushnumint(L, d);
  87. }
  88. return 1;
  89. }
  90. static int math_ceil (lua_State *L) {
  91. if (lua_isinteger(L, 1))
  92. lua_settop(L, 1); /* integer is its own ceil */
  93. else {
  94. lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
  95. pushnumint(L, d);
  96. }
  97. return 1;
  98. }
  99. static int math_fmod (lua_State *L) {
  100. if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
  101. lua_Integer d = lua_tointeger(L, 2);
  102. if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
  103. luaL_argcheck(L, d != 0, 2, "zero");
  104. lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
  105. }
  106. else
  107. lua_pushinteger(L, lua_tointeger(L, 1) % d);
  108. }
  109. else
  110. lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
  111. luaL_checknumber(L, 2)));
  112. return 1;
  113. }
  114. /*
  115. ** next function does not use 'modf', avoiding problems with 'double*'
  116. ** (which is not compatible with 'float*') when lua_Number is not
  117. ** 'double'.
  118. */
  119. static int math_modf (lua_State *L) {
  120. if (lua_isinteger(L ,1)) {
  121. lua_settop(L, 1); /* number is its own integer part */
  122. lua_pushnumber(L, 0); /* no fractional part */
  123. }
  124. else {
  125. lua_Number n = luaL_checknumber(L, 1);
  126. /* integer part (rounds toward zero) */
  127. lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
  128. pushnumint(L, ip);
  129. /* fractional part (test needed for inf/-inf) */
  130. lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
  131. }
  132. return 2;
  133. }
  134. static int math_sqrt (lua_State *L) {
  135. lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
  136. return 1;
  137. }
  138. static int math_ult (lua_State *L) {
  139. lua_Integer a = luaL_checkinteger(L, 1);
  140. lua_Integer b = luaL_checkinteger(L, 2);
  141. lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
  142. return 1;
  143. }
  144. static int math_log (lua_State *L) {
  145. lua_Number x = luaL_checknumber(L, 1);
  146. lua_Number res;
  147. if (lua_isnoneornil(L, 2))
  148. res = l_mathop(log)(x);
  149. else {
  150. lua_Number base = luaL_checknumber(L, 2);
  151. #if !defined(LUA_USE_C89)
  152. if (base == 2.0) res = l_mathop(log2)(x); else
  153. #endif
  154. if (base == 10.0) res = l_mathop(log10)(x);
  155. else res = l_mathop(log)(x)/l_mathop(log)(base);
  156. }
  157. lua_pushnumber(L, res);
  158. return 1;
  159. }
  160. static int math_exp (lua_State *L) {
  161. lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
  162. return 1;
  163. }
  164. static int math_deg (lua_State *L) {
  165. lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
  166. return 1;
  167. }
  168. static int math_rad (lua_State *L) {
  169. lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
  170. return 1;
  171. }
  172. static int math_min (lua_State *L) {
  173. int n = lua_gettop(L); /* number of arguments */
  174. int imin = 1; /* index of current minimum value */
  175. int i;
  176. luaL_argcheck(L, n >= 1, 1, "value expected");
  177. for (i = 2; i <= n; i++) {
  178. if (lua_compare(L, i, imin, LUA_OPLT))
  179. imin = i;
  180. }
  181. lua_pushvalue(L, imin);
  182. return 1;
  183. }
  184. static int math_max (lua_State *L) {
  185. int n = lua_gettop(L); /* number of arguments */
  186. int imax = 1; /* index of current maximum value */
  187. int i;
  188. luaL_argcheck(L, n >= 1, 1, "value expected");
  189. for (i = 2; i <= n; i++) {
  190. if (lua_compare(L, imax, i, LUA_OPLT))
  191. imax = i;
  192. }
  193. lua_pushvalue(L, imax);
  194. return 1;
  195. }
  196. /*
  197. ** This function uses 'double' (instead of 'lua_Number') to ensure that
  198. ** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0'
  199. ** will keep full precision (ensuring that 'r' is always less than 1.0.)
  200. */
  201. static int math_random (lua_State *L) {
  202. lua_Integer low, up;
  203. double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0));
  204. switch (lua_gettop(L)) { /* check number of arguments */
  205. case 0: { /* no arguments */
  206. lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */
  207. return 1;
  208. }
  209. case 1: { /* only upper limit */
  210. low = 1;
  211. up = luaL_checkinteger(L, 1);
  212. break;
  213. }
  214. case 2: { /* lower and upper limits */
  215. low = luaL_checkinteger(L, 1);
  216. up = luaL_checkinteger(L, 2);
  217. break;
  218. }
  219. default: return luaL_error(L, "wrong number of arguments");
  220. }
  221. /* random integer in the interval [low, up] */
  222. luaL_argcheck(L, low <= up, 1, "interval is empty");
  223. luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1,
  224. "interval too large");
  225. r *= (double)(up - low) + 1.0;
  226. lua_pushinteger(L, (lua_Integer)r + low);
  227. return 1;
  228. }
  229. static int math_randomseed (lua_State *L) {
  230. l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1));
  231. (void)l_rand(); /* discard first value to avoid undesirable correlations */
  232. return 0;
  233. }
  234. static int math_type (lua_State *L) {
  235. if (lua_type(L, 1) == LUA_TNUMBER) {
  236. if (lua_isinteger(L, 1))
  237. lua_pushliteral(L, "integer");
  238. else
  239. lua_pushliteral(L, "float");
  240. }
  241. else {
  242. luaL_checkany(L, 1);
  243. lua_pushnil(L);
  244. }
  245. return 1;
  246. }
  247. /*
  248. ** {==================================================================
  249. ** Deprecated functions (for compatibility only)
  250. ** ===================================================================
  251. */
  252. #if defined(LUA_COMPAT_MATHLIB)
  253. static int math_cosh (lua_State *L) {
  254. lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
  255. return 1;
  256. }
  257. static int math_sinh (lua_State *L) {
  258. lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
  259. return 1;
  260. }
  261. static int math_tanh (lua_State *L) {
  262. lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
  263. return 1;
  264. }
  265. static int math_pow (lua_State *L) {
  266. lua_Number x = luaL_checknumber(L, 1);
  267. lua_Number y = luaL_checknumber(L, 2);
  268. lua_pushnumber(L, l_mathop(pow)(x, y));
  269. return 1;
  270. }
  271. static int math_frexp (lua_State *L) {
  272. int e;
  273. lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
  274. lua_pushinteger(L, e);
  275. return 2;
  276. }
  277. static int math_ldexp (lua_State *L) {
  278. lua_Number x = luaL_checknumber(L, 1);
  279. int ep = (int)luaL_checkinteger(L, 2);
  280. lua_pushnumber(L, l_mathop(ldexp)(x, ep));
  281. return 1;
  282. }
  283. static int math_log10 (lua_State *L) {
  284. lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
  285. return 1;
  286. }
  287. #endif
  288. /* }================================================================== */
  289. static const luaL_Reg mathlib[] = {
  290. {"abs", math_abs},
  291. {"acos", math_acos},
  292. {"asin", math_asin},
  293. {"atan", math_atan},
  294. {"ceil", math_ceil},
  295. {"cos", math_cos},
  296. {"deg", math_deg},
  297. {"exp", math_exp},
  298. {"tointeger", math_toint},
  299. {"floor", math_floor},
  300. {"fmod", math_fmod},
  301. {"ult", math_ult},
  302. {"log", math_log},
  303. {"max", math_max},
  304. {"min", math_min},
  305. {"modf", math_modf},
  306. {"rad", math_rad},
  307. {"random", math_random},
  308. {"randomseed", math_randomseed},
  309. {"sin", math_sin},
  310. {"sqrt", math_sqrt},
  311. {"tan", math_tan},
  312. {"type", math_type},
  313. #if defined(LUA_COMPAT_MATHLIB)
  314. {"atan2", math_atan},
  315. {"cosh", math_cosh},
  316. {"sinh", math_sinh},
  317. {"tanh", math_tanh},
  318. {"pow", math_pow},
  319. {"frexp", math_frexp},
  320. {"ldexp", math_ldexp},
  321. {"log10", math_log10},
  322. #endif
  323. /* placeholders */
  324. {"pi", NULL},
  325. {"huge", NULL},
  326. {"maxinteger", NULL},
  327. {"mininteger", NULL},
  328. {NULL, NULL}
  329. };
  330. /*
  331. ** Open math library
  332. */
  333. LUAMOD_API int luaopen_math (lua_State *L) {
  334. luaL_newlib(L, mathlib);
  335. lua_pushnumber(L, PI);
  336. lua_setfield(L, -2, "pi");
  337. lua_pushnumber(L, (lua_Number)HUGE_VAL);
  338. lua_setfield(L, -2, "huge");
  339. lua_pushinteger(L, LUA_MAXINTEGER);
  340. lua_setfield(L, -2, "maxinteger");
  341. lua_pushinteger(L, LUA_MININTEGER);
  342. lua_setfield(L, -2, "mininteger");
  343. return 1;
  344. }