mathlib.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. ** mathlib.c
  3. ** Mathematics library to LUA
  4. */
  5. char *rcs_mathlib="$Id: mathlib.c,v 1.4 1994/10/11 13:06:47 celes Exp roberto $";
  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 int old_pow;
  136. static void math_pow (void)
  137. {
  138. lua_Object o1 = lua_getparam (1);
  139. lua_Object o2 = lua_getparam (2);
  140. lua_Object op = lua_getparam(3);
  141. if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p')
  142. {
  143. lua_pushobject(o1);
  144. lua_pushobject(o2);
  145. lua_pushobject(op);
  146. if (lua_callfunction(lua_getlocked(old_pow)) != 0)
  147. lua_error(NULL);
  148. }
  149. else
  150. {
  151. double d1 = lua_getnumber(o1);
  152. double d2 = lua_getnumber(o2);
  153. lua_pushnumber (pow(d1,d2));
  154. }
  155. }
  156. static void math_min (void)
  157. {
  158. int i=1;
  159. double d, dmin;
  160. lua_Object o;
  161. if ((o = lua_getparam(i++)) == NULL)
  162. { lua_error ("too few arguments to function `min'"); return; }
  163. if (!lua_isnumber(o))
  164. { lua_error ("incorrect arguments to function `min'"); return; }
  165. dmin = lua_getnumber (o);
  166. while ((o = lua_getparam(i++)) != NULL)
  167. {
  168. if (!lua_isnumber(o))
  169. { lua_error ("incorrect arguments to function `min'"); return; }
  170. d = lua_getnumber (o);
  171. if (d < dmin) dmin = d;
  172. }
  173. lua_pushnumber (dmin);
  174. }
  175. static void math_max (void)
  176. {
  177. int i=1;
  178. double d, dmax;
  179. lua_Object o;
  180. if ((o = lua_getparam(i++)) == NULL)
  181. { lua_error ("too few arguments to function `max'"); return; }
  182. if (!lua_isnumber(o))
  183. { lua_error ("incorrect arguments to function `max'"); return; }
  184. dmax = lua_getnumber (o);
  185. while ((o = lua_getparam(i++)) != NULL)
  186. {
  187. if (!lua_isnumber(o))
  188. { lua_error ("incorrect arguments to function `max'"); return; }
  189. d = lua_getnumber (o);
  190. if (d > dmax) dmax = d;
  191. }
  192. lua_pushnumber (dmax);
  193. }
  194. static void math_log (void)
  195. {
  196. double d;
  197. lua_Object o = lua_getparam (1);
  198. if (o == NULL)
  199. { lua_error ("too few arguments to function `log'"); return; }
  200. if (!lua_isnumber(o))
  201. { lua_error ("incorrect arguments to function `log'"); return; }
  202. d = lua_getnumber(o);
  203. lua_pushnumber (log(d));
  204. }
  205. static void math_log10 (void)
  206. {
  207. double d;
  208. lua_Object o = lua_getparam (1);
  209. if (o == NULL)
  210. { lua_error ("too few arguments to function `log10'"); return; }
  211. if (!lua_isnumber(o))
  212. { lua_error ("incorrect arguments to function `log10'"); return; }
  213. d = lua_getnumber(o);
  214. lua_pushnumber (log10(d));
  215. }
  216. static void math_exp (void)
  217. {
  218. double d;
  219. lua_Object o = lua_getparam (1);
  220. if (o == NULL)
  221. { lua_error ("too few arguments to function `exp'"); return; }
  222. if (!lua_isnumber(o))
  223. { lua_error ("incorrect arguments to function `exp'"); return; }
  224. d = lua_getnumber(o);
  225. lua_pushnumber (exp(d));
  226. }
  227. static void math_deg (void)
  228. {
  229. float d;
  230. lua_Object o = lua_getparam (1);
  231. if (o == NULL)
  232. { lua_error ("too few arguments to function `deg'"); return; }
  233. if (!lua_isnumber(o))
  234. { lua_error ("incorrect arguments to function `deg'"); return; }
  235. d = lua_getnumber(o);
  236. lua_pushnumber (d*180./PI);
  237. }
  238. static void math_rad (void)
  239. {
  240. float d;
  241. lua_Object o = lua_getparam (1);
  242. if (o == NULL)
  243. { lua_error ("too few arguments to function `rad'"); return; }
  244. if (!lua_isnumber(o))
  245. { lua_error ("incorrect arguments to function `rad'"); return; }
  246. d = lua_getnumber(o);
  247. lua_pushnumber (d/180.*PI);
  248. }
  249. /*
  250. ** Open math library
  251. */
  252. void mathlib_open (void)
  253. {
  254. lua_register ("abs", math_abs);
  255. lua_register ("sin", math_sin);
  256. lua_register ("cos", math_cos);
  257. lua_register ("tan", math_tan);
  258. lua_register ("asin", math_asin);
  259. lua_register ("acos", math_acos);
  260. lua_register ("atan", math_atan);
  261. lua_register ("ceil", math_ceil);
  262. lua_register ("floor", math_floor);
  263. lua_register ("mod", math_mod);
  264. lua_register ("sqrt", math_sqrt);
  265. lua_register ("min", math_min);
  266. lua_register ("max", math_max);
  267. lua_register ("log", math_log);
  268. lua_register ("log10", math_log10);
  269. lua_register ("exp", math_exp);
  270. lua_register ("deg", math_deg);
  271. lua_register ("rad", math_rad);
  272. old_pow = lua_lock(lua_setfallback("arith", math_pow));
  273. }