lmathlib.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. ** $Id: lmathlib.c,v 1.18 1999/08/16 20:52:00 roberto Exp roberto $
  3. ** Lua standard mathematical library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include "lauxlib.h"
  9. #include "lua.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 (void) {
  26. lua_pushnumber(fabs(luaL_check_number(1)));
  27. }
  28. static void math_sin (void) {
  29. lua_pushnumber(sin(TORAD(luaL_check_number(1))));
  30. }
  31. static void math_cos (void) {
  32. lua_pushnumber(cos(TORAD(luaL_check_number(1))));
  33. }
  34. static void math_tan (void) {
  35. lua_pushnumber(tan(TORAD(luaL_check_number(1))));
  36. }
  37. static void math_asin (void) {
  38. lua_pushnumber(FROMRAD(asin(luaL_check_number(1))));
  39. }
  40. static void math_acos (void) {
  41. lua_pushnumber(FROMRAD(acos(luaL_check_number(1))));
  42. }
  43. static void math_atan (void) {
  44. lua_pushnumber(FROMRAD(atan(luaL_check_number(1))));
  45. }
  46. static void math_atan2 (void) {
  47. lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2))));
  48. }
  49. static void math_ceil (void) {
  50. lua_pushnumber(ceil(luaL_check_number(1)));
  51. }
  52. static void math_floor (void) {
  53. lua_pushnumber(floor(luaL_check_number(1)));
  54. }
  55. static void math_mod (void) {
  56. lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
  57. }
  58. static void math_sqrt (void) {
  59. lua_pushnumber(sqrt(luaL_check_number(1)));
  60. }
  61. static void math_pow (void) {
  62. lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
  63. }
  64. static void math_log (void) {
  65. lua_pushnumber(log(luaL_check_number(1)));
  66. }
  67. static void math_log10 (void) {
  68. lua_pushnumber(log10(luaL_check_number(1)));
  69. }
  70. static void math_exp (void) {
  71. lua_pushnumber(exp(luaL_check_number(1)));
  72. }
  73. static void math_deg (void) {
  74. lua_pushnumber(luaL_check_number(1)/RADIANS_PER_DEGREE);
  75. }
  76. static void math_rad (void) {
  77. lua_pushnumber(luaL_check_number(1)*RADIANS_PER_DEGREE);
  78. }
  79. static void math_frexp (void) {
  80. int e;
  81. lua_pushnumber(frexp(luaL_check_number(1), &e));
  82. lua_pushnumber(e);
  83. }
  84. static void math_ldexp (void) {
  85. lua_pushnumber(ldexp(luaL_check_number(1), luaL_check_int(2)));
  86. }
  87. static void math_min (void) {
  88. int i = 1;
  89. double dmin = luaL_check_number(i);
  90. while (lua_getparam(++i) != LUA_NOOBJECT) {
  91. double d = luaL_check_number(i);
  92. if (d < dmin)
  93. dmin = d;
  94. }
  95. lua_pushnumber(dmin);
  96. }
  97. static void math_max (void) {
  98. int i = 1;
  99. double dmax = luaL_check_number(i);
  100. while (lua_getparam(++i) != LUA_NOOBJECT) {
  101. double d = luaL_check_number(i);
  102. if (d > dmax)
  103. dmax = d;
  104. }
  105. lua_pushnumber(dmax);
  106. }
  107. static void math_random (void) {
  108. /* the '%' avoids the (rare) case of r==1, and is needed also because on
  109. some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */
  110. double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
  111. if (lua_getparam(1) == LUA_NOOBJECT) /* no arguments? */
  112. lua_pushnumber(r); /* real between 0 & 1 */
  113. else {
  114. int l, u; /* lower & upper limits */
  115. if (lua_getparam(2) == LUA_NOOBJECT) { /* only one argument? */
  116. l = 1;
  117. u = luaL_check_int(1);
  118. }
  119. else { /* two arguments */
  120. l = luaL_check_int(1);
  121. u = luaL_check_int(2);
  122. }
  123. luaL_arg_check(l<=u, 1, "interval is empty");
  124. lua_pushnumber((int)(r*(u-l+1))+l); /* integer between l & u */
  125. }
  126. }
  127. static void math_randomseed (void) {
  128. srand(luaL_check_int(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 (void) {
  159. luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
  160. lua_pushcfunction(math_pow);
  161. lua_pushnumber(0); /* to get its tag */
  162. lua_settagmethod(lua_tag(lua_pop()), "pow");
  163. lua_pushnumber(PI); lua_setglobal("PI");
  164. }