lmathlib.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. ** $Id: lmathlib.c,v 1.14 1998/12/30 21:23:26 roberto Exp $
  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. #define PI (3.14159265358979323846)
  12. /*
  13. ** If you want Lua to operate in radians (instead of degrees),
  14. ** changes these two macros to identities:
  15. ** #define FROMRAD(a) (a)
  16. ** #define TORAD(a) (a)
  17. */
  18. #define FROMRAD(a) ((a)*(180.0/PI))
  19. #define TORAD(a) ((a)*(PI/180.0))
  20. static void math_abs (void)
  21. {
  22. double d = luaL_check_number(1);
  23. if (d < 0) d = -d;
  24. lua_pushnumber(d);
  25. }
  26. static void math_sin (void)
  27. {
  28. lua_pushnumber(sin(TORAD(luaL_check_number(1))));
  29. }
  30. static void math_cos (void)
  31. {
  32. lua_pushnumber(cos(TORAD(luaL_check_number(1))));
  33. }
  34. static void math_tan (void)
  35. {
  36. lua_pushnumber(tan(TORAD(luaL_check_number(1))));
  37. }
  38. static void math_asin (void)
  39. {
  40. lua_pushnumber(FROMRAD(asin(luaL_check_number(1))));
  41. }
  42. static void math_acos (void)
  43. {
  44. lua_pushnumber(FROMRAD(acos(luaL_check_number(1))));
  45. }
  46. static void math_atan (void)
  47. {
  48. lua_pushnumber(FROMRAD(atan(luaL_check_number(1))));
  49. }
  50. static void math_atan2 (void)
  51. {
  52. lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2))));
  53. }
  54. static void math_ceil (void)
  55. {
  56. lua_pushnumber(ceil(luaL_check_number(1)));
  57. }
  58. static void math_floor (void)
  59. {
  60. lua_pushnumber(floor(luaL_check_number(1)));
  61. }
  62. static void math_mod (void)
  63. {
  64. lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
  65. }
  66. static void math_sqrt (void)
  67. {
  68. lua_pushnumber(sqrt(luaL_check_number(1)));
  69. }
  70. static void math_pow (void)
  71. {
  72. lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
  73. }
  74. static void math_log (void)
  75. {
  76. lua_pushnumber(log(luaL_check_number(1)));
  77. }
  78. static void math_log10 (void)
  79. {
  80. lua_pushnumber(log10(luaL_check_number(1)));
  81. }
  82. static void math_exp (void)
  83. {
  84. lua_pushnumber(exp(luaL_check_number(1)));
  85. }
  86. static void math_deg (void)
  87. {
  88. lua_pushnumber(luaL_check_number(1)*(180.0/PI));
  89. }
  90. static void math_rad (void)
  91. {
  92. lua_pushnumber(luaL_check_number(1)*(PI/180.0));
  93. }
  94. static void math_frexp (void) {
  95. int e;
  96. lua_pushnumber(frexp(luaL_check_number(1), &e));
  97. lua_pushnumber(e);
  98. }
  99. static void math_ldexp (void) {
  100. lua_pushnumber(ldexp(luaL_check_number(1), luaL_check_int(2)));
  101. }
  102. static void math_min (void)
  103. {
  104. int i = 1;
  105. double dmin = luaL_check_number(i);
  106. while (lua_getparam(++i) != LUA_NOOBJECT) {
  107. double d = luaL_check_number(i);
  108. if (d < dmin)
  109. dmin = d;
  110. }
  111. lua_pushnumber(dmin);
  112. }
  113. static void math_max (void)
  114. {
  115. int i = 1;
  116. double dmax = luaL_check_number(i);
  117. while (lua_getparam(++i) != LUA_NOOBJECT) {
  118. double d = luaL_check_number(i);
  119. if (d > dmax)
  120. dmax = d;
  121. }
  122. lua_pushnumber(dmax);
  123. }
  124. static void math_random (void) {
  125. /* the '%' avoids the (rare) case of r==1, and is needed also because on
  126. some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */
  127. double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
  128. int l = luaL_opt_int(1, 0);
  129. if (l == 0)
  130. lua_pushnumber(r);
  131. else {
  132. int u = luaL_opt_int(2, 0);
  133. if (u == 0) {
  134. u = l;
  135. l = 1;
  136. }
  137. luaL_arg_check(l<=u, 1, "interval is empty");
  138. lua_pushnumber((int)(r*(u-l+1))+l);
  139. }
  140. }
  141. static void math_randomseed (void) {
  142. srand(luaL_check_int(1));
  143. }
  144. static struct luaL_reg mathlib[] = {
  145. {"abs", math_abs},
  146. {"sin", math_sin},
  147. {"cos", math_cos},
  148. {"tan", math_tan},
  149. {"asin", math_asin},
  150. {"acos", math_acos},
  151. {"atan", math_atan},
  152. {"atan2", math_atan2},
  153. {"ceil", math_ceil},
  154. {"floor", math_floor},
  155. {"mod", math_mod},
  156. {"frexp", math_frexp},
  157. {"ldexp", math_ldexp},
  158. {"sqrt", math_sqrt},
  159. {"min", math_min},
  160. {"max", math_max},
  161. {"log", math_log},
  162. {"log10", math_log10},
  163. {"exp", math_exp},
  164. {"deg", math_deg},
  165. {"rad", math_rad},
  166. {"random", math_random},
  167. {"randomseed", math_randomseed}
  168. };
  169. /*
  170. ** Open math library
  171. */
  172. void lua_mathlibopen (void)
  173. {
  174. luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
  175. lua_pushcfunction(math_pow);
  176. lua_pushnumber(0); /* to get its tag */
  177. lua_settagmethod(lua_tag(lua_pop()), "pow");
  178. lua_pushnumber(PI); lua_setglobal("PI");
  179. }