lmathlib.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. ** $Id: lmathlib.c,v 1.107 2014/07/17 12:30:53 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_ult (lua_State *L) {
  136. lua_Integer a = luaL_checkinteger(L, 1);
  137. lua_Integer b = luaL_checkinteger(L, 2);
  138. lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
  139. return 1;
  140. }
  141. static int math_log (lua_State *L) {
  142. lua_Number x = luaL_checknumber(L, 1);
  143. lua_Number res;
  144. if (lua_isnoneornil(L, 2))
  145. res = l_mathop(log)(x);
  146. else {
  147. lua_Number base = luaL_checknumber(L, 2);
  148. if (base == 10.0) res = l_mathop(log10)(x);
  149. else res = l_mathop(log)(x)/l_mathop(log)(base);
  150. }
  151. lua_pushnumber(L, res);
  152. return 1;
  153. }
  154. static int math_exp (lua_State *L) {
  155. lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
  156. return 1;
  157. }
  158. static int math_deg (lua_State *L) {
  159. lua_pushnumber(L, luaL_checknumber(L, 1) * (180.0 / PI));
  160. return 1;
  161. }
  162. static int math_rad (lua_State *L) {
  163. lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / 180.0));
  164. return 1;
  165. }
  166. static int math_min (lua_State *L) {
  167. int n = lua_gettop(L); /* number of arguments */
  168. int imin = 1; /* index of current minimum value */
  169. int i;
  170. luaL_argcheck(L, n >= 1, 1, "value expected");
  171. for (i = 2; i <= n; i++) {
  172. if (lua_compare(L, i, imin, LUA_OPLT))
  173. imin = i;
  174. }
  175. lua_pushvalue(L, imin);
  176. return 1;
  177. }
  178. static int math_max (lua_State *L) {
  179. int n = lua_gettop(L); /* number of arguments */
  180. int imax = 1; /* index of current maximum value */
  181. int i;
  182. luaL_argcheck(L, n >= 1, 1, "value expected");
  183. for (i = 2; i <= n; i++) {
  184. if (lua_compare(L, imax, i, LUA_OPLT))
  185. imax = i;
  186. }
  187. lua_pushvalue(L, imax);
  188. return 1;
  189. }
  190. /*
  191. ** This function uses 'double' (instead of 'lua_Number') to ensure that
  192. ** all bits from 'l_rand' can be represented, and that 'RAND_MAX + 1.0'
  193. ** will keep full precision (ensuring that 'r' is always less than 1.0.)
  194. */
  195. static int math_random (lua_State *L) {
  196. lua_Integer low, up;
  197. double r = (double)l_rand() * (1.0 / ((double)RAND_MAX + 1.0));
  198. switch (lua_gettop(L)) { /* check number of arguments */
  199. case 0: { /* no arguments */
  200. lua_pushnumber(L, r); /* Number between 0 and 1 */
  201. return 1;
  202. }
  203. case 1: { /* only upper limit */
  204. low = 1;
  205. up = luaL_checkinteger(L, 1);
  206. break;
  207. }
  208. case 2: { /* lower and upper limits */
  209. low = luaL_checkinteger(L, 1);
  210. up = luaL_checkinteger(L, 2);
  211. break;
  212. }
  213. default: return luaL_error(L, "wrong number of arguments");
  214. }
  215. /* random integer in the interval [low, up] */
  216. luaL_argcheck(L, low <= up, 1, "interval is empty");
  217. luaL_argcheck(L, (lua_Unsigned)up - low <= (lua_Unsigned)LUA_MAXINTEGER,
  218. 1, "interval too large");
  219. r *= (double)(up - low) + 1.0;
  220. lua_pushinteger(L, (lua_Integer)r + low);
  221. return 1;
  222. }
  223. static int math_randomseed (lua_State *L) {
  224. l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1));
  225. (void)rand(); /* discard first value to avoid undesirable correlations */
  226. return 0;
  227. }
  228. static int math_type (lua_State *L) {
  229. if (lua_type(L, 1) == LUA_TNUMBER) {
  230. if (lua_isinteger(L, 1))
  231. lua_pushliteral(L, "integer");
  232. else
  233. lua_pushliteral(L, "float");
  234. }
  235. else {
  236. luaL_checkany(L, 1);
  237. lua_pushnil(L);
  238. }
  239. return 1;
  240. }
  241. /*
  242. ** {==================================================================
  243. ** Deprecated functions (for compatibility only)
  244. ** ===================================================================
  245. */
  246. #if defined(LUA_COMPAT_MATHLIB)
  247. static int math_cosh (lua_State *L) {
  248. lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
  249. return 1;
  250. }
  251. static int math_sinh (lua_State *L) {
  252. lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
  253. return 1;
  254. }
  255. static int math_tanh (lua_State *L) {
  256. lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
  257. return 1;
  258. }
  259. static int math_pow (lua_State *L) {
  260. lua_Number x = luaL_checknumber(L, 1);
  261. lua_Number y = luaL_checknumber(L, 2);
  262. lua_pushnumber(L, l_mathop(pow)(x, y));
  263. return 1;
  264. }
  265. static int math_frexp (lua_State *L) {
  266. int e;
  267. lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
  268. lua_pushinteger(L, e);
  269. return 2;
  270. }
  271. static int math_ldexp (lua_State *L) {
  272. lua_Number x = luaL_checknumber(L, 1);
  273. int ep = luaL_checkint(L, 2);
  274. lua_pushnumber(L, l_mathop(ldexp)(x, ep));
  275. return 1;
  276. }
  277. static int math_log10 (lua_State *L) {
  278. lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
  279. return 1;
  280. }
  281. #endif
  282. /* }================================================================== */
  283. static const luaL_Reg mathlib[] = {
  284. {"abs", math_abs},
  285. {"acos", math_acos},
  286. {"asin", math_asin},
  287. {"atan", math_atan},
  288. {"ceil", math_ceil},
  289. {"cos", math_cos},
  290. {"deg", math_deg},
  291. {"exp", math_exp},
  292. {"tointeger", math_toint},
  293. {"floor", math_floor},
  294. {"fmod", math_fmod},
  295. {"ult", math_ult},
  296. {"log", math_log},
  297. {"max", math_max},
  298. {"min", math_min},
  299. {"modf", math_modf},
  300. {"rad", math_rad},
  301. {"random", math_random},
  302. {"randomseed", math_randomseed},
  303. {"sin", math_sin},
  304. {"sqrt", math_sqrt},
  305. {"tan", math_tan},
  306. {"type", math_type},
  307. #if defined(LUA_COMPAT_MATHLIB)
  308. {"atan2", math_atan},
  309. {"cosh", math_cosh},
  310. {"sinh", math_sinh},
  311. {"tanh", math_tanh},
  312. {"pow", math_pow},
  313. {"frexp", math_frexp},
  314. {"ldexp", math_ldexp},
  315. {"log10", math_log10},
  316. #endif
  317. /* placeholders */
  318. {"pi", NULL},
  319. {"huge", NULL},
  320. {"maxinteger", NULL},
  321. {"mininteger", NULL},
  322. {NULL, NULL}
  323. };
  324. /*
  325. ** Open math library
  326. */
  327. LUAMOD_API int luaopen_math (lua_State *L) {
  328. luaL_newlib(L, mathlib);
  329. lua_pushnumber(L, PI);
  330. lua_setfield(L, -2, "pi");
  331. lua_pushnumber(L, HUGE_VAL);
  332. lua_setfield(L, -2, "huge");
  333. lua_pushinteger(L, LUA_MAXINTEGER);
  334. lua_setfield(L, -2, "maxinteger");
  335. lua_pushinteger(L, LUA_MININTEGER);
  336. lua_setfield(L, -2, "mininteger");
  337. return 1;
  338. }