aboutsummaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-06-02 20:09:28 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-06-02 20:09:28 -0300
commit355037528c8c0896c5e643bb724f8d686c6322ad (patch)
tree647e20b546c203464a8c112bd4d8d1b018392f0b /lmathlib.c
parent9e68c047ae809608f53245e0e0f0b76f30b27c0f (diff)
downloadlua-355037528c8c0896c5e643bb724f8d686c6322ad.tar.gz
lua-355037528c8c0896c5e643bb724f8d686c6322ad.tar.bz2
lua-355037528c8c0896c5e643bb724f8d686c6322ad.zip
'math.mof' works with integers, too
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c36
1 files changed, 24 insertions, 12 deletions
diff --git a/lmathlib.c b/lmathlib.c
index 3df4b799..6ba1b3f5 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.100 2014/05/14 16:59:27 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.101 2014/05/26 17:13:52 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*/
@@ -86,16 +86,22 @@ static int math_floor (lua_State *L) {
86 return 1; 86 return 1;
87} 87}
88 88
89
90static void pushnumint (lua_State *L, lua_Number d) {
91 lua_Integer n;
92 if (lua_numtointeger(d, &n)) /* fits in an integer? */
93 lua_pushinteger(L, n); /* result is integer */
94 else
95 lua_pushnumber(L, d); /* result is float */
96}
97
98
89static int math_ceil (lua_State *L) { 99static int math_ceil (lua_State *L) {
90 if (lua_isinteger(L, 1)) 100 if (lua_isinteger(L, 1))
91 lua_settop(L, 1); /* integer is its own ceil */ 101 lua_settop(L, 1); /* integer is its own ceil */
92 else { 102 else {
93 lua_Integer n;
94 lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); 103 lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
95 if (lua_numtointeger(d, &n)) /* fits in an integer? */ 104 pushnumint(L, d);
96 lua_pushinteger(L, n); /* result is integer */
97 else
98 lua_pushnumber(L, d); /* result is float */
99 } 105 }
100 return 1; 106 return 1;
101} 107}
@@ -124,12 +130,18 @@ static int math_fmod (lua_State *L) {
124** 'double'. 130** 'double'.
125*/ 131*/
126static int math_modf (lua_State *L) { 132static int math_modf (lua_State *L) {
127 lua_Number n = luaL_checknumber(L, 1); 133 if (lua_isinteger(L ,1)) {
128 /* integer part (rounds toward zero) */ 134 lua_settop(L, 1); /* number is its own integer part */
129 lua_Number ip = (n < 0) ? -l_mathop(floor)(-n) : l_mathop(floor)(n); 135 lua_pushnumber(L, 0); /* no fractionary part */
130 lua_pushnumber(L, ip); 136 }
131 /* fractionary part (test needed for inf/-inf) */ 137 else {
132 lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip)); 138 lua_Number n = luaL_checknumber(L, 1);
139 /* integer part (rounds toward zero) */
140 lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
141 pushnumint(L, ip);
142 /* fractionary part (test needed for inf/-inf) */
143 lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip));
144 }
133 return 2; 145 return 2;
134} 146}
135 147