lmathlib.c 9.5 KB

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