2
0

lmathlib.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. ** $Id: lmathlib.c,v 1.37 2001/03/06 20:09:38 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 LUA_PRIVATE
  9. #include "lua.h"
  10. #include "lauxlib.h"
  11. #include "lualib.h"
  12. #undef PI
  13. #define PI (3.14159265358979323846)
  14. #define RADIANS_PER_DEGREE (PI/180.0)
  15. /*
  16. ** If you want Lua to operate in radians (instead of degrees),
  17. ** define RADIANS
  18. */
  19. #ifdef RADIANS
  20. #define FROMRAD(a) (a)
  21. #define TORAD(a) (a)
  22. #else
  23. #define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
  24. #define TORAD(a) ((a)*RADIANS_PER_DEGREE)
  25. #endif
  26. static int math_abs (lua_State *L) {
  27. lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
  28. return 1;
  29. }
  30. static int math_sin (lua_State *L) {
  31. lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
  32. return 1;
  33. }
  34. static int math_cos (lua_State *L) {
  35. lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
  36. return 1;
  37. }
  38. static int math_tan (lua_State *L) {
  39. lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
  40. return 1;
  41. }
  42. static int math_asin (lua_State *L) {
  43. lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
  44. return 1;
  45. }
  46. static int math_acos (lua_State *L) {
  47. lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
  48. return 1;
  49. }
  50. static int math_atan (lua_State *L) {
  51. lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
  52. return 1;
  53. }
  54. static int math_atan2 (lua_State *L) {
  55. lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2))));
  56. return 1;
  57. }
  58. static int math_ceil (lua_State *L) {
  59. lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
  60. return 1;
  61. }
  62. static int math_floor (lua_State *L) {
  63. lua_pushnumber(L, floor(luaL_check_number(L, 1)));
  64. return 1;
  65. }
  66. static int math_mod (lua_State *L) {
  67. lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
  68. return 1;
  69. }
  70. static int math_sqrt (lua_State *L) {
  71. lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
  72. return 1;
  73. }
  74. static int math_pow (lua_State *L) {
  75. lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
  76. return 1;
  77. }
  78. static int math_log (lua_State *L) {
  79. lua_pushnumber(L, log(luaL_check_number(L, 1)));
  80. return 1;
  81. }
  82. static int math_log10 (lua_State *L) {
  83. lua_pushnumber(L, log10(luaL_check_number(L, 1)));
  84. return 1;
  85. }
  86. static int math_exp (lua_State *L) {
  87. lua_pushnumber(L, exp(luaL_check_number(L, 1)));
  88. return 1;
  89. }
  90. static int math_deg (lua_State *L) {
  91. lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
  92. return 1;
  93. }
  94. static int math_rad (lua_State *L) {
  95. lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE);
  96. return 1;
  97. }
  98. static int math_frexp (lua_State *L) {
  99. int e;
  100. lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e));
  101. lua_pushnumber(L, e);
  102. return 2;
  103. }
  104. static int math_ldexp (lua_State *L) {
  105. lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2)));
  106. return 1;
  107. }
  108. static int math_min (lua_State *L) {
  109. int n = lua_gettop(L); /* number of arguments */
  110. lua_Number dmin = luaL_check_number(L, 1);
  111. int i;
  112. for (i=2; i<=n; i++) {
  113. lua_Number d = luaL_check_number(L, i);
  114. if (d < dmin)
  115. dmin = d;
  116. }
  117. lua_pushnumber(L, dmin);
  118. return 1;
  119. }
  120. static int math_max (lua_State *L) {
  121. int n = lua_gettop(L); /* number of arguments */
  122. lua_Number dmax = luaL_check_number(L, 1);
  123. int i;
  124. for (i=2; i<=n; i++) {
  125. lua_Number d = luaL_check_number(L, i);
  126. if (d > dmax)
  127. dmax = d;
  128. }
  129. lua_pushnumber(L, dmax);
  130. return 1;
  131. }
  132. static int math_random (lua_State *L) {
  133. /* the `%' avoids the (rare) case of r==1, and is needed also because on
  134. some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
  135. lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
  136. switch (lua_gettop(L)) { /* check number of arguments */
  137. case 0: { /* no arguments */
  138. lua_pushnumber(L, r); /* Number between 0 and 1 */
  139. break;
  140. }
  141. case 1: { /* only upper limit */
  142. int u = luaL_check_int(L, 1);
  143. luaL_arg_check(L, 1<=u, 1, l_s("interval is empty"));
  144. lua_pushnumber(L, (int)(r*u)+1); /* integer between 1 and `u' */
  145. break;
  146. }
  147. case 2: { /* lower and upper limits */
  148. int l = luaL_check_int(L, 1);
  149. int u = luaL_check_int(L, 2);
  150. luaL_arg_check(L, l<=u, 2, l_s("interval is empty"));
  151. lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
  152. break;
  153. }
  154. default: lua_error(L, l_s("wrong number of arguments"));
  155. }
  156. return 1;
  157. }
  158. static int math_randomseed (lua_State *L) {
  159. srand(luaL_check_int(L, 1));
  160. return 0;
  161. }
  162. static const luaL_reg mathlib[] = {
  163. {l_s("abs"), math_abs},
  164. {l_s("sin"), math_sin},
  165. {l_s("cos"), math_cos},
  166. {l_s("tan"), math_tan},
  167. {l_s("asin"), math_asin},
  168. {l_s("acos"), math_acos},
  169. {l_s("atan"), math_atan},
  170. {l_s("atan2"), math_atan2},
  171. {l_s("ceil"), math_ceil},
  172. {l_s("floor"), math_floor},
  173. {l_s("mod"), math_mod},
  174. {l_s("frexp"), math_frexp},
  175. {l_s("ldexp"), math_ldexp},
  176. {l_s("sqrt"), math_sqrt},
  177. {l_s("min"), math_min},
  178. {l_s("max"), math_max},
  179. {l_s("log"), math_log},
  180. {l_s("log10"), math_log10},
  181. {l_s("exp"), math_exp},
  182. {l_s("deg"), math_deg},
  183. {l_s("rad"), math_rad},
  184. {l_s("random"), math_random},
  185. {l_s("randomseed"), math_randomseed}
  186. };
  187. /*
  188. ** Open math library
  189. */
  190. LUALIB_API int lua_mathlibopen (lua_State *L) {
  191. luaL_openl(L, mathlib);
  192. lua_pushcfunction(L, math_pow);
  193. lua_settagmethod(L, LUA_TNUMBER, l_s("pow"));
  194. lua_pushnumber(L, PI);
  195. lua_setglobal(L, l_s("PI"));
  196. return 0;
  197. }