no more 'math.ifloor' + new semantics for 'math.floor'/'math.ceil'
This commit is contained in:
30
lmathlib.c
30
lmathlib.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lmathlib.c,v 1.99 2014/05/02 16:36:51 roberto Exp roberto $
|
** $Id: lmathlib.c,v 1.100 2014/05/14 16:59:27 roberto Exp roberto $
|
||||||
** Standard mathematical library
|
** Standard mathematical library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -75,28 +75,32 @@ static int math_atan (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int math_ceil (lua_State *L) {
|
|
||||||
lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int math_floor (lua_State *L) {
|
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)));
|
lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int math_ifloor (lua_State *L) {
|
static int math_ceil (lua_State *L) {
|
||||||
int valid;
|
if (lua_isinteger(L, 1))
|
||||||
lua_Integer n = lua_tointegerx(L, 1, &valid);
|
lua_settop(L, 1); /* integer is its own ceil */
|
||||||
if (valid)
|
|
||||||
lua_pushinteger(L, n);
|
|
||||||
else {
|
else {
|
||||||
luaL_checktype(L, 1, LUA_TNUMBER); /* error if not a number */
|
lua_Integer n;
|
||||||
lua_pushnil(L); /* number with invalid integer value */
|
lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
|
||||||
|
if (lua_numtointeger(d, &n)) /* fits in an integer? */
|
||||||
|
lua_pushinteger(L, n); /* result is integer */
|
||||||
|
else
|
||||||
|
lua_pushnumber(L, d); /* result is float */
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int math_fmod (lua_State *L) {
|
static int math_fmod (lua_State *L) {
|
||||||
if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
|
if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
|
||||||
lua_Integer d = lua_tointeger(L, 2);
|
lua_Integer d = lua_tointeger(L, 2);
|
||||||
@@ -113,6 +117,7 @@ static int math_fmod (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** next function does not use 'modf', avoiding problems with 'double*'
|
** next function does not use 'modf', avoiding problems with 'double*'
|
||||||
** (which is not compatible with 'float*') when lua_Number is not
|
** (which is not compatible with 'float*') when lua_Number is not
|
||||||
@@ -310,7 +315,6 @@ static const luaL_Reg mathlib[] = {
|
|||||||
{"deg", math_deg},
|
{"deg", math_deg},
|
||||||
{"exp", math_exp},
|
{"exp", math_exp},
|
||||||
{"floor", math_floor},
|
{"floor", math_floor},
|
||||||
{"ifloor", math_ifloor},
|
|
||||||
{"fmod", math_fmod},
|
{"fmod", math_fmod},
|
||||||
{"log", math_log},
|
{"log", math_log},
|
||||||
{"max", math_max},
|
{"max", math_max},
|
||||||
|
|||||||
Reference in New Issue
Block a user