lmathlib.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. ** $Id: lmathlib.c,v 1.4 1997/11/04 15:27:53 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. #define FROMRAD(a) ((a)/torad())
  15. #define TORAD(a) ((a)*torad())
  16. static double torad (void)
  17. {
  18. char *s = lua_getstring(lua_getglobal("_TRIGMODE"));
  19. switch (*s) {
  20. case 'd' : return PI/180.0;
  21. case 'r' : return 1.0;
  22. case 'g' : return PI/50.0;
  23. default:
  24. luaL_verror("invalid _TRIGMODE (`%.50s')", s);
  25. return 0; /* to avoid warnings */
  26. }
  27. }
  28. static void math_abs (void)
  29. {
  30. double d = luaL_check_number(1);
  31. if (d < 0) d = -d;
  32. lua_pushnumber(d);
  33. }
  34. static void math_sin (void)
  35. {
  36. lua_pushnumber(sin(TORAD(luaL_check_number(1))));
  37. }
  38. static void math_cos (void)
  39. {
  40. lua_pushnumber(cos(TORAD(luaL_check_number(1))));
  41. }
  42. static void math_tan (void)
  43. {
  44. lua_pushnumber(tan(TORAD(luaL_check_number(1))));
  45. }
  46. static void math_asin (void)
  47. {
  48. lua_pushnumber(FROMRAD(asin(luaL_check_number(1))));
  49. }
  50. static void math_acos (void)
  51. {
  52. lua_pushnumber(FROMRAD(acos(luaL_check_number(1))));
  53. }
  54. static void math_atan (void)
  55. {
  56. lua_pushnumber(FROMRAD(atan(luaL_check_number(1))));
  57. }
  58. static void math_atan2 (void)
  59. {
  60. lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2))));
  61. }
  62. static void math_ceil (void)
  63. {
  64. lua_pushnumber(ceil(luaL_check_number(1)));
  65. }
  66. static void math_floor (void)
  67. {
  68. lua_pushnumber(floor(luaL_check_number(1)));
  69. }
  70. static void math_mod (void)
  71. {
  72. lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
  73. }
  74. static void math_sqrt (void)
  75. {
  76. lua_pushnumber(sqrt(luaL_check_number(1)));
  77. }
  78. static void math_pow (void)
  79. {
  80. lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
  81. }
  82. static void math_log (void)
  83. {
  84. lua_pushnumber(log(luaL_check_number(1)));
  85. }
  86. static void math_log10 (void)
  87. {
  88. lua_pushnumber(log10(luaL_check_number(1)));
  89. }
  90. static void math_exp (void)
  91. {
  92. lua_pushnumber(exp(luaL_check_number(1)));
  93. }
  94. static void math_deg (void)
  95. {
  96. lua_pushnumber(luaL_check_number(1)*180./PI);
  97. }
  98. static void math_rad (void)
  99. {
  100. lua_pushnumber(luaL_check_number(1)/180.*PI);
  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. {
  126. double l = luaL_opt_number(1, 0);
  127. double r = (double)rand() / (double)RAND_MAX;
  128. if (l == 0)
  129. lua_pushnumber(r);
  130. else
  131. lua_pushnumber((int)(r*l)+1);
  132. }
  133. static void math_randomseed (void)
  134. {
  135. srand(luaL_check_number(1));
  136. }
  137. static struct luaL_reg mathlib[] = {
  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_pushstring("deg"); lua_setglobal("_TRIGMODE");
  167. lua_pushcfunction(math_pow);
  168. lua_pushnumber(0); /* to get its tag */
  169. lua_settagmethod(lua_tag(lua_pop()), "pow");
  170. lua_pushnumber(PI); lua_setglobal("PI");
  171. }