lmathlib.c 6.0 KB

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