lmathlib.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. ** $Id: lmathlib.c,v 1.99 2014/05/02 16:36:51 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_ceil (lua_State *L) {
  61. lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));
  62. return 1;
  63. }
  64. static int math_floor (lua_State *L) {
  65. lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
  66. return 1;
  67. }
  68. static int math_ifloor (lua_State *L) {
  69. int valid;
  70. lua_Integer n = lua_tointegerx(L, 1, &valid);
  71. if (valid)
  72. lua_pushinteger(L, n);
  73. else {
  74. luaL_checktype(L, 1, LUA_TNUMBER); /* error if not a number */
  75. lua_pushnil(L); /* number with invalid integer value */
  76. }
  77. return 1;
  78. }
  79. static int math_fmod (lua_State *L) {
  80. if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
  81. lua_Integer d = lua_tointeger(L, 2);
  82. if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
  83. luaL_argcheck(L, d != 0, 2, "zero");
  84. lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
  85. }
  86. else
  87. lua_pushinteger(L, lua_tointeger(L, 1) % d);
  88. }
  89. else
  90. lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
  91. luaL_checknumber(L, 2)));
  92. return 1;
  93. }
  94. /*
  95. ** next function does not use 'modf', avoiding problems with 'double*'
  96. ** (which is not compatible with 'float*') when lua_Number is not
  97. ** 'double'.
  98. */
  99. static int math_modf (lua_State *L) {
  100. lua_Number n = luaL_checknumber(L, 1);
  101. /* integer part (rounds toward zero) */
  102. lua_Number ip = (n < 0) ? -l_mathop(floor)(-n) : l_mathop(floor)(n);
  103. lua_pushnumber(L, ip);
  104. /* fractionary part (test needed for inf/-inf) */
  105. lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip));
  106. return 2;
  107. }
  108. static int math_sqrt (lua_State *L) {
  109. lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
  110. return 1;
  111. }
  112. static int math_log (lua_State *L) {
  113. lua_Number x = luaL_checknumber(L, 1);
  114. lua_Number res;
  115. if (lua_isnoneornil(L, 2))
  116. res = l_mathop(log)(x);
  117. else {
  118. lua_Number base = luaL_checknumber(L, 2);
  119. if (base == 10.0) res = l_mathop(log10)(x);
  120. else res = l_mathop(log)(x)/l_mathop(log)(base);
  121. }
  122. lua_pushnumber(L, res);
  123. return 1;
  124. }
  125. static int math_exp (lua_State *L) {
  126. lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
  127. return 1;
  128. }
  129. static int math_deg (lua_State *L) {
  130. lua_pushnumber(L, luaL_checknumber(L, 1) * (180.0 / PI));
  131. return 1;
  132. }
  133. static int math_rad (lua_State *L) {
  134. lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / 180.0));
  135. return 1;
  136. }
  137. static int math_min (lua_State *L) {
  138. int n = lua_gettop(L); /* number of arguments */
  139. int imin = 1; /* index of current minimum value */
  140. int i;
  141. luaL_argcheck(L, n >= 1, 1, "value expected");
  142. for (i = 2; i <= n; i++) {
  143. if (lua_compare(L, i, imin, LUA_OPLT))
  144. imin = i;
  145. }
  146. lua_pushvalue(L, imin);
  147. return 1;
  148. }
  149. static int math_max (lua_State *L) {
  150. int n = lua_gettop(L); /* number of arguments */
  151. int imax = 1; /* index of current maximum value */
  152. int i;
  153. luaL_argcheck(L, n >= 1, 1, "value expected");
  154. for (i = 2; i <= n; i++) {
  155. if (lua_compare(L, imax, i, LUA_OPLT))
  156. imax = i;
  157. }
  158. lua_pushvalue(L, imax);
  159. return 1;
  160. }
  161. /*
  162. ** This function uses 'double' (instead of 'lua_Number') to ensure that
  163. ** all bits from 'l_rand' can be represented, and that 'RAND_MAX + 1.0'
  164. ** will keep full precision (ensuring that 'r' is always less than 1.0.)
  165. */
  166. static int math_random (lua_State *L) {
  167. lua_Integer low, up;
  168. double r = (double)l_rand() * (1.0 / ((double)RAND_MAX + 1.0));
  169. switch (lua_gettop(L)) { /* check number of arguments */
  170. case 0: { /* no arguments */
  171. lua_pushnumber(L, r); /* Number between 0 and 1 */
  172. return 1;
  173. }
  174. case 1: { /* only upper limit */
  175. low = 1;
  176. up = luaL_checkinteger(L, 1);
  177. break;
  178. }
  179. case 2: { /* lower and upper limits */
  180. low = luaL_checkinteger(L, 1);
  181. up = luaL_checkinteger(L, 2);
  182. break;
  183. }
  184. default: return luaL_error(L, "wrong number of arguments");
  185. }
  186. /* random integer in the interval [low, up] */
  187. luaL_argcheck(L, low <= up, 1, "interval is empty");
  188. luaL_argcheck(L, (lua_Unsigned)up - low <= (lua_Unsigned)LUA_MAXINTEGER,
  189. 1, "interval too large");
  190. r *= (double)(up - low) + 1.0;
  191. lua_pushinteger(L, (lua_Integer)r + low);
  192. return 1;
  193. }
  194. static int math_randomseed (lua_State *L) {
  195. l_srand((unsigned int)luaL_checkunsigned(L, 1));
  196. (void)rand(); /* discard first value to avoid undesirable correlations */
  197. return 0;
  198. }
  199. static int math_type (lua_State *L) {
  200. luaL_checkany(L, 1);
  201. if (lua_type(L, 1) == LUA_TNUMBER) {
  202. if (lua_isinteger(L, 1))
  203. lua_pushliteral(L, "integer");
  204. else
  205. lua_pushliteral(L, "float");
  206. }
  207. else
  208. lua_pushnil(L);
  209. return 1;
  210. }
  211. /*
  212. ** {==================================================================
  213. ** Deprecated functions (for compatibility only)
  214. ** ===================================================================
  215. */
  216. #if defined(LUA_COMPAT_MATHLIB)
  217. static int math_cosh (lua_State *L) {
  218. lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
  219. return 1;
  220. }
  221. static int math_sinh (lua_State *L) {
  222. lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
  223. return 1;
  224. }
  225. static int math_tanh (lua_State *L) {
  226. lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
  227. return 1;
  228. }
  229. static int math_pow (lua_State *L) {
  230. lua_Number x = luaL_checknumber(L, 1);
  231. lua_Number y = luaL_checknumber(L, 2);
  232. lua_pushnumber(L, l_mathop(pow)(x, y));
  233. return 1;
  234. }
  235. static int math_frexp (lua_State *L) {
  236. int e;
  237. lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
  238. lua_pushinteger(L, e);
  239. return 2;
  240. }
  241. static int math_ldexp (lua_State *L) {
  242. lua_Number x = luaL_checknumber(L, 1);
  243. int ep = luaL_checkint(L, 2);
  244. lua_pushnumber(L, l_mathop(ldexp)(x, ep));
  245. return 1;
  246. }
  247. static int math_log10 (lua_State *L) {
  248. lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
  249. return 1;
  250. }
  251. #endif
  252. /* }================================================================== */
  253. static const luaL_Reg mathlib[] = {
  254. {"abs", math_abs},
  255. {"acos", math_acos},
  256. {"asin", math_asin},
  257. {"atan", math_atan},
  258. {"ceil", math_ceil},
  259. {"cos", math_cos},
  260. {"deg", math_deg},
  261. {"exp", math_exp},
  262. {"floor", math_floor},
  263. {"ifloor", math_ifloor},
  264. {"fmod", math_fmod},
  265. {"log", math_log},
  266. {"max", math_max},
  267. {"min", math_min},
  268. {"modf", math_modf},
  269. {"rad", math_rad},
  270. {"random", math_random},
  271. {"randomseed", math_randomseed},
  272. {"sin", math_sin},
  273. {"sqrt", math_sqrt},
  274. {"tan", math_tan},
  275. {"type", math_type},
  276. #if defined(LUA_COMPAT_MATHLIB)
  277. {"atan2", math_atan},
  278. {"cosh", math_cosh},
  279. {"sinh", math_sinh},
  280. {"tanh", math_tanh},
  281. {"pow", math_pow},
  282. {"frexp", math_frexp},
  283. {"ldexp", math_ldexp},
  284. {"log10", math_log10},
  285. #endif
  286. {NULL, NULL}
  287. };
  288. /*
  289. ** Open math library
  290. */
  291. LUAMOD_API int luaopen_math (lua_State *L) {
  292. luaL_newlib(L, mathlib);
  293. lua_pushnumber(L, PI);
  294. lua_setfield(L, -2, "pi");
  295. lua_pushnumber(L, HUGE_VAL);
  296. lua_setfield(L, -2, "huge");
  297. lua_pushinteger(L, LUA_MAXINTEGER);
  298. lua_setfield(L, -2, "maxinteger");
  299. lua_pushinteger(L, LUA_MININTEGER);
  300. lua_setfield(L, -2, "mininteger");
  301. return 1;
  302. }