lmathlib.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. ** $Id: lmathlib.c,v 1.25 2000/06/12 13:52:05 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 void math_abs (lua_State *L) {
  26. lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
  27. }
  28. static void math_sin (lua_State *L) {
  29. lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
  30. }
  31. static void math_cos (lua_State *L) {
  32. lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
  33. }
  34. static void math_tan (lua_State *L) {
  35. lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
  36. }
  37. static void math_asin (lua_State *L) {
  38. lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
  39. }
  40. static void math_acos (lua_State *L) {
  41. lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
  42. }
  43. static void math_atan (lua_State *L) {
  44. lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
  45. }
  46. static void math_atan2 (lua_State *L) {
  47. lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2))));
  48. }
  49. static void math_ceil (lua_State *L) {
  50. lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
  51. }
  52. static void math_floor (lua_State *L) {
  53. lua_pushnumber(L, floor(luaL_check_number(L, 1)));
  54. }
  55. static void math_mod (lua_State *L) {
  56. lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
  57. }
  58. static void math_sqrt (lua_State *L) {
  59. lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
  60. }
  61. static void math_pow (lua_State *L) {
  62. lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
  63. }
  64. static void math_log (lua_State *L) {
  65. lua_pushnumber(L, log(luaL_check_number(L, 1)));
  66. }
  67. static void math_log10 (lua_State *L) {
  68. lua_pushnumber(L, log10(luaL_check_number(L, 1)));
  69. }
  70. static void math_exp (lua_State *L) {
  71. lua_pushnumber(L, exp(luaL_check_number(L, 1)));
  72. }
  73. static void math_deg (lua_State *L) {
  74. lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
  75. }
  76. static void math_rad (lua_State *L) {
  77. lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE);
  78. }
  79. static void math_frexp (lua_State *L) {
  80. int e;
  81. lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e));
  82. lua_pushnumber(L, e);
  83. }
  84. static void math_ldexp (lua_State *L) {
  85. lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2)));
  86. }
  87. static void math_min (lua_State *L) {
  88. int i = 1;
  89. double dmin = luaL_check_number(L, i);
  90. while (lua_getparam(L, ++i) != LUA_NOOBJECT) {
  91. double d = luaL_check_number(L, i);
  92. if (d < dmin)
  93. dmin = d;
  94. }
  95. lua_pushnumber(L, dmin);
  96. }
  97. static void math_max (lua_State *L) {
  98. int i = 1;
  99. double dmax = luaL_check_number(L, i);
  100. while (lua_getparam(L, ++i) != LUA_NOOBJECT) {
  101. double d = luaL_check_number(L, i);
  102. if (d > dmax)
  103. dmax = d;
  104. }
  105. lua_pushnumber(L, dmax);
  106. }
  107. static void math_random (lua_State *L) {
  108. /* the '%' avoids the (rare) case of r==1, and is needed also because on
  109. some systems (SunOS!) "rand()" may return a value larger than RAND_MAX */
  110. double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
  111. if (lua_getparam(L, 1) == LUA_NOOBJECT) /* no arguments? */
  112. lua_pushnumber(L, r); /* Number between 0 and 1 */
  113. else {
  114. int l, u; /* lower & upper limits */
  115. if (lua_getparam(L, 2) == LUA_NOOBJECT) { /* only one argument? */
  116. l = 1;
  117. u = luaL_check_int(L, 1);
  118. }
  119. else { /* two arguments */
  120. l = luaL_check_int(L, 1);
  121. u = luaL_check_int(L, 2);
  122. }
  123. luaL_arg_check(L, l<=u, 1, "interval is empty");
  124. lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
  125. }
  126. }
  127. static void math_randomseed (lua_State *L) {
  128. srand(luaL_check_int(L, 1));
  129. }
  130. static const struct luaL_reg mathlib[] = {
  131. {"abs", math_abs},
  132. {"sin", math_sin},
  133. {"cos", math_cos},
  134. {"tan", math_tan},
  135. {"asin", math_asin},
  136. {"acos", math_acos},
  137. {"atan", math_atan},
  138. {"atan2", math_atan2},
  139. {"ceil", math_ceil},
  140. {"floor", math_floor},
  141. {"mod", math_mod},
  142. {"frexp", math_frexp},
  143. {"ldexp", math_ldexp},
  144. {"sqrt", math_sqrt},
  145. {"min", math_min},
  146. {"max", math_max},
  147. {"log", math_log},
  148. {"log10", math_log10},
  149. {"exp", math_exp},
  150. {"deg", math_deg},
  151. {"rad", math_rad},
  152. {"random", math_random},
  153. {"randomseed", math_randomseed}
  154. };
  155. /*
  156. ** Open math library
  157. */
  158. void lua_mathlibopen (lua_State *L) {
  159. luaL_openl(L, mathlib);
  160. lua_pushcfunction(L, math_pow);
  161. lua_pushnumber(L, 0); /* to get its tag */
  162. lua_settagmethod(L, lua_tag(L, lua_pop(L)), "pow");
  163. lua_pushnumber(L, PI); lua_setglobal(L, "PI");
  164. }