lmathlib.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. ** $Id: lmathlib.c,v 1.54 2002/12/20 10:26:33 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. #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 degrees (instead of radians),
  17. ** define USE_DEGREES
  18. */
  19. #ifdef USE_DEGREES
  20. #define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
  21. #define TORAD(a) ((a)*RADIANS_PER_DEGREE)
  22. #else
  23. #define FROMRAD(a) (a)
  24. #define TORAD(a) (a)
  25. #endif
  26. static int math_abs (lua_State *L) {
  27. lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
  28. return 1;
  29. }
  30. static int math_sin (lua_State *L) {
  31. lua_pushnumber(L, sin(TORAD(luaL_checknumber(L, 1))));
  32. return 1;
  33. }
  34. static int math_cos (lua_State *L) {
  35. lua_pushnumber(L, cos(TORAD(luaL_checknumber(L, 1))));
  36. return 1;
  37. }
  38. static int math_tan (lua_State *L) {
  39. lua_pushnumber(L, tan(TORAD(luaL_checknumber(L, 1))));
  40. return 1;
  41. }
  42. static int math_asin (lua_State *L) {
  43. lua_pushnumber(L, FROMRAD(asin(luaL_checknumber(L, 1))));
  44. return 1;
  45. }
  46. static int math_acos (lua_State *L) {
  47. lua_pushnumber(L, FROMRAD(acos(luaL_checknumber(L, 1))));
  48. return 1;
  49. }
  50. static int math_atan (lua_State *L) {
  51. lua_pushnumber(L, FROMRAD(atan(luaL_checknumber(L, 1))));
  52. return 1;
  53. }
  54. static int math_atan2 (lua_State *L) {
  55. lua_pushnumber(L, FROMRAD(atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))));
  56. return 1;
  57. }
  58. static int math_ceil (lua_State *L) {
  59. lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
  60. return 1;
  61. }
  62. static int math_floor (lua_State *L) {
  63. lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
  64. return 1;
  65. }
  66. static int math_mod (lua_State *L) {
  67. lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  68. return 1;
  69. }
  70. static int math_sqrt (lua_State *L) {
  71. lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
  72. return 1;
  73. }
  74. static int math_pow (lua_State *L) {
  75. lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  76. return 1;
  77. }
  78. static int math_log (lua_State *L) {
  79. lua_pushnumber(L, log(luaL_checknumber(L, 1)));
  80. return 1;
  81. }
  82. static int math_log10 (lua_State *L) {
  83. lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
  84. return 1;
  85. }
  86. static int math_exp (lua_State *L) {
  87. lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
  88. return 1;
  89. }
  90. static int math_deg (lua_State *L) {
  91. lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
  92. return 1;
  93. }
  94. static int math_rad (lua_State *L) {
  95. lua_pushnumber(L, luaL_checknumber(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_checknumber(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_checknumber(L, 1), luaL_checkint(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_checknumber(L, 1);
  111. int i;
  112. for (i=2; i<=n; i++) {
  113. lua_Number d = luaL_checknumber(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_checknumber(L, 1);
  123. int i;
  124. for (i=2; i<=n; i++) {
  125. lua_Number d = luaL_checknumber(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_checkint(L, 1);
  143. luaL_argcheck(L, 1<=u, 1, "interval is empty");
  144. lua_pushnumber(L, (int)floor(r*u)+1); /* int between 1 and `u' */
  145. break;
  146. }
  147. case 2: { /* lower and upper limits */
  148. int l = luaL_checkint(L, 1);
  149. int u = luaL_checkint(L, 2);
  150. luaL_argcheck(L, l<=u, 2, "interval is empty");
  151. lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */
  152. break;
  153. }
  154. default: return luaL_error(L, "wrong number of arguments");
  155. }
  156. return 1;
  157. }
  158. static int math_randomseed (lua_State *L) {
  159. srand(luaL_checkint(L, 1));
  160. return 0;
  161. }
  162. static const luaL_reg mathlib[] = {
  163. {"abs", math_abs},
  164. {"sin", math_sin},
  165. {"cos", math_cos},
  166. {"tan", math_tan},
  167. {"asin", math_asin},
  168. {"acos", math_acos},
  169. {"atan", math_atan},
  170. {"atan2", math_atan2},
  171. {"ceil", math_ceil},
  172. {"floor", math_floor},
  173. {"mod", math_mod},
  174. {"frexp", math_frexp},
  175. {"ldexp", math_ldexp},
  176. {"sqrt", math_sqrt},
  177. {"min", math_min},
  178. {"max", math_max},
  179. {"log", math_log},
  180. {"log10", math_log10},
  181. {"exp", math_exp},
  182. {"deg", math_deg},
  183. {"pow", math_pow},
  184. {"rad", math_rad},
  185. {"random", math_random},
  186. {"randomseed", math_randomseed},
  187. {NULL, NULL}
  188. };
  189. /*
  190. ** Open math library
  191. */
  192. LUALIB_API int luaopen_math (lua_State *L) {
  193. luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
  194. lua_pushliteral(L, "pi");
  195. lua_pushnumber(L, PI);
  196. lua_settable(L, -3);
  197. lua_pushliteral(L, "__pow");
  198. lua_pushcfunction(L, math_pow);
  199. lua_settable(L, LUA_REGISTRYINDEX);
  200. return 1;
  201. }