lmathlib.c 8.6 KB

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