lmathlib.c 8.8 KB

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