mathlib.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. ** mathlib.c
  3. ** Mathematics library to LUA
  4. */
  5. char *rcs_mathlib="$Id: mathlib.c,v 1.8 1995/01/04 18:49:54 roberto Exp $";
  6. #include <stdio.h> /* NULL */
  7. #include <math.h>
  8. #include "lualib.h"
  9. #include "lua.h"
  10. #define PI 3.14159265358979323846
  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 == LUA_NOOBJECT)
  18. lua_error ("too few arguments to function `abs'");
  19. if (!lua_isnumber(o))
  20. lua_error ("incorrect arguments to function `abs'");
  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 == LUA_NOOBJECT)
  30. lua_error ("too few arguments to function `sin'");
  31. if (!lua_isnumber(o))
  32. lua_error ("incorrect arguments to function `sin'");
  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 == LUA_NOOBJECT)
  41. lua_error ("too few arguments to function `cos'");
  42. if (!lua_isnumber(o))
  43. lua_error ("incorrect arguments to function `cos'");
  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 == LUA_NOOBJECT)
  52. lua_error ("too few arguments to function `tan'");
  53. if (!lua_isnumber(o))
  54. lua_error ("incorrect arguments to function `tan'");
  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 == LUA_NOOBJECT)
  63. lua_error ("too few arguments to function `asin'");
  64. if (!lua_isnumber(o))
  65. lua_error ("incorrect arguments to function `asin'");
  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 == LUA_NOOBJECT)
  74. lua_error ("too few arguments to function `acos'");
  75. if (!lua_isnumber(o))
  76. lua_error ("incorrect arguments to function `acos'");
  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 == LUA_NOOBJECT)
  85. lua_error ("too few arguments to function `atan'");
  86. if (!lua_isnumber(o))
  87. lua_error ("incorrect arguments to function `atan'");
  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 == LUA_NOOBJECT)
  96. lua_error ("too few arguments to function `ceil'");
  97. if (!lua_isnumber(o))
  98. lua_error ("incorrect arguments to function `ceil'");
  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 == LUA_NOOBJECT)
  107. lua_error ("too few arguments to function `floor'");
  108. if (!lua_isnumber(o))
  109. lua_error ("incorrect arguments to function `floor'");
  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'");
  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 == LUA_NOOBJECT)
  129. lua_error ("too few arguments to function `sqrt'");
  130. if (!lua_isnumber(o))
  131. lua_error ("incorrect arguments to function `sqrt'");
  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_Object old = lua_getlocked(old_pow);
  144. lua_pushobject(o1);
  145. lua_pushobject(o2);
  146. lua_pushobject(op);
  147. if (lua_callfunction(old) != 0)
  148. lua_error(NULL);
  149. }
  150. else
  151. {
  152. double d1 = lua_getnumber(o1);
  153. double d2 = lua_getnumber(o2);
  154. lua_pushnumber (pow(d1,d2));
  155. }
  156. }
  157. static void math_min (void)
  158. {
  159. int i=1;
  160. double d, dmin;
  161. lua_Object o;
  162. if ((o = lua_getparam(i++)) == LUA_NOOBJECT)
  163. lua_error ("too few arguments to function `min'");
  164. if (!lua_isnumber(o))
  165. lua_error ("incorrect arguments to function `min'");
  166. dmin = lua_getnumber (o);
  167. while ((o = lua_getparam(i++)) != LUA_NOOBJECT)
  168. {
  169. if (!lua_isnumber(o))
  170. lua_error ("incorrect arguments to function `min'");
  171. d = lua_getnumber (o);
  172. if (d < dmin) dmin = d;
  173. }
  174. lua_pushnumber (dmin);
  175. }
  176. static void math_max (void)
  177. {
  178. int i=1;
  179. double d, dmax;
  180. lua_Object o;
  181. if ((o = lua_getparam(i++)) == LUA_NOOBJECT)
  182. lua_error ("too few arguments to function `max'");
  183. if (!lua_isnumber(o))
  184. lua_error ("incorrect arguments to function `max'");
  185. dmax = lua_getnumber (o);
  186. while ((o = lua_getparam(i++)) != LUA_NOOBJECT)
  187. {
  188. if (!lua_isnumber(o))
  189. lua_error ("incorrect arguments to function `max'");
  190. d = lua_getnumber (o);
  191. if (d > dmax) dmax = d;
  192. }
  193. lua_pushnumber (dmax);
  194. }
  195. static void math_log (void)
  196. {
  197. double d;
  198. lua_Object o = lua_getparam (1);
  199. if (o == LUA_NOOBJECT)
  200. lua_error ("too few arguments to function `log'");
  201. if (!lua_isnumber(o))
  202. lua_error ("incorrect arguments to function `log'");
  203. d = lua_getnumber(o);
  204. lua_pushnumber (log(d));
  205. }
  206. static void math_log10 (void)
  207. {
  208. double d;
  209. lua_Object o = lua_getparam (1);
  210. if (o == LUA_NOOBJECT)
  211. lua_error ("too few arguments to function `log10'");
  212. if (!lua_isnumber(o))
  213. lua_error ("incorrect arguments to function `log10'");
  214. d = lua_getnumber(o);
  215. lua_pushnumber (log10(d));
  216. }
  217. static void math_exp (void)
  218. {
  219. double d;
  220. lua_Object o = lua_getparam (1);
  221. if (o == LUA_NOOBJECT)
  222. lua_error ("too few arguments to function `exp'");
  223. if (!lua_isnumber(o))
  224. lua_error ("incorrect arguments to function `exp'");
  225. d = lua_getnumber(o);
  226. lua_pushnumber (exp(d));
  227. }
  228. static void math_deg (void)
  229. {
  230. float d;
  231. lua_Object o = lua_getparam (1);
  232. if (o == LUA_NOOBJECT)
  233. lua_error ("too few arguments to function `deg'");
  234. if (!lua_isnumber(o))
  235. lua_error ("incorrect arguments to function `deg'");
  236. d = lua_getnumber(o);
  237. lua_pushnumber (d*180./PI);
  238. }
  239. static void math_rad (void)
  240. {
  241. float d;
  242. lua_Object o = lua_getparam (1);
  243. if (o == LUA_NOOBJECT)
  244. lua_error ("too few arguments to function `rad'");
  245. if (!lua_isnumber(o))
  246. lua_error ("incorrect arguments to function `rad'");
  247. d = lua_getnumber(o);
  248. lua_pushnumber (d/180.*PI);
  249. }
  250. /*
  251. ** Open math library
  252. */
  253. void mathlib_open (void)
  254. {
  255. lua_register ("abs", math_abs);
  256. lua_register ("sin", math_sin);
  257. lua_register ("cos", math_cos);
  258. lua_register ("tan", math_tan);
  259. lua_register ("asin", math_asin);
  260. lua_register ("acos", math_acos);
  261. lua_register ("atan", math_atan);
  262. lua_register ("ceil", math_ceil);
  263. lua_register ("floor", math_floor);
  264. lua_register ("mod", math_mod);
  265. lua_register ("sqrt", math_sqrt);
  266. lua_register ("min", math_min);
  267. lua_register ("max", math_max);
  268. lua_register ("log", math_log);
  269. lua_register ("log10", math_log10);
  270. lua_register ("exp", math_exp);
  271. lua_register ("deg", math_deg);
  272. lua_register ("rad", math_rad);
  273. old_pow = lua_lockobject(lua_setfallback("arith", math_pow));
  274. }