2
0

mathlib.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. ** mathlib.c
  3. ** Mathematics library to LUA
  4. */
  5. char *rcs_mathlib="$Id: mathlib.c,v 1.19 1997/03/17 17:01: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, "abs");
  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, "sin");
  25. lua_pushnumber (sin(TORAD(d)));
  26. }
  27. static void math_cos (void)
  28. {
  29. double d = luaL_check_number(1, "cos");
  30. lua_pushnumber (cos(TORAD(d)));
  31. }
  32. static void math_tan (void)
  33. {
  34. double d = luaL_check_number(1, "tan");
  35. lua_pushnumber (tan(TORAD(d)));
  36. }
  37. static void math_asin (void)
  38. {
  39. double d = luaL_check_number(1, "asin");
  40. lua_pushnumber (TODEGREE(asin(d)));
  41. }
  42. static void math_acos (void)
  43. {
  44. double d = luaL_check_number(1, "acos");
  45. lua_pushnumber (TODEGREE(acos(d)));
  46. }
  47. static void math_atan (void)
  48. {
  49. double d = luaL_check_number(1, "atan");
  50. lua_pushnumber (TODEGREE(atan(d)));
  51. }
  52. static void math_atan2 (void)
  53. {
  54. double d1 = luaL_check_number(1, "atan2");
  55. double d2 = luaL_check_number(2, "atan2");
  56. lua_pushnumber (TODEGREE(atan2(d1, d2)));
  57. }
  58. static void math_ceil (void)
  59. {
  60. double d = luaL_check_number(1, "ceil");
  61. lua_pushnumber (ceil(d));
  62. }
  63. static void math_floor (void)
  64. {
  65. double d = luaL_check_number(1, "floor");
  66. lua_pushnumber (floor(d));
  67. }
  68. static void math_mod (void)
  69. {
  70. float x = luaL_check_number(1, "mod");
  71. float y = luaL_check_number(2, "mod");
  72. lua_pushnumber(fmod(x, y));
  73. }
  74. static void math_sqrt (void)
  75. {
  76. double d = luaL_check_number(1, "sqrt");
  77. lua_pushnumber (sqrt(d));
  78. }
  79. static int old_pow;
  80. static void math_pow (void)
  81. {
  82. lua_Object o1 = lua_getparam (1);
  83. lua_Object o2 = lua_getparam (2);
  84. lua_Object op = lua_getparam(3);
  85. if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p')
  86. {
  87. lua_Object old = lua_getref(old_pow);
  88. lua_pushobject(o1);
  89. lua_pushobject(o2);
  90. lua_pushobject(op);
  91. if (lua_callfunction(old) != 0)
  92. lua_error(NULL);
  93. }
  94. else
  95. {
  96. double d1 = lua_getnumber(o1);
  97. double d2 = lua_getnumber(o2);
  98. lua_pushnumber (pow(d1,d2));
  99. }
  100. }
  101. static void math_min (void)
  102. {
  103. int i=1;
  104. double dmin = luaL_check_number(i, "min");
  105. while (lua_getparam(++i) != LUA_NOOBJECT)
  106. {
  107. double d = luaL_check_number(i, "min");
  108. if (d < dmin) 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, "max");
  116. while (lua_getparam(++i) != LUA_NOOBJECT)
  117. {
  118. double d = luaL_check_number(i, "max");
  119. if (d > dmax) dmax = d;
  120. }
  121. lua_pushnumber (dmax);
  122. }
  123. static void math_log (void)
  124. {
  125. double d = luaL_check_number(1, "log");
  126. lua_pushnumber (log(d));
  127. }
  128. static void math_log10 (void)
  129. {
  130. double d = luaL_check_number(1, "log10");
  131. lua_pushnumber (log10(d));
  132. }
  133. static void math_exp (void)
  134. {
  135. double d = luaL_check_number(1, "exp");
  136. lua_pushnumber (exp(d));
  137. }
  138. static void math_deg (void)
  139. {
  140. float d = luaL_check_number(1, "deg");
  141. lua_pushnumber (d*180./PI);
  142. }
  143. static void math_rad (void)
  144. {
  145. float d = luaL_check_number(1, "rad");
  146. lua_pushnumber (d/180.*PI);
  147. }
  148. static void math_random (void)
  149. {
  150. lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX);
  151. }
  152. static void math_randomseed (void)
  153. {
  154. srand(luaL_check_number(1, "randomseed"));
  155. }
  156. static struct luaL_reg mathlib[] = {
  157. {"abs", math_abs},
  158. {"sin", math_sin},
  159. {"cos", math_cos},
  160. {"tan", math_tan},
  161. {"asin", math_asin},
  162. {"acos", math_acos},
  163. {"atan", math_atan},
  164. {"atan2", math_atan2},
  165. {"ceil", math_ceil},
  166. {"floor", math_floor},
  167. {"mod", math_mod},
  168. {"sqrt", math_sqrt},
  169. {"min", math_min},
  170. {"max", math_max},
  171. {"log", math_log},
  172. {"log10", math_log10},
  173. {"exp", math_exp},
  174. {"deg", math_deg},
  175. {"rad", math_rad},
  176. {"random", math_random},
  177. {"randomseed", math_randomseed}
  178. };
  179. /*
  180. ** Open math library
  181. */
  182. void mathlib_open (void)
  183. {
  184. luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
  185. old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1);
  186. }