mathlib.c 5.4 KB

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