diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-26 14:13:52 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-26 14:13:52 -0300 |
commit | 456806f25c239bf36039fc2742547547cc55fbb1 (patch) | |
tree | 20e97df77fc51e13758d1f265cd0fa265bc82bc9 /lmathlib.c | |
parent | c98f195eb930422be2829f78696fb4bf79b93677 (diff) | |
download | lua-456806f25c239bf36039fc2742547547cc55fbb1.tar.gz lua-456806f25c239bf36039fc2742547547cc55fbb1.tar.bz2 lua-456806f25c239bf36039fc2742547547cc55fbb1.zip |
no more 'math.ifloor' + new semantics for 'math.floor'/'math.ceil'
Diffstat (limited to 'lmathlib.c')
-rw-r--r-- | lmathlib.c | 32 |
1 files changed, 18 insertions, 14 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmathlib.c,v 1.99 2014/05/02 16:36:51 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.100 2014/05/14 16:59:27 roberto Exp roberto $ |
3 | ** Standard mathematical library | 3 | ** Standard mathematical library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -75,28 +75,32 @@ static int math_atan (lua_State *L) { | |||
75 | return 1; | 75 | return 1; |
76 | } | 76 | } |
77 | 77 | ||
78 | static int math_ceil (lua_State *L) { | ||
79 | lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1))); | ||
80 | return 1; | ||
81 | } | ||
82 | 78 | ||
83 | static int math_floor (lua_State *L) { | 79 | static int math_floor (lua_State *L) { |
84 | lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); | ||
85 | return 1; | ||
86 | } | ||
87 | |||
88 | static int math_ifloor (lua_State *L) { | ||
89 | int valid; | 80 | int valid; |
90 | lua_Integer n = lua_tointegerx(L, 1, &valid); | 81 | lua_Integer n = lua_tointegerx(L, 1, &valid); |
91 | if (valid) | 82 | if (valid) |
92 | lua_pushinteger(L, n); | 83 | lua_pushinteger(L, n); /* floor computed by Lua */ |
84 | else | ||
85 | lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); | ||
86 | return 1; | ||
87 | } | ||
88 | |||
89 | static int math_ceil (lua_State *L) { | ||
90 | if (lua_isinteger(L, 1)) | ||
91 | lua_settop(L, 1); /* integer is its own ceil */ | ||
93 | else { | 92 | else { |
94 | luaL_checktype(L, 1, LUA_TNUMBER); /* error if not a number */ | 93 | lua_Integer n; |
95 | lua_pushnil(L); /* number with invalid integer value */ | 94 | lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); |
95 | if (lua_numtointeger(d, &n)) /* fits in an integer? */ | ||
96 | lua_pushinteger(L, n); /* result is integer */ | ||
97 | else | ||
98 | lua_pushnumber(L, d); /* result is float */ | ||
96 | } | 99 | } |
97 | return 1; | 100 | return 1; |
98 | } | 101 | } |
99 | 102 | ||
103 | |||
100 | static int math_fmod (lua_State *L) { | 104 | static int math_fmod (lua_State *L) { |
101 | if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) { | 105 | if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) { |
102 | lua_Integer d = lua_tointeger(L, 2); | 106 | lua_Integer d = lua_tointeger(L, 2); |
@@ -113,6 +117,7 @@ static int math_fmod (lua_State *L) { | |||
113 | return 1; | 117 | return 1; |
114 | } | 118 | } |
115 | 119 | ||
120 | |||
116 | /* | 121 | /* |
117 | ** next function does not use 'modf', avoiding problems with 'double*' | 122 | ** next function does not use 'modf', avoiding problems with 'double*' |
118 | ** (which is not compatible with 'float*') when lua_Number is not | 123 | ** (which is not compatible with 'float*') when lua_Number is not |
@@ -310,7 +315,6 @@ static const luaL_Reg mathlib[] = { | |||
310 | {"deg", math_deg}, | 315 | {"deg", math_deg}, |
311 | {"exp", math_exp}, | 316 | {"exp", math_exp}, |
312 | {"floor", math_floor}, | 317 | {"floor", math_floor}, |
313 | {"ifloor", math_ifloor}, | ||
314 | {"fmod", math_fmod}, | 318 | {"fmod", math_fmod}, |
315 | {"log", math_log}, | 319 | {"log", math_log}, |
316 | {"max", math_max}, | 320 | {"max", math_max}, |