mathlib.c 3.5 KB

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