mathlib.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. ** mathlib.c
  3. ** Mathematics library to LUA
  4. */
  5. char *rcs_mathlib="$Id: mathlib.c,v 1.10 1995/10/02 17:03:33 roberto Exp roberto $";
  6. #include <stdio.h> /* NULL */
  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;
  18. lua_Object o = lua_getparam (1);
  19. if (o == LUA_NOOBJECT)
  20. lua_error ("too few arguments to function `abs'");
  21. if (!lua_isnumber(o))
  22. lua_error ("incorrect arguments to function `abs'");
  23. d = lua_getnumber(o);
  24. if (d < 0) d = -d;
  25. lua_pushnumber (d);
  26. }
  27. static void math_sin (void)
  28. {
  29. double d;
  30. lua_Object o = lua_getparam (1);
  31. if (o == LUA_NOOBJECT)
  32. lua_error ("too few arguments to function `sin'");
  33. if (!lua_isnumber(o))
  34. lua_error ("incorrect arguments to function `sin'");
  35. d = lua_getnumber(o);
  36. lua_pushnumber (sin(TORAD(d)));
  37. }
  38. static void math_cos (void)
  39. {
  40. double d;
  41. lua_Object o = lua_getparam (1);
  42. if (o == LUA_NOOBJECT)
  43. lua_error ("too few arguments to function `cos'");
  44. if (!lua_isnumber(o))
  45. lua_error ("incorrect arguments to function `cos'");
  46. d = lua_getnumber(o);
  47. lua_pushnumber (cos(TORAD(d)));
  48. }
  49. static void math_tan (void)
  50. {
  51. double d;
  52. lua_Object o = lua_getparam (1);
  53. if (o == LUA_NOOBJECT)
  54. lua_error ("too few arguments to function `tan'");
  55. if (!lua_isnumber(o))
  56. lua_error ("incorrect arguments to function `tan'");
  57. d = lua_getnumber(o);
  58. lua_pushnumber (tan(TORAD(d)));
  59. }
  60. static void math_asin (void)
  61. {
  62. double d;
  63. lua_Object o = lua_getparam (1);
  64. if (o == LUA_NOOBJECT)
  65. lua_error ("too few arguments to function `asin'");
  66. if (!lua_isnumber(o))
  67. lua_error ("incorrect arguments to function `asin'");
  68. d = lua_getnumber(o);
  69. lua_pushnumber (TODEGREE(asin(d)));
  70. }
  71. static void math_acos (void)
  72. {
  73. double d;
  74. lua_Object o = lua_getparam (1);
  75. if (o == LUA_NOOBJECT)
  76. lua_error ("too few arguments to function `acos'");
  77. if (!lua_isnumber(o))
  78. lua_error ("incorrect arguments to function `acos'");
  79. d = lua_getnumber(o);
  80. lua_pushnumber (TODEGREE(acos(d)));
  81. }
  82. static void math_atan (void)
  83. {
  84. double d;
  85. lua_Object o = lua_getparam (1);
  86. if (o == LUA_NOOBJECT)
  87. lua_error ("too few arguments to function `atan'");
  88. if (!lua_isnumber(o))
  89. lua_error ("incorrect arguments to function `atan'");
  90. d = lua_getnumber(o);
  91. lua_pushnumber (TODEGREE(atan(d)));
  92. }
  93. static void math_atan2 (void)
  94. {
  95. int d1, d2;
  96. lua_Object o1 = lua_getparam (1);
  97. lua_Object o2 = lua_getparam (2);
  98. if (!lua_isnumber(o1) || !lua_isnumber(o2))
  99. lua_error ("incorrect arguments to function `atan2'");
  100. d1 = (int) lua_getnumber(o1);
  101. d2 = (int) lua_getnumber(o2);
  102. lua_pushnumber (TODEGREE(atan2(d1, d2)));
  103. }
  104. static void math_ceil (void)
  105. {
  106. double d;
  107. lua_Object o = lua_getparam (1);
  108. if (o == LUA_NOOBJECT)
  109. lua_error ("too few arguments to function `ceil'");
  110. if (!lua_isnumber(o))
  111. lua_error ("incorrect arguments to function `ceil'");
  112. d = lua_getnumber(o);
  113. lua_pushnumber (ceil(d));
  114. }
  115. static void math_floor (void)
  116. {
  117. double d;
  118. lua_Object o = lua_getparam (1);
  119. if (o == LUA_NOOBJECT)
  120. lua_error ("too few arguments to function `floor'");
  121. if (!lua_isnumber(o))
  122. lua_error ("incorrect arguments to function `floor'");
  123. d = lua_getnumber(o);
  124. lua_pushnumber (floor(d));
  125. }
  126. static void math_mod (void)
  127. {
  128. int d1, d2;
  129. lua_Object o1 = lua_getparam (1);
  130. lua_Object o2 = lua_getparam (2);
  131. if (!lua_isnumber(o1) || !lua_isnumber(o2))
  132. lua_error ("incorrect arguments to function `mod'");
  133. d1 = (int) lua_getnumber(o1);
  134. d2 = (int) lua_getnumber(o2);
  135. lua_pushnumber (d1%d2);
  136. }
  137. static void math_sqrt (void)
  138. {
  139. double d;
  140. lua_Object o = lua_getparam (1);
  141. if (o == LUA_NOOBJECT)
  142. lua_error ("too few arguments to function `sqrt'");
  143. if (!lua_isnumber(o))
  144. lua_error ("incorrect arguments to function `sqrt'");
  145. d = lua_getnumber(o);
  146. lua_pushnumber (sqrt(d));
  147. }
  148. static int old_pow;
  149. static void math_pow (void)
  150. {
  151. lua_Object o1 = lua_getparam (1);
  152. lua_Object o2 = lua_getparam (2);
  153. lua_Object op = lua_getparam(3);
  154. if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p')
  155. {
  156. lua_Object old = lua_getlocked(old_pow);
  157. lua_pushobject(o1);
  158. lua_pushobject(o2);
  159. lua_pushobject(op);
  160. if (lua_callfunction(old) != 0)
  161. lua_error(NULL);
  162. }
  163. else
  164. {
  165. double d1 = lua_getnumber(o1);
  166. double d2 = lua_getnumber(o2);
  167. lua_pushnumber (pow(d1,d2));
  168. }
  169. }
  170. static void math_min (void)
  171. {
  172. int i=1;
  173. double d, dmin;
  174. lua_Object o;
  175. if ((o = lua_getparam(i++)) == LUA_NOOBJECT)
  176. lua_error ("too few arguments to function `min'");
  177. if (!lua_isnumber(o))
  178. lua_error ("incorrect arguments to function `min'");
  179. dmin = lua_getnumber (o);
  180. while ((o = lua_getparam(i++)) != LUA_NOOBJECT)
  181. {
  182. if (!lua_isnumber(o))
  183. lua_error ("incorrect arguments to function `min'");
  184. d = lua_getnumber (o);
  185. if (d < dmin) dmin = d;
  186. }
  187. lua_pushnumber (dmin);
  188. }
  189. static void math_max (void)
  190. {
  191. int i=1;
  192. double d, dmax;
  193. lua_Object o;
  194. if ((o = lua_getparam(i++)) == LUA_NOOBJECT)
  195. lua_error ("too few arguments to function `max'");
  196. if (!lua_isnumber(o))
  197. lua_error ("incorrect arguments to function `max'");
  198. dmax = lua_getnumber (o);
  199. while ((o = lua_getparam(i++)) != LUA_NOOBJECT)
  200. {
  201. if (!lua_isnumber(o))
  202. lua_error ("incorrect arguments to function `max'");
  203. d = lua_getnumber (o);
  204. if (d > dmax) dmax = d;
  205. }
  206. lua_pushnumber (dmax);
  207. }
  208. static void math_log (void)
  209. {
  210. double d;
  211. lua_Object o = lua_getparam (1);
  212. if (o == LUA_NOOBJECT)
  213. lua_error ("too few arguments to function `log'");
  214. if (!lua_isnumber(o))
  215. lua_error ("incorrect arguments to function `log'");
  216. d = lua_getnumber(o);
  217. lua_pushnumber (log(d));
  218. }
  219. static void math_log10 (void)
  220. {
  221. double d;
  222. lua_Object o = lua_getparam (1);
  223. if (o == LUA_NOOBJECT)
  224. lua_error ("too few arguments to function `log10'");
  225. if (!lua_isnumber(o))
  226. lua_error ("incorrect arguments to function `log10'");
  227. d = lua_getnumber(o);
  228. lua_pushnumber (log10(d));
  229. }
  230. static void math_exp (void)
  231. {
  232. double d;
  233. lua_Object o = lua_getparam (1);
  234. if (o == LUA_NOOBJECT)
  235. lua_error ("too few arguments to function `exp'");
  236. if (!lua_isnumber(o))
  237. lua_error ("incorrect arguments to function `exp'");
  238. d = lua_getnumber(o);
  239. lua_pushnumber (exp(d));
  240. }
  241. static void math_deg (void)
  242. {
  243. float d;
  244. lua_Object o = lua_getparam (1);
  245. if (o == LUA_NOOBJECT)
  246. lua_error ("too few arguments to function `deg'");
  247. if (!lua_isnumber(o))
  248. lua_error ("incorrect arguments to function `deg'");
  249. d = lua_getnumber(o);
  250. lua_pushnumber (d*180./PI);
  251. }
  252. static void math_rad (void)
  253. {
  254. float d;
  255. lua_Object o = lua_getparam (1);
  256. if (o == LUA_NOOBJECT)
  257. lua_error ("too few arguments to function `rad'");
  258. if (!lua_isnumber(o))
  259. lua_error ("incorrect arguments to function `rad'");
  260. d = lua_getnumber(o);
  261. lua_pushnumber (d/180.*PI);
  262. }
  263. /*
  264. ** Open math library
  265. */
  266. void mathlib_open (void)
  267. {
  268. lua_register ("abs", math_abs);
  269. lua_register ("sin", math_sin);
  270. lua_register ("cos", math_cos);
  271. lua_register ("tan", math_tan);
  272. lua_register ("asin", math_asin);
  273. lua_register ("acos", math_acos);
  274. lua_register ("atan", math_atan);
  275. lua_register ("atan2", math_atan2);
  276. lua_register ("ceil", math_ceil);
  277. lua_register ("floor", math_floor);
  278. lua_register ("mod", math_mod);
  279. lua_register ("sqrt", math_sqrt);
  280. lua_register ("min", math_min);
  281. lua_register ("max", math_max);
  282. lua_register ("log", math_log);
  283. lua_register ("log10", math_log10);
  284. lua_register ("exp", math_exp);
  285. lua_register ("deg", math_deg);
  286. lua_register ("rad", math_rad);
  287. old_pow = lua_lockobject(lua_setfallback("arith", math_pow));
  288. }