lmathlib.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. ** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $
  3. ** Standard mathematical library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lmathlib_c
  7. #define LUA_LIB
  8. #include "lua.h"
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include "lauxlib.h"
  12. #include "lualib.h"
  13. #undef PI
  14. #define PI (3.14159265358979323846)
  15. #define RADIANS_PER_DEGREE (PI/180.0)
  16. static int math_abs (lua_State *L) {
  17. #ifdef LUA_NUMBER_INTEGRAL
  18. lua_Number x = luaL_checknumber(L, 1);
  19. if (x < 0) x = -x; //fails for -2^31
  20. lua_pushnumber(L, x);
  21. #else
  22. lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
  23. #endif
  24. return 1;
  25. }
  26. #ifndef LUA_NUMBER_INTEGRAL
  27. #if 0
  28. static int math_sin (lua_State *L) {
  29. lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
  30. return 1;
  31. }
  32. static int math_sinh (lua_State *L) {
  33. lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
  34. return 1;
  35. }
  36. static int math_cos (lua_State *L) {
  37. lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
  38. return 1;
  39. }
  40. static int math_cosh (lua_State *L) {
  41. lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
  42. return 1;
  43. }
  44. static int math_tan (lua_State *L) {
  45. lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
  46. return 1;
  47. }
  48. static int math_tanh (lua_State *L) {
  49. lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
  50. return 1;
  51. }
  52. static int math_asin (lua_State *L) {
  53. lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
  54. return 1;
  55. }
  56. static int math_acos (lua_State *L) {
  57. lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
  58. return 1;
  59. }
  60. static int math_atan (lua_State *L) {
  61. lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
  62. return 1;
  63. }
  64. static int math_atan2 (lua_State *L) {
  65. lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  66. return 1;
  67. }
  68. #endif
  69. static int math_ceil (lua_State *L) {
  70. lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
  71. return 1;
  72. }
  73. static int math_floor (lua_State *L) {
  74. lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
  75. return 1;
  76. }
  77. #if 0
  78. static int math_fmod (lua_State *L) {
  79. lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  80. return 1;
  81. }
  82. static int math_modf (lua_State *L) {
  83. double ip;
  84. double fp = modf(luaL_checknumber(L, 1), &ip);
  85. lua_pushnumber(L, ip);
  86. lua_pushnumber(L, fp);
  87. return 2;
  88. }
  89. #endif
  90. #else // #ifndef LUA_NUMBER_INTEGRAL
  91. // In integer math, floor() and ceil() give the same value;
  92. // having them in the integer library allows you to write code that
  93. // works in both integer and floating point versions of Lua.
  94. // This identity function is used for them.
  95. static int math_identity (lua_State *L) {
  96. lua_pushnumber(L, luaL_checknumber(L, 1));
  97. return 1;
  98. }
  99. #endif // #ifndef LUA_NUMBER_INTEGRAL
  100. #ifdef LUA_NUMBER_INTEGRAL
  101. // Integer square root for integer version
  102. static lua_Number isqrt(lua_Number x)
  103. {
  104. lua_Number op, res, one;
  105. op = x; res = 0;
  106. /* "one" starts at the highest power of four <= than the argument. */
  107. one = 1 << 30; /* second-to-top bit set */
  108. while (one > op) one >>= 2;
  109. while (one != 0) {
  110. if (op >= res + one) {
  111. op = op - (res + one);
  112. res = res + 2 * one;
  113. }
  114. res >>= 1;
  115. one >>= 2;
  116. }
  117. return(res);
  118. }
  119. #endif
  120. static int math_sqrt (lua_State *L) {
  121. #ifdef LUA_NUMBER_INTEGRAL
  122. lua_Number x = luaL_checknumber(L, 1);
  123. luaL_argcheck(L, 0<=x, 1, "negative");
  124. lua_pushnumber(L, isqrt(x));
  125. #else
  126. lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
  127. #endif
  128. return 1;
  129. }
  130. #ifdef LUA_NUMBER_INTEGRAL
  131. # define pow(a,b) luai_ipow(a,b)
  132. #endif
  133. static int math_pow (lua_State *L) {
  134. lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  135. return 1;
  136. }
  137. #ifdef LUA_NUMBER_INTEGRAL
  138. # undef pow
  139. #endif
  140. #ifndef LUA_NUMBER_INTEGRAL
  141. #if 0
  142. static int math_log (lua_State *L) {
  143. lua_pushnumber(L, log(luaL_checknumber(L, 1)));
  144. return 1;
  145. }
  146. static int math_log10 (lua_State *L) {
  147. lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
  148. return 1;
  149. }
  150. static int math_exp (lua_State *L) {
  151. lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
  152. return 1;
  153. }
  154. static int math_deg (lua_State *L) {
  155. lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
  156. return 1;
  157. }
  158. static int math_rad (lua_State *L) {
  159. lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
  160. return 1;
  161. }
  162. static int math_frexp (lua_State *L) {
  163. int e;
  164. lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
  165. lua_pushinteger(L, e);
  166. return 2;
  167. }
  168. static int math_ldexp (lua_State *L) {
  169. lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
  170. return 1;
  171. }
  172. #endif
  173. #endif // #ifdef LUA_NUMBER_INTEGRAL
  174. static int math_min (lua_State *L) {
  175. int n = lua_gettop(L); /* number of arguments */
  176. lua_Number dmin = luaL_checknumber(L, 1);
  177. int i;
  178. for (i=2; i<=n; i++) {
  179. lua_Number d = luaL_checknumber(L, i);
  180. if (d < dmin)
  181. dmin = d;
  182. }
  183. lua_pushnumber(L, dmin);
  184. return 1;
  185. }
  186. static int math_max (lua_State *L) {
  187. int n = lua_gettop(L); /* number of arguments */
  188. lua_Number dmax = luaL_checknumber(L, 1);
  189. int i;
  190. for (i=2; i<=n; i++) {
  191. lua_Number d = luaL_checknumber(L, i);
  192. if (d > dmax)
  193. dmax = d;
  194. }
  195. lua_pushnumber(L, dmax);
  196. return 1;
  197. }
  198. #ifdef LUA_NUMBER_INTEGRAL
  199. static int math_random (lua_State *L) {
  200. lua_Number r = (lua_Number)(rand()%RAND_MAX);
  201. switch (lua_gettop(L)) { /* check number of arguments */
  202. case 0: { /* no arguments */
  203. lua_pushnumber(L, 0); /* Number between 0 and 1 - always 0 with ints */
  204. break;
  205. }
  206. case 1: { /* only upper limit */
  207. int u = luaL_checkint(L, 1);
  208. luaL_argcheck(L, 1<=u, 1, "interval is empty");
  209. lua_pushnumber(L, (r % u)+1); /* int between 1 and `u' */
  210. break;
  211. }
  212. case 2: { /* lower and upper limits */
  213. int l = luaL_checkint(L, 1);
  214. int u = luaL_checkint(L, 2);
  215. luaL_argcheck(L, l<=u, 2, "interval is empty");
  216. lua_pushnumber(L, (r%(u-l+1))+l); /* int between `l' and `u' */
  217. break;
  218. }
  219. default: return luaL_error(L, "wrong number of arguments");
  220. }
  221. return 1;
  222. }
  223. #else
  224. static int math_random (lua_State *L) {
  225. /* the `%' avoids the (rare) case of r==1, and is needed also because on
  226. some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
  227. lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
  228. switch (lua_gettop(L)) { /* check number of arguments */
  229. case 0: { /* no arguments */
  230. lua_pushnumber(L, r); /* Number between 0 and 1 */
  231. break;
  232. }
  233. case 1: { /* only upper limit */
  234. int u = luaL_checkint(L, 1);
  235. luaL_argcheck(L, 1<=u, 1, "interval is empty");
  236. lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
  237. break;
  238. }
  239. case 2: { /* lower and upper limits */
  240. int l = luaL_checkint(L, 1);
  241. int u = luaL_checkint(L, 2);
  242. luaL_argcheck(L, l<=u, 2, "interval is empty");
  243. lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
  244. break;
  245. }
  246. default: return luaL_error(L, "wrong number of arguments");
  247. }
  248. return 1;
  249. }
  250. #endif
  251. static int math_randomseed (lua_State *L) {
  252. srand(luaL_checkint(L, 1));
  253. return 0;
  254. }
  255. LROT_BEGIN(math, NULL, 0)
  256. #ifdef LUA_NUMBER_INTEGRAL
  257. LROT_FUNCENTRY( abs, math_abs )
  258. LROT_FUNCENTRY( ceil, math_identity )
  259. LROT_FUNCENTRY( floor, math_identity )
  260. LROT_FUNCENTRY( max, math_max )
  261. LROT_FUNCENTRY( min, math_min )
  262. LROT_FUNCENTRY( pow, math_pow )
  263. LROT_FUNCENTRY( random, math_random )
  264. LROT_FUNCENTRY( randomseed, math_randomseed )
  265. LROT_FUNCENTRY( sqrt, math_sqrt )
  266. LROT_NUMENTRY( huge, INT_MAX )
  267. #else
  268. LROT_FUNCENTRY( abs, math_abs )
  269. // LROT_FUNCENTRY( acos, math_acos )
  270. // LROT_FUNCENTRY( asin, math_asin )
  271. // LROT_FUNCENTRY( atan2, math_atan2 )
  272. // LROT_FUNCENTRY( atan, math_atan )
  273. LROT_FUNCENTRY( ceil, math_ceil )
  274. // LROT_FUNCENTRY( cosh, math_cosh )
  275. // LROT_FUNCENTRY( cos, math_cos )
  276. // LROT_FUNCENTRY( deg, math_deg )
  277. // LROT_FUNCENTRY( exp, math_exp )
  278. LROT_FUNCENTRY( floor, math_floor )
  279. // LROT_FUNCENTRY( fmod, math_fmod )
  280. // LROT_FUNCENTRY( mod, math_fmod )
  281. // LROT_FUNCENTRY( frexp, math_frexp )
  282. // LROT_FUNCENTRY( ldexp, math_ldexp )
  283. // LROT_FUNCENTRY( log10, math_log10 )
  284. // LROT_FUNCENTRY( log, math_log )
  285. LROT_FUNCENTRY( max, math_max )
  286. LROT_FUNCENTRY( min, math_min )
  287. // LROT_FUNCENTRY( modf, math_modf )
  288. LROT_FUNCENTRY( pow, math_pow )
  289. // LROT_FUNCENTRY( rad, math_rad )
  290. LROT_FUNCENTRY( random, math_random )
  291. LROT_FUNCENTRY( randomseed, math_randomseed )
  292. // LROT_FUNCENTRY( sinh, math_sinh )
  293. // LROT_FUNCENTRY( sin, math_sin )
  294. LROT_FUNCENTRY( sqrt, math_sqrt )
  295. // LROT_FUNCENTRY( tanh, math_tanh )
  296. // LROT_FUNCENTRY( tan, math_tan )
  297. LROT_NUMENTRY( pi, PI )
  298. LROT_NUMENTRY( huge, HUGE_VAL )
  299. #endif // #ifdef LUA_NUMBER_INTEGRAL
  300. LROT_END(math, NULL, 0)
  301. /*
  302. ** Open math library
  303. */
  304. LUALIB_API int luaopen_math (lua_State *L) {
  305. return 0;
  306. }