You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

287 lines
6.5 KiB

  1. /*
  2. ** $Id: lmathlib.c,v 1.81 2012/05/18 17:47:53 roberto Exp $
  3. ** Standard mathematical library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #if defined HAVE_CONFIG_H // LOL BEGIN
  7. # include "config.h"
  8. #endif // LOL END
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #define lmathlib_c
  12. #define LUA_LIB
  13. #include "lua.h"
  14. #include "lauxlib.h"
  15. #include "lualib.h"
  16. /* macro 'l_tg' allows the addition of an 'l' or 'f' to all math operations */
  17. #if !defined(l_tg)
  18. #define l_tg(x) (x)
  19. #endif
  20. #undef PI
  21. #define PI (l_tg(3.1415926535897932384626433832795))
  22. #define RADIANS_PER_DEGREE (PI/180.0)
  23. static int math_abs (lua_State *L) {
  24. lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1)));
  25. return 1;
  26. }
  27. static int math_sin (lua_State *L) {
  28. lua_pushnumber(L, l_tg(sin)(luaL_checknumber(L, 1)));
  29. return 1;
  30. }
  31. static int math_sinh (lua_State *L) {
  32. lua_pushnumber(L, l_tg(sinh)(luaL_checknumber(L, 1)));
  33. return 1;
  34. }
  35. static int math_cos (lua_State *L) {
  36. lua_pushnumber(L, l_tg(cos)(luaL_checknumber(L, 1)));
  37. return 1;
  38. }
  39. static int math_cosh (lua_State *L) {
  40. lua_pushnumber(L, l_tg(cosh)(luaL_checknumber(L, 1)));
  41. return 1;
  42. }
  43. static int math_tan (lua_State *L) {
  44. lua_pushnumber(L, l_tg(tan)(luaL_checknumber(L, 1)));
  45. return 1;
  46. }
  47. static int math_tanh (lua_State *L) {
  48. lua_pushnumber(L, l_tg(tanh)(luaL_checknumber(L, 1)));
  49. return 1;
  50. }
  51. static int math_asin (lua_State *L) {
  52. lua_pushnumber(L, l_tg(asin)(luaL_checknumber(L, 1)));
  53. return 1;
  54. }
  55. static int math_acos (lua_State *L) {
  56. lua_pushnumber(L, l_tg(acos)(luaL_checknumber(L, 1)));
  57. return 1;
  58. }
  59. static int math_atan (lua_State *L) {
  60. lua_pushnumber(L, l_tg(atan)(luaL_checknumber(L, 1)));
  61. return 1;
  62. }
  63. static int math_atan2 (lua_State *L) {
  64. lua_pushnumber(L, l_tg(atan2)(luaL_checknumber(L, 1),
  65. luaL_checknumber(L, 2)));
  66. return 1;
  67. }
  68. static int math_ceil (lua_State *L) {
  69. lua_pushnumber(L, l_tg(ceil)(luaL_checknumber(L, 1)));
  70. return 1;
  71. }
  72. static int math_floor (lua_State *L) {
  73. lua_pushnumber(L, l_tg(floor)(luaL_checknumber(L, 1)));
  74. return 1;
  75. }
  76. static int math_fmod (lua_State *L) {
  77. lua_pushnumber(L, l_tg(fmod)(luaL_checknumber(L, 1),
  78. luaL_checknumber(L, 2)));
  79. return 1;
  80. }
  81. static int math_modf (lua_State *L) {
  82. lua_Number ip;
  83. lua_Number fp = l_tg(modf)(luaL_checknumber(L, 1), &ip);
  84. lua_pushnumber(L, ip);
  85. lua_pushnumber(L, fp);
  86. return 2;
  87. }
  88. static int math_sqrt (lua_State *L) {
  89. lua_pushnumber(L, l_tg(sqrt)(luaL_checknumber(L, 1)));
  90. return 1;
  91. }
  92. static int math_pow (lua_State *L) {
  93. lua_pushnumber(L, l_tg(pow)(luaL_checknumber(L, 1),
  94. luaL_checknumber(L, 2)));
  95. return 1;
  96. }
  97. static int math_log (lua_State *L) {
  98. lua_Number x = luaL_checknumber(L, 1);
  99. lua_Number res;
  100. if (lua_isnoneornil(L, 2))
  101. res = l_tg(log)(x);
  102. else {
  103. lua_Number base = luaL_checknumber(L, 2);
  104. if (base == 10.0) res = l_tg(log10)(x);
  105. else res = l_tg(log)(x)/l_tg(log)(base);
  106. }
  107. lua_pushnumber(L, res);
  108. return 1;
  109. }
  110. #if defined(LUA_COMPAT_LOG10)
  111. static int math_log10 (lua_State *L) {
  112. lua_pushnumber(L, l_tg(log10)(luaL_checknumber(L, 1)));
  113. return 1;
  114. }
  115. #endif
  116. static int math_exp (lua_State *L) {
  117. lua_pushnumber(L, l_tg(exp)(luaL_checknumber(L, 1)));
  118. return 1;
  119. }
  120. static int math_deg (lua_State *L) {
  121. lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
  122. return 1;
  123. }
  124. static int math_rad (lua_State *L) {
  125. lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
  126. return 1;
  127. }
  128. static int math_frexp (lua_State *L) {
  129. int e;
  130. lua_pushnumber(L, l_tg(frexp)(luaL_checknumber(L, 1), &e));
  131. lua_pushinteger(L, e);
  132. return 2;
  133. }
  134. static int math_ldexp (lua_State *L) {
  135. lua_pushnumber(L, l_tg(ldexp)(luaL_checknumber(L, 1),
  136. luaL_checkint(L, 2)));
  137. return 1;
  138. }
  139. static int math_min (lua_State *L) {
  140. int n = lua_gettop(L); /* number of arguments */
  141. lua_Number dmin = luaL_checknumber(L, 1);
  142. int i;
  143. for (i=2; i<=n; i++) {
  144. lua_Number d = luaL_checknumber(L, i);
  145. if (d < dmin)
  146. dmin = d;
  147. }
  148. lua_pushnumber(L, dmin);
  149. return 1;
  150. }
  151. static int math_max (lua_State *L) {
  152. int n = lua_gettop(L); /* number of arguments */
  153. lua_Number dmax = luaL_checknumber(L, 1);
  154. int i;
  155. for (i=2; i<=n; i++) {
  156. lua_Number d = luaL_checknumber(L, i);
  157. if (d > dmax)
  158. dmax = d;
  159. }
  160. lua_pushnumber(L, dmax);
  161. return 1;
  162. }
  163. static int math_random (lua_State *L) {
  164. /* the `%' avoids the (rare) case of r==1, and is needed also because on
  165. some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
  166. lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
  167. switch (lua_gettop(L)) { /* check number of arguments */
  168. case 0: { /* no arguments */
  169. lua_pushnumber(L, r); /* Number between 0 and 1 */
  170. break;
  171. }
  172. case 1: { /* only upper limit */
  173. lua_Number u = luaL_checknumber(L, 1);
  174. luaL_argcheck(L, 1.0 <= u, 1, "interval is empty");
  175. lua_pushnumber(L, l_tg(floor)(r*u) + 1.0); /* int in [1, u] */
  176. break;
  177. }
  178. case 2: { /* lower and upper limits */
  179. lua_Number l = luaL_checknumber(L, 1);
  180. lua_Number u = luaL_checknumber(L, 2);
  181. luaL_argcheck(L, l <= u, 2, "interval is empty");
  182. lua_pushnumber(L, l_tg(floor)(r*(u-l+1)) + l); /* int in [l, u] */
  183. break;
  184. }
  185. default: return luaL_error(L, "wrong number of arguments");
  186. }
  187. return 1;
  188. }
  189. static int math_randomseed (lua_State *L) {
  190. srand(luaL_checkunsigned(L, 1));
  191. (void)rand(); /* discard first value to avoid undesirable correlations */
  192. return 0;
  193. }
  194. static const luaL_Reg mathlib[] = {
  195. {"abs", math_abs},
  196. {"acos", math_acos},
  197. {"asin", math_asin},
  198. {"atan2", math_atan2},
  199. {"atan", math_atan},
  200. {"ceil", math_ceil},
  201. {"cosh", math_cosh},
  202. {"cos", math_cos},
  203. {"deg", math_deg},
  204. {"exp", math_exp},
  205. {"floor", math_floor},
  206. {"fmod", math_fmod},
  207. {"frexp", math_frexp},
  208. {"ldexp", math_ldexp},
  209. #if defined(LUA_COMPAT_LOG10)
  210. {"log10", math_log10},
  211. #endif
  212. {"log", math_log},
  213. {"max", math_max},
  214. {"min", math_min},
  215. {"modf", math_modf},
  216. {"pow", math_pow},
  217. {"rad", math_rad},
  218. {"random", math_random},
  219. {"randomseed", math_randomseed},
  220. {"sinh", math_sinh},
  221. {"sin", math_sin},
  222. {"sqrt", math_sqrt},
  223. {"tanh", math_tanh},
  224. {"tan", math_tan},
  225. {NULL, NULL}
  226. };
  227. /*
  228. ** Open math library
  229. */
  230. LUAMOD_API int luaopen_math (lua_State *L) {
  231. luaL_newlib(L, mathlib);
  232. lua_pushnumber(L, PI);
  233. lua_setfield(L, -2, "pi");
  234. lua_pushnumber(L, HUGE_VAL);
  235. lua_setfield(L, -2, "huge");
  236. return 1;
  237. }