mathlib.c 5.3 KB

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