2
0

lmathlib.c 4.8 KB

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