lmathlib.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. ** $Id: lmathlib.c,v 1.78 2010/11/12 15:47:34 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 (3.14159265358979323846)
  15. #define RADIANS_PER_DEGREE (PI/180.0)
  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. static int math_abs (lua_State *L) {
  21. lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1)));
  22. return 1;
  23. }
  24. static int math_sin (lua_State *L) {
  25. lua_pushnumber(L, l_tg(sin)(luaL_checknumber(L, 1)));
  26. return 1;
  27. }
  28. static int math_sinh (lua_State *L) {
  29. lua_pushnumber(L, l_tg(sinh)(luaL_checknumber(L, 1)));
  30. return 1;
  31. }
  32. static int math_cos (lua_State *L) {
  33. lua_pushnumber(L, l_tg(cos)(luaL_checknumber(L, 1)));
  34. return 1;
  35. }
  36. static int math_cosh (lua_State *L) {
  37. lua_pushnumber(L, l_tg(cosh)(luaL_checknumber(L, 1)));
  38. return 1;
  39. }
  40. static int math_tan (lua_State *L) {
  41. lua_pushnumber(L, l_tg(tan)(luaL_checknumber(L, 1)));
  42. return 1;
  43. }
  44. static int math_tanh (lua_State *L) {
  45. lua_pushnumber(L, l_tg(tanh)(luaL_checknumber(L, 1)));
  46. return 1;
  47. }
  48. static int math_asin (lua_State *L) {
  49. lua_pushnumber(L, l_tg(asin)(luaL_checknumber(L, 1)));
  50. return 1;
  51. }
  52. static int math_acos (lua_State *L) {
  53. lua_pushnumber(L, l_tg(acos)(luaL_checknumber(L, 1)));
  54. return 1;
  55. }
  56. static int math_atan (lua_State *L) {
  57. lua_pushnumber(L, l_tg(atan)(luaL_checknumber(L, 1)));
  58. return 1;
  59. }
  60. static int math_atan2 (lua_State *L) {
  61. lua_pushnumber(L, l_tg(atan2)(luaL_checknumber(L, 1),
  62. luaL_checknumber(L, 2)));
  63. return 1;
  64. }
  65. static int math_ceil (lua_State *L) {
  66. lua_pushnumber(L, l_tg(ceil)(luaL_checknumber(L, 1)));
  67. return 1;
  68. }
  69. static int math_floor (lua_State *L) {
  70. lua_pushnumber(L, l_tg(floor)(luaL_checknumber(L, 1)));
  71. return 1;
  72. }
  73. static int math_fmod (lua_State *L) {
  74. lua_pushnumber(L, l_tg(fmod)(luaL_checknumber(L, 1),
  75. luaL_checknumber(L, 2)));
  76. return 1;
  77. }
  78. static int math_modf (lua_State *L) {
  79. lua_Number ip;
  80. lua_Number fp = l_tg(modf)(luaL_checknumber(L, 1), &ip);
  81. lua_pushnumber(L, ip);
  82. lua_pushnumber(L, fp);
  83. return 2;
  84. }
  85. static int math_sqrt (lua_State *L) {
  86. lua_pushnumber(L, l_tg(sqrt)(luaL_checknumber(L, 1)));
  87. return 1;
  88. }
  89. static int math_pow (lua_State *L) {
  90. lua_pushnumber(L, l_tg(pow)(luaL_checknumber(L, 1),
  91. luaL_checknumber(L, 2)));
  92. return 1;
  93. }
  94. static int math_log (lua_State *L) {
  95. lua_Number x = luaL_checknumber(L, 1);
  96. lua_Number res;
  97. if (lua_isnoneornil(L, 2))
  98. res = l_tg(log)(x);
  99. else {
  100. lua_Number base = luaL_checknumber(L, 2);
  101. if (base == 10.0) res = l_tg(log10)(x);
  102. else res = l_tg(log)(x)/l_tg(log)(base);
  103. }
  104. lua_pushnumber(L, res);
  105. return 1;
  106. }
  107. static int math_log10 (lua_State *L) {
  108. #if !defined(LUA_COMPAT_LOG10)
  109. return luaL_error(L, "function " LUA_QL("log10")
  110. " is deprecated; use log(x, 10) instead");
  111. #else
  112. lua_pushnumber(L, l_tg(log10)(luaL_checknumber(L, 1)));
  113. return 1;
  114. #endif
  115. }
  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. {"log10", math_log10},
  210. {"log", math_log},
  211. {"max", math_max},
  212. {"min", math_min},
  213. {"modf", math_modf},
  214. {"pow", math_pow},
  215. {"rad", math_rad},
  216. {"random", math_random},
  217. {"randomseed", math_randomseed},
  218. {"sinh", math_sinh},
  219. {"sin", math_sin},
  220. {"sqrt", math_sqrt},
  221. {"tanh", math_tanh},
  222. {"tan", math_tan},
  223. {NULL, NULL}
  224. };
  225. /*
  226. ** Open math library
  227. */
  228. LUAMOD_API int luaopen_math (lua_State *L) {
  229. luaL_newlib(L, mathlib);
  230. lua_pushnumber(L, PI);
  231. lua_setfield(L, -2, "pi");
  232. lua_pushnumber(L, HUGE_VAL);
  233. lua_setfield(L, -2, "huge");
  234. return 1;
  235. }