lua

A copy of the Lua development repository
Log | Files | Refs | README

commit cbe164191c6c3510e225a43c935c6cc3e2da2cd4
parent 66d046833d39b6e33be4d3e10ce5c718d283caef
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Wed,  3 Jul 2013 14:22:54 -0300

new function 'ifloor'

Diffstat:
Mlmathlib.c | 15++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/lmathlib.c b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.88 2013/06/25 14:02:18 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.89 2013/06/25 19:37:00 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -88,6 +88,18 @@ static int math_floor (lua_State *L) { return 1; } +static int math_ifloor (lua_State *L) { + int valid; + lua_Integer n = lua_tointegerx(L, 1, &valid); + if (valid) + lua_pushinteger(L, n); + else { + luaL_checktype(L, 1, LUA_TNUMBER); /* error if not a number */ + lua_pushnil(L); /* number with invalid integer value */ + } + return 1; +} + static int math_fmod (lua_State *L) { lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); @@ -258,6 +270,7 @@ static const luaL_Reg mathlib[] = { {"deg", math_deg}, {"exp", math_exp}, {"floor", math_floor}, + {"ifloor", math_ifloor}, {"fmod", math_fmod}, {"frexp", math_frexp}, {"isfloat", math_isfloat},