lmathlib.c 9.2 KB

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