diff src/lua/lmathlib.c @ 11:27763b933818

raise lua sources up one level
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 11:07:39 -0600
parents src/lua/src/lmathlib.c@f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/lmathlib.c	Sat Mar 03 11:07:39 2012 -0600
     1.3 @@ -0,0 +1,263 @@
     1.4 +/*
     1.5 +** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $
     1.6 +** Standard mathematical library
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +#include <stdlib.h>
    1.12 +#include <math.h>
    1.13 +
    1.14 +#define lmathlib_c
    1.15 +#define LUA_LIB
    1.16 +
    1.17 +#include "lua.h"
    1.18 +
    1.19 +#include "lauxlib.h"
    1.20 +#include "lualib.h"
    1.21 +
    1.22 +
    1.23 +#undef PI
    1.24 +#define PI (3.14159265358979323846)
    1.25 +#define RADIANS_PER_DEGREE (PI/180.0)
    1.26 +
    1.27 +
    1.28 +
    1.29 +static int math_abs (lua_State *L) {
    1.30 +  lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
    1.31 +  return 1;
    1.32 +}
    1.33 +
    1.34 +static int math_sin (lua_State *L) {
    1.35 +  lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
    1.36 +  return 1;
    1.37 +}
    1.38 +
    1.39 +static int math_sinh (lua_State *L) {
    1.40 +  lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
    1.41 +  return 1;
    1.42 +}
    1.43 +
    1.44 +static int math_cos (lua_State *L) {
    1.45 +  lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
    1.46 +  return 1;
    1.47 +}
    1.48 +
    1.49 +static int math_cosh (lua_State *L) {
    1.50 +  lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
    1.51 +  return 1;
    1.52 +}
    1.53 +
    1.54 +static int math_tan (lua_State *L) {
    1.55 +  lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
    1.56 +  return 1;
    1.57 +}
    1.58 +
    1.59 +static int math_tanh (lua_State *L) {
    1.60 +  lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
    1.61 +  return 1;
    1.62 +}
    1.63 +
    1.64 +static int math_asin (lua_State *L) {
    1.65 +  lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
    1.66 +  return 1;
    1.67 +}
    1.68 +
    1.69 +static int math_acos (lua_State *L) {
    1.70 +  lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
    1.71 +  return 1;
    1.72 +}
    1.73 +
    1.74 +static int math_atan (lua_State *L) {
    1.75 +  lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
    1.76 +  return 1;
    1.77 +}
    1.78 +
    1.79 +static int math_atan2 (lua_State *L) {
    1.80 +  lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
    1.81 +  return 1;
    1.82 +}
    1.83 +
    1.84 +static int math_ceil (lua_State *L) {
    1.85 +  lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
    1.86 +  return 1;
    1.87 +}
    1.88 +
    1.89 +static int math_floor (lua_State *L) {
    1.90 +  lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
    1.91 +  return 1;
    1.92 +}
    1.93 +
    1.94 +static int math_fmod (lua_State *L) {
    1.95 +  lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
    1.96 +  return 1;
    1.97 +}
    1.98 +
    1.99 +static int math_modf (lua_State *L) {
   1.100 +  double ip;
   1.101 +  double fp = modf(luaL_checknumber(L, 1), &ip);
   1.102 +  lua_pushnumber(L, ip);
   1.103 +  lua_pushnumber(L, fp);
   1.104 +  return 2;
   1.105 +}
   1.106 +
   1.107 +static int math_sqrt (lua_State *L) {
   1.108 +  lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
   1.109 +  return 1;
   1.110 +}
   1.111 +
   1.112 +static int math_pow (lua_State *L) {
   1.113 +  lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
   1.114 +  return 1;
   1.115 +}
   1.116 +
   1.117 +static int math_log (lua_State *L) {
   1.118 +  lua_pushnumber(L, log(luaL_checknumber(L, 1)));
   1.119 +  return 1;
   1.120 +}
   1.121 +
   1.122 +static int math_log10 (lua_State *L) {
   1.123 +  lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
   1.124 +  return 1;
   1.125 +}
   1.126 +
   1.127 +static int math_exp (lua_State *L) {
   1.128 +  lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
   1.129 +  return 1;
   1.130 +}
   1.131 +
   1.132 +static int math_deg (lua_State *L) {
   1.133 +  lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
   1.134 +  return 1;
   1.135 +}
   1.136 +
   1.137 +static int math_rad (lua_State *L) {
   1.138 +  lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
   1.139 +  return 1;
   1.140 +}
   1.141 +
   1.142 +static int math_frexp (lua_State *L) {
   1.143 +  int e;
   1.144 +  lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
   1.145 +  lua_pushinteger(L, e);
   1.146 +  return 2;
   1.147 +}
   1.148 +
   1.149 +static int math_ldexp (lua_State *L) {
   1.150 +  lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
   1.151 +  return 1;
   1.152 +}
   1.153 +
   1.154 +
   1.155 +
   1.156 +static int math_min (lua_State *L) {
   1.157 +  int n = lua_gettop(L);  /* number of arguments */
   1.158 +  lua_Number dmin = luaL_checknumber(L, 1);
   1.159 +  int i;
   1.160 +  for (i=2; i<=n; i++) {
   1.161 +    lua_Number d = luaL_checknumber(L, i);
   1.162 +    if (d < dmin)
   1.163 +      dmin = d;
   1.164 +  }
   1.165 +  lua_pushnumber(L, dmin);
   1.166 +  return 1;
   1.167 +}
   1.168 +
   1.169 +
   1.170 +static int math_max (lua_State *L) {
   1.171 +  int n = lua_gettop(L);  /* number of arguments */
   1.172 +  lua_Number dmax = luaL_checknumber(L, 1);
   1.173 +  int i;
   1.174 +  for (i=2; i<=n; i++) {
   1.175 +    lua_Number d = luaL_checknumber(L, i);
   1.176 +    if (d > dmax)
   1.177 +      dmax = d;
   1.178 +  }
   1.179 +  lua_pushnumber(L, dmax);
   1.180 +  return 1;
   1.181 +}
   1.182 +
   1.183 +
   1.184 +static int math_random (lua_State *L) {
   1.185 +  /* the `%' avoids the (rare) case of r==1, and is needed also because on
   1.186 +     some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
   1.187 +  lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
   1.188 +  switch (lua_gettop(L)) {  /* check number of arguments */
   1.189 +    case 0: {  /* no arguments */
   1.190 +      lua_pushnumber(L, r);  /* Number between 0 and 1 */
   1.191 +      break;
   1.192 +    }
   1.193 +    case 1: {  /* only upper limit */
   1.194 +      int u = luaL_checkint(L, 1);
   1.195 +      luaL_argcheck(L, 1<=u, 1, "interval is empty");
   1.196 +      lua_pushnumber(L, floor(r*u)+1);  /* int between 1 and `u' */
   1.197 +      break;
   1.198 +    }
   1.199 +    case 2: {  /* lower and upper limits */
   1.200 +      int l = luaL_checkint(L, 1);
   1.201 +      int u = luaL_checkint(L, 2);
   1.202 +      luaL_argcheck(L, l<=u, 2, "interval is empty");
   1.203 +      lua_pushnumber(L, floor(r*(u-l+1))+l);  /* int between `l' and `u' */
   1.204 +      break;
   1.205 +    }
   1.206 +    default: return luaL_error(L, "wrong number of arguments");
   1.207 +  }
   1.208 +  return 1;
   1.209 +}
   1.210 +
   1.211 +
   1.212 +static int math_randomseed (lua_State *L) {
   1.213 +  srand(luaL_checkint(L, 1));
   1.214 +  return 0;
   1.215 +}
   1.216 +
   1.217 +
   1.218 +static const luaL_Reg mathlib[] = {
   1.219 +  {"abs",   math_abs},
   1.220 +  {"acos",  math_acos},
   1.221 +  {"asin",  math_asin},
   1.222 +  {"atan2", math_atan2},
   1.223 +  {"atan",  math_atan},
   1.224 +  {"ceil",  math_ceil},
   1.225 +  {"cosh",   math_cosh},
   1.226 +  {"cos",   math_cos},
   1.227 +  {"deg",   math_deg},
   1.228 +  {"exp",   math_exp},
   1.229 +  {"floor", math_floor},
   1.230 +  {"fmod",   math_fmod},
   1.231 +  {"frexp", math_frexp},
   1.232 +  {"ldexp", math_ldexp},
   1.233 +  {"log10", math_log10},
   1.234 +  {"log",   math_log},
   1.235 +  {"max",   math_max},
   1.236 +  {"min",   math_min},
   1.237 +  {"modf",   math_modf},
   1.238 +  {"pow",   math_pow},
   1.239 +  {"rad",   math_rad},
   1.240 +  {"random",     math_random},
   1.241 +  {"randomseed", math_randomseed},
   1.242 +  {"sinh",   math_sinh},
   1.243 +  {"sin",   math_sin},
   1.244 +  {"sqrt",  math_sqrt},
   1.245 +  {"tanh",   math_tanh},
   1.246 +  {"tan",   math_tan},
   1.247 +  {NULL, NULL}
   1.248 +};
   1.249 +
   1.250 +
   1.251 +/*
   1.252 +** Open math library
   1.253 +*/
   1.254 +LUALIB_API int luaopen_math (lua_State *L) {
   1.255 +  luaL_register(L, LUA_MATHLIBNAME, mathlib);
   1.256 +  lua_pushnumber(L, PI);
   1.257 +  lua_setfield(L, -2, "pi");
   1.258 +  lua_pushnumber(L, HUGE_VAL);
   1.259 +  lua_setfield(L, -2, "huge");
   1.260 +#if defined(LUA_COMPAT_MOD)
   1.261 +  lua_getfield(L, -1, "fmod");
   1.262 +  lua_setfield(L, -2, "mod");
   1.263 +#endif
   1.264 +  return 1;
   1.265 +}
   1.266 +