lmathlib.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. ** $Id: lmathlib.c,v 1.91 2013/07/10 20:57:05 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 ((lua_Number)(3.1415926535897932384626433832795))
  15. #define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0))
  16. static int math_abs (lua_State *L) {
  17. lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
  18. return 1;
  19. }
  20. static int math_sin (lua_State *L) {
  21. lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
  22. return 1;
  23. }
  24. static int math_sinh (lua_State *L) {
  25. lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
  26. return 1;
  27. }
  28. static int math_cos (lua_State *L) {
  29. lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
  30. return 1;
  31. }
  32. static int math_cosh (lua_State *L) {
  33. lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
  34. return 1;
  35. }
  36. static int math_tan (lua_State *L) {
  37. lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
  38. return 1;
  39. }
  40. static int math_tanh (lua_State *L) {
  41. lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
  42. return 1;
  43. }
  44. static int math_asin (lua_State *L) {
  45. lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
  46. return 1;
  47. }
  48. static int math_acos (lua_State *L) {
  49. lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
  50. return 1;
  51. }
  52. static int math_atan (lua_State *L) {
  53. lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1)));
  54. return 1;
  55. }
  56. static int math_atan2 (lua_State *L) {
  57. lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1),
  58. luaL_checknumber(L, 2)));
  59. return 1;
  60. }
  61. static int math_ceil (lua_State *L) {
  62. lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));
  63. return 1;
  64. }
  65. static int math_floor (lua_State *L) {
  66. lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
  67. return 1;
  68. }
  69. static int math_ifloor (lua_State *L) {
  70. int valid;
  71. lua_Integer n = lua_tointegerx(L, 1, &valid);
  72. if (valid)
  73. lua_pushinteger(L, n);
  74. else {
  75. luaL_checktype(L, 1, LUA_TNUMBER); /* error if not a number */
  76. lua_pushnil(L); /* number with invalid integer value */
  77. }
  78. return 1;
  79. }
  80. static int math_fmod (lua_State *L) {
  81. lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
  82. luaL_checknumber(L, 2)));
  83. return 1;
  84. }
  85. /*
  86. ** next function does not use 'modf', avoiding problems with 'double*'
  87. ** (which is not compatible with 'float*') when lua_Number is not
  88. ** 'double'.
  89. */
  90. static int math_modf (lua_State *L) {
  91. lua_Number n = luaL_checknumber(L, 1);
  92. /* integer part (rounds toward zero) */
  93. lua_Number ip = (n < 0) ? -l_mathop(floor)(-n) : l_mathop(floor)(n);
  94. lua_pushnumber(L, ip);
  95. /* fractionary part (test handles inf/-inf) */
  96. lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip));
  97. return 2;
  98. }
  99. static int math_sqrt (lua_State *L) {
  100. lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
  101. return 1;
  102. }
  103. static int math_pow (lua_State *L) {
  104. lua_Number x = luaL_checknumber(L, 1);
  105. lua_Number y = luaL_checknumber(L, 2);
  106. lua_pushnumber(L, l_mathop(pow)(x, y));
  107. return 1;
  108. }
  109. static int math_log (lua_State *L) {
  110. lua_Number x = luaL_checknumber(L, 1);
  111. lua_Number res;
  112. if (lua_isnoneornil(L, 2))
  113. res = l_mathop(log)(x);
  114. else {
  115. lua_Number base = luaL_checknumber(L, 2);
  116. if (base == (lua_Number)10.0) res = l_mathop(log10)(x);
  117. else res = l_mathop(log)(x)/l_mathop(log)(base);
  118. }
  119. lua_pushnumber(L, res);
  120. return 1;
  121. }
  122. #if defined(LUA_COMPAT_LOG10)
  123. static int math_log10 (lua_State *L) {
  124. lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
  125. return 1;
  126. }
  127. #endif
  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)/RADIANS_PER_DEGREE);
  134. return 1;
  135. }
  136. static int math_rad (lua_State *L) {
  137. lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
  138. return 1;
  139. }
  140. static int math_frexp (lua_State *L) {
  141. int e;
  142. lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
  143. lua_pushinteger(L, e);
  144. return 2;
  145. }
  146. static int math_ldexp (lua_State *L) {
  147. lua_Number x = luaL_checknumber(L, 1);
  148. int ep = luaL_checkint(L, 2);
  149. lua_pushnumber(L, l_mathop(ldexp)(x, ep));
  150. return 1;
  151. }
  152. static int math_min (lua_State *L) {
  153. int n = lua_gettop(L); /* number of arguments */
  154. lua_Number dmin = luaL_checknumber(L, 1);
  155. int i;
  156. for (i=2; i<=n; i++) {
  157. lua_Number d = luaL_checknumber(L, i);
  158. if (d < dmin)
  159. dmin = d;
  160. }
  161. lua_pushnumber(L, dmin);
  162. return 1;
  163. }
  164. static int math_max (lua_State *L) {
  165. int n = lua_gettop(L); /* number of arguments */
  166. lua_Number dmax = luaL_checknumber(L, 1);
  167. int i;
  168. for (i=2; i<=n; i++) {
  169. lua_Number d = luaL_checknumber(L, i);
  170. if (d > dmax)
  171. dmax = d;
  172. }
  173. lua_pushnumber(L, dmax);
  174. return 1;
  175. }
  176. static int math_random (lua_State *L) {
  177. /* the `%' avoids the (rare) case of r==1, and is needed also because on
  178. some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
  179. lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
  180. lua_Integer low, up;
  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. up++; /* change interval to [low, up) */
  200. luaL_argcheck(L, up - low > 0, 1, "interval is empty");
  201. lua_pushinteger(L, (lua_Integer)(r * (lua_Number)(up - low)) + low);
  202. return 1;
  203. }
  204. static int math_randomseed (lua_State *L) {
  205. srand(luaL_checkunsigned(L, 1));
  206. (void)rand(); /* discard first value to avoid undesirable correlations */
  207. return 0;
  208. }
  209. static int math_type (lua_State *L) {
  210. luaL_checkany(L, 1);
  211. if (lua_type(L, 1) == LUA_TNUMBER) {
  212. if (lua_isinteger(L, 1))
  213. lua_pushliteral(L, "integer");
  214. else
  215. lua_pushliteral(L, "float");
  216. }
  217. else
  218. lua_pushnil(L);
  219. return 1;
  220. }
  221. static const luaL_Reg mathlib[] = {
  222. {"abs", math_abs},
  223. {"acos", math_acos},
  224. {"asin", math_asin},
  225. {"atan2", math_atan2},
  226. {"atan", math_atan},
  227. {"ceil", math_ceil},
  228. {"cosh", math_cosh},
  229. {"cos", math_cos},
  230. {"deg", math_deg},
  231. {"exp", math_exp},
  232. {"floor", math_floor},
  233. {"ifloor", math_ifloor},
  234. {"fmod", math_fmod},
  235. {"frexp", math_frexp},
  236. {"ldexp", math_ldexp},
  237. #if defined(LUA_COMPAT_LOG10)
  238. {"log10", math_log10},
  239. #endif
  240. {"log", math_log},
  241. {"max", math_max},
  242. {"min", math_min},
  243. {"modf", math_modf},
  244. {"pow", math_pow},
  245. {"rad", math_rad},
  246. {"random", math_random},
  247. {"randomseed", math_randomseed},
  248. {"sinh", math_sinh},
  249. {"sin", math_sin},
  250. {"sqrt", math_sqrt},
  251. {"tanh", math_tanh},
  252. {"tan", math_tan},
  253. {"type", math_type},
  254. {NULL, NULL}
  255. };
  256. /*
  257. ** Open math library
  258. */
  259. LUAMOD_API int luaopen_math (lua_State *L) {
  260. luaL_newlib(L, mathlib);
  261. lua_pushnumber(L, PI);
  262. lua_setfield(L, -2, "pi");
  263. lua_pushnumber(L, HUGE_VAL);
  264. lua_setfield(L, -2, "huge");
  265. return 1;
  266. }