mathlib.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. ** mathlib.c
  3. ** Mathematics library to LUA
  4. */
  5. char *rcs_mathlib="$Id: mathlib.c,v 1.2 1994/07/20 22:12:27 celes Exp celes $";
  6. #include <stdio.h> /* NULL */
  7. #include <math.h>
  8. #include "lualib.h"
  9. #include "lua.h"
  10. #define PI (3.141592653589793)
  11. #define TODEGREE(a) ((a)*180.0/PI)
  12. #define TORAD(a) ((a)*PI/180.0)
  13. static void math_abs (void)
  14. {
  15. double d;
  16. lua_Object o = lua_getparam (1);
  17. if (o == NULL)
  18. { lua_error ("too few arguments to function `abs'"); return; }
  19. if (!lua_isnumber(o))
  20. { lua_error ("incorrect arguments to function `abs'"); return; }
  21. d = lua_getnumber(o);
  22. if (d < 0) d = -d;
  23. lua_pushnumber (d);
  24. }
  25. static void math_sin (void)
  26. {
  27. double d;
  28. lua_Object o = lua_getparam (1);
  29. if (o == NULL)
  30. { lua_error ("too few arguments to function `sin'"); return; }
  31. if (!lua_isnumber(o))
  32. { lua_error ("incorrect arguments to function `sin'"); return; }
  33. d = lua_getnumber(o);
  34. lua_pushnumber (sin(TORAD(d)));
  35. }
  36. static void math_cos (void)
  37. {
  38. double d;
  39. lua_Object o = lua_getparam (1);
  40. if (o == NULL)
  41. { lua_error ("too few arguments to function `cos'"); return; }
  42. if (!lua_isnumber(o))
  43. { lua_error ("incorrect arguments to function `cos'"); return; }
  44. d = lua_getnumber(o);
  45. lua_pushnumber (cos(TORAD(d)));
  46. }
  47. static void math_tan (void)
  48. {
  49. double d;
  50. lua_Object o = lua_getparam (1);
  51. if (o == NULL)
  52. { lua_error ("too few arguments to function `tan'"); return; }
  53. if (!lua_isnumber(o))
  54. { lua_error ("incorrect arguments to function `tan'"); return; }
  55. d = lua_getnumber(o);
  56. lua_pushnumber (tan(TORAD(d)));
  57. }
  58. static void math_asin (void)
  59. {
  60. double d;
  61. lua_Object o = lua_getparam (1);
  62. if (o == NULL)
  63. { lua_error ("too few arguments to function `asin'"); return; }
  64. if (!lua_isnumber(o))
  65. { lua_error ("incorrect arguments to function `asin'"); return; }
  66. d = lua_getnumber(o);
  67. lua_pushnumber (TODEGREE(asin(d)));
  68. }
  69. static void math_acos (void)
  70. {
  71. double d;
  72. lua_Object o = lua_getparam (1);
  73. if (o == NULL)
  74. { lua_error ("too few arguments to function `acos'"); return; }
  75. if (!lua_isnumber(o))
  76. { lua_error ("incorrect arguments to function `acos'"); return; }
  77. d = lua_getnumber(o);
  78. lua_pushnumber (TODEGREE(acos(d)));
  79. }
  80. static void math_atan (void)
  81. {
  82. double d;
  83. lua_Object o = lua_getparam (1);
  84. if (o == NULL)
  85. { lua_error ("too few arguments to function `atan'"); return; }
  86. if (!lua_isnumber(o))
  87. { lua_error ("incorrect arguments to function `atan'"); return; }
  88. d = lua_getnumber(o);
  89. lua_pushnumber (TODEGREE(atan(d)));
  90. }
  91. static void math_ceil (void)
  92. {
  93. double d;
  94. lua_Object o = lua_getparam (1);
  95. if (o == NULL)
  96. { lua_error ("too few arguments to function `ceil'"); return; }
  97. if (!lua_isnumber(o))
  98. { lua_error ("incorrect arguments to function `ceil'"); return; }
  99. d = lua_getnumber(o);
  100. lua_pushnumber (ceil(d));
  101. }
  102. static void math_floor (void)
  103. {
  104. double d;
  105. lua_Object o = lua_getparam (1);
  106. if (o == NULL)
  107. { lua_error ("too few arguments to function `floor'"); return; }
  108. if (!lua_isnumber(o))
  109. { lua_error ("incorrect arguments to function `floor'"); return; }
  110. d = lua_getnumber(o);
  111. lua_pushnumber (floor(d));
  112. }
  113. static void math_mod (void)
  114. {
  115. int d1, d2;
  116. lua_Object o1 = lua_getparam (1);
  117. lua_Object o2 = lua_getparam (2);
  118. if (!lua_isnumber(o1) || !lua_isnumber(o2))
  119. { lua_error ("incorrect arguments to function `mod'"); return; }
  120. d1 = (int) lua_getnumber(o1);
  121. d2 = (int) lua_getnumber(o2);
  122. lua_pushnumber (d1%d2);
  123. }
  124. static void math_sqrt (void)
  125. {
  126. double d;
  127. lua_Object o = lua_getparam (1);
  128. if (o == NULL)
  129. { lua_error ("too few arguments to function `sqrt'"); return; }
  130. if (!lua_isnumber(o))
  131. { lua_error ("incorrect arguments to function `sqrt'"); return; }
  132. d = lua_getnumber(o);
  133. lua_pushnumber (sqrt(d));
  134. }
  135. static void math_pow (void)
  136. {
  137. double d1, d2;
  138. lua_Object o1 = lua_getparam (1);
  139. lua_Object o2 = lua_getparam (2);
  140. if (!lua_isnumber(o1) || !lua_isnumber(o2))
  141. { lua_error ("incorrect arguments to function `pow'"); return; }
  142. d1 = lua_getnumber(o1);
  143. d2 = lua_getnumber(o2);
  144. lua_pushnumber (pow(d1,d2));
  145. }
  146. static void math_min (void)
  147. {
  148. int i=1;
  149. double d, dmin;
  150. lua_Object o;
  151. if ((o = lua_getparam(i++)) == NULL)
  152. { lua_error ("too few arguments to function `min'"); return; }
  153. if (!lua_isnumber(o))
  154. { lua_error ("incorrect arguments to function `min'"); return; }
  155. dmin = lua_getnumber (o);
  156. while ((o = lua_getparam(i++)) != NULL)
  157. {
  158. if (!lua_isnumber(o))
  159. { lua_error ("incorrect arguments to function `min'"); return; }
  160. d = lua_getnumber (o);
  161. if (d < dmin) dmin = d;
  162. }
  163. lua_pushnumber (dmin);
  164. }
  165. static void math_max (void)
  166. {
  167. int i=1;
  168. double d, dmax;
  169. lua_Object o;
  170. if ((o = lua_getparam(i++)) == NULL)
  171. { lua_error ("too few arguments to function `max'"); return; }
  172. if (!lua_isnumber(o))
  173. { lua_error ("incorrect arguments to function `max'"); return; }
  174. dmax = lua_getnumber (o);
  175. while ((o = lua_getparam(i++)) != NULL)
  176. {
  177. if (!lua_isnumber(o))
  178. { lua_error ("incorrect arguments to function `max'"); return; }
  179. d = lua_getnumber (o);
  180. if (d > dmax) dmax = d;
  181. }
  182. lua_pushnumber (dmax);
  183. }
  184. static void math_log (void)
  185. {
  186. double d;
  187. lua_Object o = lua_getparam (1);
  188. if (o == NULL)
  189. { lua_error ("too few arguments to function `log'"); return; }
  190. if (!lua_isnumber(o))
  191. { lua_error ("incorrect arguments to function `log'"); return; }
  192. d = lua_getnumber(o);
  193. lua_pushnumber (log(d));
  194. }
  195. static void math_log10 (void)
  196. {
  197. double d;
  198. lua_Object o = lua_getparam (1);
  199. if (o == NULL)
  200. { lua_error ("too few arguments to function `log10'"); return; }
  201. if (!lua_isnumber(o))
  202. { lua_error ("incorrect arguments to function `log10'"); return; }
  203. d = lua_getnumber(o);
  204. lua_pushnumber (log10(d));
  205. }
  206. static void math_exp (void)
  207. {
  208. double d;
  209. lua_Object o = lua_getparam (1);
  210. if (o == NULL)
  211. { lua_error ("too few arguments to function `exp'"); return; }
  212. if (!lua_isnumber(o))
  213. { lua_error ("incorrect arguments to function `exp'"); return; }
  214. d = lua_getnumber(o);
  215. lua_pushnumber (exp(d));
  216. }
  217. /*
  218. ** Open math library
  219. */
  220. void mathlib_open (void)
  221. {
  222. lua_register ("abs", math_abs);
  223. lua_register ("sin", math_sin);
  224. lua_register ("cos", math_cos);
  225. lua_register ("tan", math_tan);
  226. lua_register ("asin", math_asin);
  227. lua_register ("acos", math_acos);
  228. lua_register ("atan", math_atan);
  229. lua_register ("ceil", math_ceil);
  230. lua_register ("floor", math_floor);
  231. lua_register ("mod", math_mod);
  232. lua_register ("sqrt", math_sqrt);
  233. lua_register ("pow", math_pow);
  234. lua_register ("min", math_min);
  235. lua_register ("max", math_max);
  236. lua_register ("log", math_log);
  237. lua_register ("log10", math_log10);
  238. lua_register ("exp", math_exp);
  239. }