lmathlib.c 5.3 KB

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