From d630daca1a3dad6357226fdc6472692fa7e4b5c0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 May 2013 09:43:37 -0300 Subject: "legal" way to convert a float to an integer in C --- ltable.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'ltable.c') diff --git a/ltable.c b/ltable.c index 2e95ce26..ae92cbd9 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.74 2013/04/26 15:39:25 roberto Exp roberto $ +** $Id: ltable.c,v 2.75 2013/04/29 17:12:50 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -65,6 +65,12 @@ #define hashpointer(t,p) hashmod(t, IntPoint(p)) +/* checks whether a float has a value representable as a lua_Integer + (and does the conversion if so) */ +#define numisinteger(x,i) \ + (((x) == l_mathop(floor)(x)) && luaV_numtointeger(x, i)) + + #define dummynode (&dummynode_) #define isdummy(n) ((n) == dummynode) @@ -412,7 +418,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { lua_Integer k; if (luai_numisnan(L, n)) luaG_runerror(L, "table index is NaN"); - if (luaV_numtointeger(n, &k)) { /* index is int? */ + if (numisinteger(n, &k)) { /* index is int? */ setivalue(&aux, k); key = &aux; /* insert it as an integer */ } @@ -494,7 +500,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { case LUA_TNIL: return luaO_nilobject; case LUA_TNUMFLT: { lua_Integer k; - if (luaV_numtointeger(fltvalue(key), &k)) /* index is int? */ + if (numisinteger(fltvalue(key), &k)) /* index is int? */ return luaH_getint(t, k); /* use specialized version */ /* else go through */ } -- cgit v1.2.3-55-g6feb