2
0

lmathlib.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. ** $Id: lmathlib.c,v 1.60 2004/04/30 20:13:38 roberto Exp roberto $
  3. ** Standard mathematical library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #define lmathlib_c
  9. #define LUA_LIB
  10. #include "lua.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. lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
  18. return 1;
  19. }
  20. static int math_sin (lua_State *L) {
  21. lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
  22. return 1;
  23. }
  24. static int math_cos (lua_State *L) {
  25. lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
  26. return 1;
  27. }
  28. static int math_tan (lua_State *L) {
  29. lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
  30. return 1;
  31. }
  32. static int math_asin (lua_State *L) {
  33. lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
  34. return 1;
  35. }
  36. static int math_acos (lua_State *L) {
  37. lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
  38. return 1;
  39. }
  40. static int math_atan (lua_State *L) {
  41. lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
  42. return 1;
  43. }
  44. static int math_atan2 (lua_State *L) {
  45. lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  46. return 1;
  47. }
  48. static int math_ceil (lua_State *L) {
  49. lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
  50. return 1;
  51. }
  52. static int math_floor (lua_State *L) {
  53. lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
  54. return 1;
  55. }
  56. static int math_mod (lua_State *L) {
  57. lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  58. return 1;
  59. }
  60. static int math_sqrt (lua_State *L) {
  61. lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
  62. return 1;
  63. }
  64. static int math_pow (lua_State *L) {
  65. lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  66. return 1;
  67. }
  68. static int math_log (lua_State *L) {
  69. lua_pushnumber(L, log(luaL_checknumber(L, 1)));
  70. return 1;
  71. }
  72. static int math_log10 (lua_State *L) {
  73. lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
  74. return 1;
  75. }
  76. static int math_exp (lua_State *L) {
  77. lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
  78. return 1;
  79. }
  80. static int math_deg (lua_State *L) {
  81. lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
  82. return 1;
  83. }
  84. static int math_rad (lua_State *L) {
  85. lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
  86. return 1;
  87. }
  88. static int math_frexp (lua_State *L) {
  89. int e;
  90. lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
  91. lua_pushinteger(L, e);
  92. return 2;
  93. }
  94. static int math_ldexp (lua_State *L) {
  95. lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
  96. return 1;
  97. }
  98. static int math_min (lua_State *L) {
  99. int n = lua_gettop(L); /* number of arguments */
  100. lua_Number dmin = luaL_checknumber(L, 1);
  101. int i;
  102. for (i=2; i<=n; i++) {
  103. lua_Number d = luaL_checknumber(L, i);
  104. if (d < dmin)
  105. dmin = d;
  106. }
  107. lua_pushnumber(L, dmin);
  108. return 1;
  109. }
  110. static int math_max (lua_State *L) {
  111. int n = lua_gettop(L); /* number of arguments */
  112. lua_Number dmax = luaL_checknumber(L, 1);
  113. int i;
  114. for (i=2; i<=n; i++) {
  115. lua_Number d = luaL_checknumber(L, i);
  116. if (d > dmax)
  117. dmax = d;
  118. }
  119. lua_pushnumber(L, dmax);
  120. return 1;
  121. }
  122. static int math_random (lua_State *L) {
  123. /* the `%' avoids the (rare) case of r==1, and is needed also because on
  124. some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
  125. lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
  126. switch (lua_gettop(L)) { /* check number of arguments */
  127. case 0: { /* no arguments */
  128. lua_pushnumber(L, r); /* Number between 0 and 1 */
  129. break;
  130. }
  131. case 1: { /* only upper limit */
  132. int u = luaL_checkint(L, 1);
  133. luaL_argcheck(L, 1<=u, 1, "interval is empty");
  134. lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
  135. break;
  136. }
  137. case 2: { /* lower and upper limits */
  138. int l = luaL_checkint(L, 1);
  139. int u = luaL_checkint(L, 2);
  140. luaL_argcheck(L, l<=u, 2, "interval is empty");
  141. lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
  142. break;
  143. }
  144. default: return luaL_error(L, "wrong number of arguments");
  145. }
  146. return 1;
  147. }
  148. static int math_randomseed (lua_State *L) {
  149. srand(luaL_checkint(L, 1));
  150. return 0;
  151. }
  152. static const luaL_reg mathlib[] = {
  153. {"abs", math_abs},
  154. {"sin", math_sin},
  155. {"cos", math_cos},
  156. {"tan", math_tan},
  157. {"asin", math_asin},
  158. {"acos", math_acos},
  159. {"atan", math_atan},
  160. {"atan2", math_atan2},
  161. {"ceil", math_ceil},
  162. {"floor", math_floor},
  163. {"mod", math_mod},
  164. {"frexp", math_frexp},
  165. {"ldexp", math_ldexp},
  166. {"sqrt", math_sqrt},
  167. {"min", math_min},
  168. {"max", math_max},
  169. {"log", math_log},
  170. {"log10", math_log10},
  171. {"exp", math_exp},
  172. {"deg", math_deg},
  173. {"pow", math_pow},
  174. {"rad", math_rad},
  175. {"random", math_random},
  176. {"randomseed", math_randomseed},
  177. {NULL, NULL}
  178. };
  179. /*
  180. ** Open math library
  181. */
  182. LUALIB_API int luaopen_math (lua_State *L) {
  183. luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
  184. lua_pushnumber(L, PI);
  185. lua_setfield(L, -2, "pi");
  186. lua_pushcfunction(L, math_pow);
  187. lua_setglobal(L, "__pow");
  188. return 1;
  189. }