summaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-05-26 14:13:52 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-05-26 14:13:52 -0300
commit456806f25c239bf36039fc2742547547cc55fbb1 (patch)
tree20e97df77fc51e13758d1f265cd0fa265bc82bc9 /lmathlib.c
parentc98f195eb930422be2829f78696fb4bf79b93677 (diff)
downloadlua-github-456806f25c239bf36039fc2742547547cc55fbb1.tar.gz
no more 'math.ifloor' + new semantics for 'math.floor'/'math.ceil'
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c32
1 files changed, 18 insertions, 14 deletions
diff --git a/lmathlib.c b/lmathlib.c
index 0762d5dd..3df4b799 100644
--- a/lmathlib.c
+++ b/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
** See Copyright Notice in lua.h
*/
@@ -75,28 +75,32 @@ static int math_atan (lua_State *L) {
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) {
- lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
- 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);
+ lua_pushinteger(L, n); /* floor computed by Lua */
+ else
+ lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
+ return 1;
+}
+
+static int math_ceil (lua_State *L) {
+ if (lua_isinteger(L, 1))
+ lua_settop(L, 1); /* integer is its own ceil */
else {
- luaL_checktype(L, 1, LUA_TNUMBER); /* error if not a number */
- lua_pushnil(L); /* number with invalid integer value */
+ lua_Integer n;
+ 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;
}
+
static int math_fmod (lua_State *L) {
if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
lua_Integer d = lua_tointeger(L, 2);
@@ -113,6 +117,7 @@ static int math_fmod (lua_State *L) {
return 1;
}
+
/*
** next function does not use 'modf', avoiding problems with 'double*'
** (which is not compatible with 'float*') when lua_Number is not
@@ -310,7 +315,6 @@ static const luaL_Reg mathlib[] = {
{"deg", math_deg},
{"exp", math_exp},
{"floor", math_floor},
- {"ifloor", math_ifloor},
{"fmod", math_fmod},
{"log", math_log},
{"max", math_max},