mathlib.c 4.0 KB

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