lmathlib.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. ** $Id: $
  3. ** Lua standard mathematical library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include "lauxlib.h"
  9. #include "lua.h"
  10. #include "lualib.h"
  11. #ifndef PI
  12. #define PI 3.14159265358979323846
  13. #endif
  14. #define TODEGREE(a) ((a)*180.0/PI)
  15. #define TORAD(a) ((a)*PI/180.0)
  16. static void math_abs (void)
  17. {
  18. double d = luaL_check_number(1);
  19. if (d < 0) d = -d;
  20. lua_pushnumber(d);
  21. }
  22. static void math_sin (void)
  23. {
  24. lua_pushnumber(sin(TORAD(luaL_check_number(1))));
  25. }
  26. static void math_cos (void)
  27. {
  28. lua_pushnumber(cos(TORAD(luaL_check_number(1))));
  29. }
  30. static void math_tan (void)
  31. {
  32. lua_pushnumber(tan(TORAD(luaL_check_number(1))));
  33. }
  34. static void math_asin (void)
  35. {
  36. lua_pushnumber(asin(TODEGREE(luaL_check_number(1))));
  37. }
  38. static void math_acos (void)
  39. {
  40. lua_pushnumber(acos(TODEGREE(luaL_check_number(1))));
  41. }
  42. static void math_atan (void)
  43. {
  44. lua_pushnumber(atan(TODEGREE(luaL_check_number(1))));
  45. }
  46. static void math_atan2 (void)
  47. {
  48. lua_pushnumber(TODEGREE(atan2(luaL_check_number(1), luaL_check_number(2))));
  49. }
  50. static void math_ceil (void)
  51. {
  52. lua_pushnumber(ceil(luaL_check_number(1)));
  53. }
  54. static void math_floor (void)
  55. {
  56. lua_pushnumber(floor(luaL_check_number(1)));
  57. }
  58. static void math_mod (void)
  59. {
  60. lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
  61. }
  62. static void math_sqrt (void)
  63. {
  64. lua_pushnumber(sqrt(luaL_check_number(1)));
  65. }
  66. static void math_pow (void)
  67. {
  68. lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
  69. }
  70. static void math_log (void)
  71. {
  72. lua_pushnumber(log(luaL_check_number(1)));
  73. }
  74. static void math_log10 (void)
  75. {
  76. lua_pushnumber(log10(luaL_check_number(1)));
  77. }
  78. static void math_exp (void)
  79. {
  80. lua_pushnumber(exp(luaL_check_number(1)));
  81. }
  82. static void math_deg (void)
  83. {
  84. lua_pushnumber(luaL_check_number(1)*180./PI);
  85. }
  86. static void math_rad (void)
  87. {
  88. lua_pushnumber(luaL_check_number(1)/180.*PI);
  89. }
  90. static void math_min (void)
  91. {
  92. int i = 1;
  93. double dmin = luaL_check_number(i);
  94. while (lua_getparam(++i) != LUA_NOOBJECT) {
  95. double d = luaL_check_number(i);
  96. if (d < dmin)
  97. dmin = d;
  98. }
  99. lua_pushnumber(dmin);
  100. }
  101. static void math_max (void)
  102. {
  103. int i = 1;
  104. double dmax = luaL_check_number(i);
  105. while (lua_getparam(++i) != LUA_NOOBJECT) {
  106. double d = luaL_check_number(i);
  107. if (d > dmax)
  108. dmax = d;
  109. }
  110. lua_pushnumber(dmax);
  111. }
  112. static void math_random (void)
  113. {
  114. double l = luaL_opt_number(1, 0);
  115. double r = (double)rand() / (double)RAND_MAX;
  116. if (l == 0)
  117. lua_pushnumber(r);
  118. else
  119. lua_pushnumber((int)(r*l)+1);
  120. }
  121. static void math_randomseed (void)
  122. {
  123. srand(luaL_check_number(1));
  124. }
  125. static struct luaL_reg mathlib[] = {
  126. {"abs", math_abs},
  127. {"sin", math_sin},
  128. {"cos", math_cos},
  129. {"tan", math_tan},
  130. {"asin", math_asin},
  131. {"acos", math_acos},
  132. {"atan", math_atan},
  133. {"atan2", math_atan2},
  134. {"ceil", math_ceil},
  135. {"floor", math_floor},
  136. {"mod", math_mod},
  137. {"sqrt", math_sqrt},
  138. {"min", math_min},
  139. {"max", math_max},
  140. {"log", math_log},
  141. {"log10", math_log10},
  142. {"exp", math_exp},
  143. {"deg", math_deg},
  144. {"rad", math_rad},
  145. {"random", math_random},
  146. {"randomseed", math_randomseed}
  147. };
  148. /*
  149. ** Open math library
  150. */
  151. void lua_mathlibopen (void)
  152. {
  153. luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
  154. lua_pushcfunction(math_pow);
  155. lua_pushnumber(0); /* to get its tag */
  156. lua_settagmethod(lua_tag(lua_pop()), "pow");
  157. }