aboutsummaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-31 16:00:52 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-31 16:00:52 -0300
commit66b7b9b58294961bc07efe6ac4651663772c0e93 (patch)
treed56d2c1e7a39a289d74c6430c3667fca50d45def /lmathlib.c
parent8ef9117924709b0ce013b5f5d2db5bb7fe41b987 (diff)
downloadlua-66b7b9b58294961bc07efe6ac4651663772c0e93.tar.gz
lua-66b7b9b58294961bc07efe6ac4651663772c0e93.tar.bz2
lua-66b7b9b58294961bc07efe6ac4651663772c0e93.zip
math.abs, math.max, and math.min work for integers, too.
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c35
1 files changed, 20 insertions, 15 deletions
diff --git a/lmathlib.c b/lmathlib.c
index a68b6f1e..bede732e 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.91 2013/07/10 20:57:05 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.92 2013/07/22 16:05:53 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*/
@@ -23,7 +23,13 @@
23 23
24 24
25static int math_abs (lua_State *L) { 25static int math_abs (lua_State *L) {
26 lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); 26 if (lua_isinteger(L, 1)) {
27 lua_Integer n = lua_tointeger(L, 1);
28 if (n < 0) n = (lua_Integer)(0u - n);
29 lua_pushinteger(L, n);
30 }
31 else
32 lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
27 return 1; 33 return 1;
28} 34}
29 35
@@ -185,31 +191,30 @@ static int math_ldexp (lua_State *L) {
185} 191}
186 192
187 193
188
189static int math_min (lua_State *L) { 194static int math_min (lua_State *L) {
190 int n = lua_gettop(L); /* number of arguments */ 195 int n = lua_gettop(L); /* number of arguments */
191 lua_Number dmin = luaL_checknumber(L, 1); 196 int imax = 1;
192 int i; 197 int i;
193 for (i=2; i<=n; i++) { 198 luaL_argcheck(L, n >= 1, 1, "value expected");
194 lua_Number d = luaL_checknumber(L, i); 199 for (i = 2; i <= n; i++) {
195 if (d < dmin) 200 if (lua_compare(L, i, imax, LUA_OPLT))
196 dmin = d; 201 imax = i;
197 } 202 }
198 lua_pushnumber(L, dmin); 203 lua_pushvalue(L, imax);
199 return 1; 204 return 1;
200} 205}
201 206
202 207
203static int math_max (lua_State *L) { 208static int math_max (lua_State *L) {
204 int n = lua_gettop(L); /* number of arguments */ 209 int n = lua_gettop(L); /* number of arguments */
205 lua_Number dmax = luaL_checknumber(L, 1); 210 int imax = 1;
206 int i; 211 int i;
207 for (i=2; i<=n; i++) { 212 luaL_argcheck(L, n >= 1, 1, "value expected");
208 lua_Number d = luaL_checknumber(L, i); 213 for (i = 2; i <= n; i++) {
209 if (d > dmax) 214 if (lua_compare(L, imax, i, LUA_OPLT))
210 dmax = d; 215 imax = i;
211 } 216 }
212 lua_pushnumber(L, dmax); 217 lua_pushvalue(L, imax);
213 return 1; 218 return 1;
214} 219}
215 220