lmathlib.c 5.3 KB

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