lmathlib.c 5.7 KB

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