lmathlib.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. ** $Id: lmathlib.c,v 1.1 1997/09/16 19:25:59 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. #ifndef PI
  12. #define PI 3.14159265358979323846
  13. #endif
  14. static double torad = PI/180.0;
  15. #define FROMRAD(a) ((a)/torad)
  16. #define TORAD(a) ((a)*torad)
  17. static void modeang (void)
  18. {
  19. char *s = luaL_opt_string(1, "degree");
  20. switch (*s) {
  21. case 'd' : torad = PI/180.0; break;
  22. case 'r' : torad = 1.0; break;
  23. case 'g' : torad = PI/50.0; break;
  24. default: luaL_arg_check(0, 1, "invalid mode");
  25. }
  26. }
  27. static void math_abs (void)
  28. {
  29. double d = luaL_check_number(1);
  30. if (d < 0) d = -d;
  31. lua_pushnumber(d);
  32. }
  33. static void math_sin (void)
  34. {
  35. lua_pushnumber(sin(TORAD(luaL_check_number(1))));
  36. }
  37. static void math_cos (void)
  38. {
  39. lua_pushnumber(cos(TORAD(luaL_check_number(1))));
  40. }
  41. static void math_tan (void)
  42. {
  43. lua_pushnumber(tan(TORAD(luaL_check_number(1))));
  44. }
  45. static void math_asin (void)
  46. {
  47. lua_pushnumber(FROMRAD(asin(luaL_check_number(1))));
  48. }
  49. static void math_acos (void)
  50. {
  51. lua_pushnumber(FROMRAD(acos(luaL_check_number(1))));
  52. }
  53. static void math_atan (void)
  54. {
  55. lua_pushnumber(FROMRAD(atan(luaL_check_number(1))));
  56. }
  57. static void math_atan2 (void)
  58. {
  59. lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2))));
  60. }
  61. static void math_ceil (void)
  62. {
  63. lua_pushnumber(ceil(luaL_check_number(1)));
  64. }
  65. static void math_floor (void)
  66. {
  67. lua_pushnumber(floor(luaL_check_number(1)));
  68. }
  69. static void math_mod (void)
  70. {
  71. lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
  72. }
  73. static void math_sqrt (void)
  74. {
  75. lua_pushnumber(sqrt(luaL_check_number(1)));
  76. }
  77. static void math_pow (void)
  78. {
  79. lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
  80. }
  81. static void math_log (void)
  82. {
  83. lua_pushnumber(log(luaL_check_number(1)));
  84. }
  85. static void math_log10 (void)
  86. {
  87. lua_pushnumber(log10(luaL_check_number(1)));
  88. }
  89. static void math_exp (void)
  90. {
  91. lua_pushnumber(exp(luaL_check_number(1)));
  92. }
  93. static void math_deg (void)
  94. {
  95. lua_pushnumber(luaL_check_number(1)*180./PI);
  96. }
  97. static void math_rad (void)
  98. {
  99. lua_pushnumber(luaL_check_number(1)/180.*PI);
  100. }
  101. static void math_min (void)
  102. {
  103. int i = 1;
  104. double dmin = luaL_check_number(i);
  105. while (lua_getparam(++i) != LUA_NOOBJECT) {
  106. double d = luaL_check_number(i);
  107. if (d < dmin)
  108. dmin = d;
  109. }
  110. lua_pushnumber(dmin);
  111. }
  112. static void math_max (void)
  113. {
  114. int i = 1;
  115. double dmax = luaL_check_number(i);
  116. while (lua_getparam(++i) != LUA_NOOBJECT) {
  117. double d = luaL_check_number(i);
  118. if (d > dmax)
  119. dmax = d;
  120. }
  121. lua_pushnumber(dmax);
  122. }
  123. static void math_random (void)
  124. {
  125. double l = luaL_opt_number(1, 0);
  126. double r = (double)rand() / (double)RAND_MAX;
  127. if (l == 0)
  128. lua_pushnumber(r);
  129. else
  130. lua_pushnumber((int)(r*l)+1);
  131. }
  132. static void math_randomseed (void)
  133. {
  134. srand(luaL_check_number(1));
  135. }
  136. static struct luaL_reg mathlib[] = {
  137. {"modeang", modeang},
  138. {"abs", math_abs},
  139. {"sin", math_sin},
  140. {"cos", math_cos},
  141. {"tan", math_tan},
  142. {"asin", math_asin},
  143. {"acos", math_acos},
  144. {"atan", math_atan},
  145. {"atan2", math_atan2},
  146. {"ceil", math_ceil},
  147. {"floor", math_floor},
  148. {"mod", math_mod},
  149. {"sqrt", math_sqrt},
  150. {"min", math_min},
  151. {"max", math_max},
  152. {"log", math_log},
  153. {"log10", math_log10},
  154. {"exp", math_exp},
  155. {"deg", math_deg},
  156. {"rad", math_rad},
  157. {"random", math_random},
  158. {"randomseed", math_randomseed}
  159. };
  160. /*
  161. ** Open math library
  162. */
  163. void lua_mathlibopen (void)
  164. {
  165. luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
  166. lua_pushcfunction(math_pow);
  167. lua_pushnumber(0); /* to get its tag */
  168. lua_settagmethod(lua_tag(lua_pop()), "pow");
  169. lua_pushnumber(PI); lua_setglobal("PI");
  170. }