lmathlib.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. ** $Id: lmathlib.c,v 1.16 1999/02/19 17:33:35 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. int l = luaL_opt_int(1, 0);
  112. if (l == 0)
  113. lua_pushnumber(r);
  114. else {
  115. int u = luaL_opt_int(2, 0);
  116. if (u == 0) {
  117. u = l;
  118. l = 1;
  119. }
  120. luaL_arg_check(l<=u, 1, "interval is empty");
  121. lua_pushnumber((int)(r*(u-l+1))+l);
  122. }
  123. }
  124. static void math_randomseed (void) {
  125. srand(luaL_check_int(1));
  126. }
  127. static struct luaL_reg mathlib[] = {
  128. {"abs", math_abs},
  129. {"sin", math_sin},
  130. {"cos", math_cos},
  131. {"tan", math_tan},
  132. {"asin", math_asin},
  133. {"acos", math_acos},
  134. {"atan", math_atan},
  135. {"atan2", math_atan2},
  136. {"ceil", math_ceil},
  137. {"floor", math_floor},
  138. {"mod", math_mod},
  139. {"frexp", math_frexp},
  140. {"ldexp", math_ldexp},
  141. {"sqrt", math_sqrt},
  142. {"min", math_min},
  143. {"max", math_max},
  144. {"log", math_log},
  145. {"log10", math_log10},
  146. {"exp", math_exp},
  147. {"deg", math_deg},
  148. {"rad", math_rad},
  149. {"random", math_random},
  150. {"randomseed", math_randomseed}
  151. };
  152. /*
  153. ** Open math library
  154. */
  155. void lua_mathlibopen (void) {
  156. luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
  157. lua_pushcfunction(math_pow);
  158. lua_pushnumber(0); /* to get its tag */
  159. lua_settagmethod(lua_tag(lua_pop()), "pow");
  160. lua_pushnumber(PI); lua_setglobal("PI");
  161. }