diff options
author | Lua Team <team@lua.org> | 2014-07-31 12:00:00 +0000 |
---|---|---|
committer | repogen <> | 2014-07-31 12:00:00 +0000 |
commit | d7648e85b78d53a2248de909868192598ad0eb69 (patch) | |
tree | c67708a14fd29f8ff7e4981aadb041c5ab577e08 /src/lmathlib.c | |
parent | 3907bda05b0e73eba86487ad703e832ca14b64ce (diff) | |
download | lua-github-5.3.0-alpha.tar.gz |
Lua 5.3.0-alpha5.3.0-alpha
Diffstat (limited to 'src/lmathlib.c')
-rw-r--r-- | src/lmathlib.c | 61 |
1 files changed, 38 insertions, 23 deletions
diff --git a/src/lmathlib.c b/src/lmathlib.c index 145b35ab..312df517 100644 --- a/src/lmathlib.c +++ b/src/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.103 2014/06/18 12:35:53 roberto Exp $ +** $Id: lmathlib.c,v 1.108 2014/07/28 17:35:47 roberto Exp $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -76,39 +76,39 @@ static int math_atan (lua_State *L) { } -static int math_ifloor (lua_State *L) { +static int math_toint (lua_State *L) { int valid; lua_Integer n = lua_tointegerx(L, 1, &valid); if (valid) - lua_pushinteger(L, n); /* floor computed by Lua */ + lua_pushinteger(L, n); else { - luaL_checktype(L, 1, LUA_TNUMBER); /* argument must be a number */ - lua_pushnil(L); /* number is not convertible to integer */ + luaL_checkany(L, 1); + lua_pushnil(L); /* value is not convertible to integer */ } return 1; } -static int math_floor (lua_State *L) { - int valid; - lua_Integer n = lua_tointegerx(L, 1, &valid); - if (valid) - lua_pushinteger(L, n); /* floor computed by Lua */ - else - lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); - return 1; -} - - static void pushnumint (lua_State *L, lua_Number d) { lua_Integer n; - if (lua_numtointeger(d, &n)) /* fits in an integer? */ + if (lua_numtointeger(d, &n)) /* does 'd' fit in an integer? */ lua_pushinteger(L, n); /* result is integer */ else lua_pushnumber(L, d); /* result is float */ } +static int math_floor (lua_State *L) { + if (lua_isinteger(L, 1)) + lua_settop(L, 1); /* integer is its own floor */ + else { + lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1)); + pushnumint(L, d); + } + return 1; +} + + static int math_ceil (lua_State *L) { if (lua_isinteger(L, 1)) lua_settop(L, 1); /* integer is its own ceil */ @@ -145,14 +145,14 @@ static int math_fmod (lua_State *L) { static int math_modf (lua_State *L) { if (lua_isinteger(L ,1)) { lua_settop(L, 1); /* number is its own integer part */ - lua_pushnumber(L, 0); /* no fractionary part */ + lua_pushnumber(L, 0); /* no fractional part */ } else { lua_Number n = luaL_checknumber(L, 1); /* integer part (rounds toward zero) */ lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n); pushnumint(L, ip); - /* fractionary part (test needed for inf/-inf) */ + /* fractional part (test needed for inf/-inf) */ lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip)); } return 2; @@ -164,6 +164,14 @@ static int math_sqrt (lua_State *L) { return 1; } + +static int math_ult (lua_State *L) { + lua_Integer a = luaL_checkinteger(L, 1); + lua_Integer b = luaL_checkinteger(L, 2); + lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b); + return 1; +} + static int math_log (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); lua_Number res; @@ -257,22 +265,23 @@ static int math_random (lua_State *L) { static int math_randomseed (lua_State *L) { - l_srand((unsigned int)luaL_checkunsigned(L, 1)); + l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1)); (void)rand(); /* discard first value to avoid undesirable correlations */ return 0; } static int math_type (lua_State *L) { - luaL_checkany(L, 1); if (lua_type(L, 1) == LUA_TNUMBER) { if (lua_isinteger(L, 1)) lua_pushliteral(L, "integer"); else lua_pushliteral(L, "float"); } - else + else { + luaL_checkany(L, 1); lua_pushnil(L); + } return 1; } @@ -339,9 +348,10 @@ static const luaL_Reg mathlib[] = { {"cos", math_cos}, {"deg", math_deg}, {"exp", math_exp}, - {"ifloor", math_ifloor}, + {"tointeger", math_toint}, {"floor", math_floor}, {"fmod", math_fmod}, + {"ult", math_ult}, {"log", math_log}, {"max", math_max}, {"min", math_min}, @@ -363,6 +373,11 @@ static const luaL_Reg mathlib[] = { {"ldexp", math_ldexp}, {"log10", math_log10}, #endif + /* placeholders */ + {"pi", NULL}, + {"huge", NULL}, + {"maxinteger", NULL}, + {"mininteger", NULL}, {NULL, NULL} }; |