mathlib.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. ** mathlib.c
  3. ** Mathematics library to LUA
  4. */
  5. char *rcs_mathlib="$Id: mathlib.c,v 1.11 1995/10/04 13:52:09 roberto Exp $";
  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 str_error(char *funcname)
  16. {
  17. char buff[250];
  18. sprintf(buff, "incorrect arguments to function `%s'", funcname);
  19. lua_error(buff);
  20. }
  21. static float get_and_check_float (int numArg, char *funcname)
  22. {
  23. lua_Object o = lua_getparam(numArg);
  24. if (!lua_isnumber(o))
  25. str_error(funcname);
  26. return lua_getnumber(o);
  27. }
  28. static void math_abs (void)
  29. {
  30. double d = get_and_check_float(1, "abs");
  31. if (d < 0) d = -d;
  32. lua_pushnumber (d);
  33. }
  34. static void math_sin (void)
  35. {
  36. double d = get_and_check_float(1, "sin");
  37. lua_pushnumber (sin(TORAD(d)));
  38. }
  39. static void math_cos (void)
  40. {
  41. double d = get_and_check_float(1, "cos");
  42. lua_pushnumber (cos(TORAD(d)));
  43. }
  44. static void math_tan (void)
  45. {
  46. double d = get_and_check_float(1, "tan");
  47. lua_pushnumber (tan(TORAD(d)));
  48. }
  49. static void math_asin (void)
  50. {
  51. double d = get_and_check_float(1, "asin");
  52. lua_pushnumber (TODEGREE(asin(d)));
  53. }
  54. static void math_acos (void)
  55. {
  56. double d = get_and_check_float(1, "acos");
  57. lua_pushnumber (TODEGREE(acos(d)));
  58. }
  59. static void math_atan (void)
  60. {
  61. double d = get_and_check_float(1, "atan");
  62. lua_pushnumber (TODEGREE(atan(d)));
  63. }
  64. static void math_atan2 (void)
  65. {
  66. double d1 = get_and_check_float(1, "atan2");
  67. double d2 = get_and_check_float(2, "atan2");
  68. lua_pushnumber (TODEGREE(atan2(d1, d2)));
  69. }
  70. static void math_ceil (void)
  71. {
  72. double d = get_and_check_float(1, "ceil");
  73. lua_pushnumber (ceil(d));
  74. }
  75. static void math_floor (void)
  76. {
  77. double d = get_and_check_float(1, "floor");
  78. lua_pushnumber (floor(d));
  79. }
  80. static void math_mod (void)
  81. {
  82. int d1 = (int)get_and_check_float(1, "mod");
  83. int d2 = (int)get_and_check_float(2, "mod");
  84. lua_pushnumber (d1%d2);
  85. }
  86. static void math_sqrt (void)
  87. {
  88. double d = get_and_check_float(1, "sqrt");
  89. lua_pushnumber (sqrt(d));
  90. }
  91. static int old_pow;
  92. static void math_pow (void)
  93. {
  94. lua_Object o1 = lua_getparam (1);
  95. lua_Object o2 = lua_getparam (2);
  96. lua_Object op = lua_getparam(3);
  97. if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p')
  98. {
  99. lua_Object old = lua_getlocked(old_pow);
  100. lua_pushobject(o1);
  101. lua_pushobject(o2);
  102. lua_pushobject(op);
  103. if (lua_callfunction(old) != 0)
  104. lua_error(NULL);
  105. }
  106. else
  107. {
  108. double d1 = lua_getnumber(o1);
  109. double d2 = lua_getnumber(o2);
  110. lua_pushnumber (pow(d1,d2));
  111. }
  112. }
  113. static void math_min (void)
  114. {
  115. int i=1;
  116. double dmin = get_and_check_float(i, "min");
  117. while (lua_getparam(++i) != LUA_NOOBJECT)
  118. {
  119. double d = get_and_check_float(i, "min");
  120. if (d < dmin) dmin = d;
  121. }
  122. lua_pushnumber (dmin);
  123. }
  124. static void math_max (void)
  125. {
  126. int i=1;
  127. double dmax = get_and_check_float(i, "max");
  128. while (lua_getparam(++i) != LUA_NOOBJECT)
  129. {
  130. double d = get_and_check_float(i, "max");
  131. if (d > dmax) dmax = d;
  132. }
  133. lua_pushnumber (dmax);
  134. }
  135. static void math_log (void)
  136. {
  137. double d = get_and_check_float(1, "log");
  138. lua_pushnumber (log(d));
  139. }
  140. static void math_log10 (void)
  141. {
  142. double d = get_and_check_float(1, "log10");
  143. lua_pushnumber (log10(d));
  144. }
  145. static void math_exp (void)
  146. {
  147. double d = get_and_check_float(1, "exp");
  148. lua_pushnumber (exp(d));
  149. }
  150. static void math_deg (void)
  151. {
  152. float d = get_and_check_float(1, "deg");
  153. lua_pushnumber (d*180./PI);
  154. }
  155. static void math_rad (void)
  156. {
  157. float d = get_and_check_float(1, "rad");
  158. lua_pushnumber (d/180.*PI);
  159. }
  160. /*
  161. ** Open math library
  162. */
  163. void mathlib_open (void)
  164. {
  165. lua_register ("abs", math_abs);
  166. lua_register ("sin", math_sin);
  167. lua_register ("cos", math_cos);
  168. lua_register ("tan", math_tan);
  169. lua_register ("asin", math_asin);
  170. lua_register ("acos", math_acos);
  171. lua_register ("atan", math_atan);
  172. lua_register ("atan2", math_atan2);
  173. lua_register ("ceil", math_ceil);
  174. lua_register ("floor", math_floor);
  175. lua_register ("mod", math_mod);
  176. lua_register ("sqrt", math_sqrt);
  177. lua_register ("min", math_min);
  178. lua_register ("max", math_max);
  179. lua_register ("log", math_log);
  180. lua_register ("log10", math_log10);
  181. lua_register ("exp", math_exp);
  182. lua_register ("deg", math_deg);
  183. lua_register ("rad", math_rad);
  184. old_pow = lua_lockobject(lua_setfallback("arith", math_pow));
  185. }