lmathlib.c 9.7 KB

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