Browse Source

"legal" way to convert a float to an integer in C

Roberto Ierusalimschy 12 years ago
parent
commit
d630daca1a
3 changed files with 30 additions and 10 deletions
  1. 7 1
      llimits.h
  2. 9 3
      ltable.c
  3. 14 6
      lvm.c

+ 7 - 1
llimits.h

@@ -1,5 +1,5 @@
 /*
-** $Id: llimits.h,v 1.104 2013/04/25 21:15:37 roberto Exp roberto $
+** $Id: llimits.h,v 1.105 2013/05/23 21:27:06 roberto Exp roberto $
 ** Limits, basic types, and some other `installation-dependent' definitions
 ** See Copyright Notice in lua.h
 */
@@ -36,6 +36,12 @@ typedef unsigned char lu_byte;
 
 #define MAX_INT (INT_MAX-2)  /* maximum value of an int (-2 for safety) */
 
+
+/* minimum and maximum values for lua_Integer */
+#define MAX_INTEGER	((lua_Integer)(~(lua_Unsigned)0 >> 1))
+#define MIN_INTEGER	(~MAX_INTEGER)
+
+
 /*
 ** conversion of pointer to integer
 ** this is for hashing only; there is no problem if the integer

+ 9 - 3
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 */
     }

+ 14 - 6
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 2.169 2013/05/06 17:17:09 roberto Exp roberto $
+** $Id: lvm.c,v 2.170 2013/05/26 14:47:51 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -58,17 +58,25 @@ int luaV_tostring (lua_State *L, StkId obj) {
 }
 
 
+/*
+** Check whether a float number is within the range of a lua_Integer.
+** (The comparisons are tricky because of rounding, which can or
+** not occur depending on the relative sizes of floats and integers.)
+** This function is called only when 'n' has an integer value.
+*/
 int luaV_numtointeger (lua_Number n, lua_Integer *p) {
-  lua_Integer k;
-  lua_number2integer(k, n);
-  if (luai_numeq(cast_num(k), n)) {  /* 'k' is int? */
-    *p = k;
+  if (cast_num(MIN_INTEGER) <= n && n < (MAX_INTEGER + cast_num(1))) {
+    *p = cast_integer(n);
+    lua_assert(cast_num(*p) == n);
     return 1;
   }
-  return 0;
+  return 0;  /* number is outside integer limits */
 }
 
 
+/*
+** try to convert a non-integer value to an integer
+*/
 int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
   lua_Number n;
   lua_assert(!ttisinteger(obj));