root/lib/lua/lmathlib.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. math_abs
  2. math_sin
  3. math_sinh
  4. math_cos
  5. math_cosh
  6. math_tan
  7. math_tanh
  8. math_asin
  9. math_acos
  10. math_atan
  11. math_atan2
  12. math_ceil
  13. math_floor
  14. math_fmod
  15. math_modf
  16. math_sqrt
  17. math_pow
  18. math_log
  19. math_log10
  20. math_exp
  21. math_deg
  22. math_rad
  23. math_frexp
  24. math_ldexp
  25. math_min
  26. math_max
  27. math_random
  28. math_randomseed
  29. luaopen_math

   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 
   7 
   8 #include <stdlib.h>
   9 #include <math.h>
  10 
  11 #define lmathlib_c
  12 #define LUA_LIB
  13 
  14 #include "lua.h"
  15 
  16 #include "lauxlib.h"
  17 #include "lualib.h"
  18 
  19 
  20 #undef PI
  21 #define PI (3.14159265358979323846)
  22 #define RADIANS_PER_DEGREE (PI/180.0)
  23 
  24 
  25 
  26 static int math_abs (lua_State *L) {
  27 #if 0
  28   lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
  29 #else
  30   lua_pushnumber(L, abs(luaL_checknumber(L, 1)));
  31 #endif
  32   return 1;
  33 }
  34 
  35 #if 0
  36 static int math_sin (lua_State *L) {
  37   lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
  38   return 1;
  39 }
  40 
  41 static int math_sinh (lua_State *L) {
  42   lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
  43   return 1;
  44 }
  45 
  46 static int math_cos (lua_State *L) {
  47   lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
  48   return 1;
  49 }
  50 
  51 static int math_cosh (lua_State *L) {
  52   lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
  53   return 1;
  54 }
  55 
  56 static int math_tan (lua_State *L) {
  57   lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
  58   return 1;
  59 }
  60 
  61 static int math_tanh (lua_State *L) {
  62   lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
  63   return 1;
  64 }
  65 
  66 static int math_asin (lua_State *L) {
  67   lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
  68   return 1;
  69 }
  70 
  71 static int math_acos (lua_State *L) {
  72   lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
  73   return 1;
  74 }
  75 
  76 static int math_atan (lua_State *L) {
  77   lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
  78   return 1;
  79 }
  80 
  81 static int math_atan2 (lua_State *L) {
  82   lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  83   return 1;
  84 }
  85 
  86 static int math_ceil (lua_State *L) {
  87   lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
  88   return 1;
  89 }
  90 
  91 static int math_floor (lua_State *L) {
  92   lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
  93   return 1;
  94 }
  95 
  96 static int math_fmod (lua_State *L) {
  97   lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  98   return 1;
  99 }
 100 
 101 static int math_modf (lua_State *L) {
 102   double ip;
 103   double fp = modf(luaL_checknumber(L, 1), &ip);
 104   lua_pushnumber(L, ip);
 105   lua_pushnumber(L, fp);
 106   return 2;
 107 }
 108 
 109 static int math_sqrt (lua_State *L) {
 110   lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
 111   return 1;
 112 }
 113 #endif
 114 static int math_pow (lua_State *L) {
 115 #if 0
 116   lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
 117 #else
 118   lua_pushnumber(L, luai_ipow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
 119 #endif
 120   return 1;
 121 }
 122 #if 0
 123 static int math_log (lua_State *L) {
 124   lua_pushnumber(L, log(luaL_checknumber(L, 1)));
 125   return 1;
 126 }
 127 
 128 static int math_log10 (lua_State *L) {
 129   lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
 130   return 1;
 131 }
 132 
 133 static int math_exp (lua_State *L) {
 134   lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
 135   return 1;
 136 }
 137 
 138 static int math_deg (lua_State *L) {
 139   lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
 140   return 1;
 141 }
 142 
 143 static int math_rad (lua_State *L) {
 144   lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
 145   return 1;
 146 }
 147 
 148 static int math_frexp (lua_State *L) {
 149   int e;
 150   lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
 151   lua_pushinteger(L, e);
 152   return 2;
 153 }
 154 
 155 static int math_ldexp (lua_State *L) {
 156   lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
 157   return 1;
 158 }
 159 #endif
 160 
 161 
 162 static int math_min (lua_State *L) {
 163   int n = lua_gettop(L);  /* number of arguments */
 164   lua_Number dmin = luaL_checknumber(L, 1);
 165   int i;
 166   for (i=2; i<=n; i++) {
 167     lua_Number d = luaL_checknumber(L, i);
 168     if (d < dmin)
 169       dmin = d;
 170   }
 171   lua_pushnumber(L, dmin);
 172   return 1;
 173 }
 174 
 175 
 176 static int math_max (lua_State *L) {
 177   int n = lua_gettop(L);  /* number of arguments */
 178   lua_Number dmax = 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 > dmax)
 183       dmax = d;
 184   }
 185   lua_pushnumber(L, dmax);
 186   return 1;
 187 }
 188 
 189 
 190 static int math_random (lua_State *L) {
 191   /* the `%' avoids the (rare) case of r==1, and is needed also because on
 192      some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
 193 #if 0
 194   lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
 195 #endif
 196   switch (lua_gettop(L)) {  /* check number of arguments */
 197 #if 0
 198     case 0: {  /* no arguments */
 199       lua_pushnumber(L, r);  /* Number between 0 and 1 */
 200       break;
 201     }
 202 #endif
 203     case 1: {  /* only upper limit */
 204       int u = luaL_checkint(L, 1);
 205       luaL_argcheck(L, 1<=u, 1, "interval is empty");
 206 #if 0
 207       lua_pushnumber(L, floor(r*u)+1);  /* int between 1 and `u' */
 208 #else
 209       lua_pushnumber(L, rand()%u+1);
 210 #endif
 211       break;
 212     }
 213     case 2: {  /* lower and upper limits */
 214       int l = luaL_checkint(L, 1);
 215       int u = luaL_checkint(L, 2);
 216       luaL_argcheck(L, l<=u, 2, "interval is empty");
 217 #if 0
 218       lua_pushnumber(L, floor(r*(u-l+1))+l);  /* int between `l' and `u' */
 219 #else
 220       lua_pushnumber(L, rand()%(u-l+1)+l);
 221 #endif
 222       break;
 223     }
 224     default: return luaL_error(L, "wrong number of arguments");
 225   }
 226   return 1;
 227 }
 228 
 229 
 230 static int math_randomseed (lua_State *L) {
 231   srand(luaL_checkint(L, 1));
 232   return 0;
 233 }
 234 
 235 
 236 static const luaL_Reg mathlib[] = {
 237   {"abs",   math_abs},
 238 //  {"acos",  math_acos},
 239 //  {"asin",  math_asin},
 240 //  {"atan2", math_atan2},
 241 //  {"atan",  math_atan},
 242 //  {"ceil",  math_ceil},
 243 //  {"cosh",   math_cosh},
 244 //  {"cos",   math_cos},
 245 //  {"deg",   math_deg},
 246 //  {"exp",   math_exp},
 247 //  {"floor", math_floor},
 248 //  {"fmod",   math_fmod},
 249 //  {"frexp", math_frexp},
 250 //  {"ldexp", math_ldexp},
 251 //  {"log10", math_log10},
 252 //  {"log",   math_log},
 253   {"max",   math_max},
 254   {"min",   math_min},
 255 //  {"modf",   math_modf},
 256   {"pow",   math_pow},
 257 //  {"rad",   math_rad},
 258   {"random",     math_random},
 259   {"randomseed", math_randomseed},
 260 //  {"sinh",   math_sinh},
 261 //  {"sin",   math_sin},
 262 //  {"sqrt",  math_sqrt},
 263 //  {"tanh",   math_tanh},
 264 //  {"tan",   math_tan},
 265   {NULL, NULL}
 266 };
 267 
 268 
 269 /*
 270 ** Open math library
 271 */
 272 LUALIB_API int luaopen_math (lua_State *L) {
 273   luaL_register(L, LUA_MATHLIBNAME, mathlib);
 274 #if 0
 275   lua_pushnumber(L, PI);
 276   lua_setfield(L, -2, "pi");
 277   lua_pushnumber(L, HUGE_VAL);
 278   lua_setfield(L, -2, "huge");
 279 #endif
 280 #if defined(LUA_COMPAT_MOD)
 281   lua_getfield(L, -1, "fmod");
 282   lua_setfield(L, -2, "mod");
 283 #endif
 284   return 1;
 285 }
 286 

/* [<][>][^][v][top][bottom][index][help] */