diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-27 09:43:37 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-27 09:43:37 -0300 |
commit | d630daca1a3dad6357226fdc6472692fa7e4b5c0 (patch) | |
tree | c4fa77406b680091b4325e46736fe85ac012279c | |
parent | 8c883cb4e86a6c176465c9347dfff5e0044f1c93 (diff) | |
download | lua-d630daca1a3dad6357226fdc6472692fa7e4b5c0.tar.gz lua-d630daca1a3dad6357226fdc6472692fa7e4b5c0.tar.bz2 lua-d630daca1a3dad6357226fdc6472692fa7e4b5c0.zip |
"legal" way to convert a float to an integer in C
-rw-r--r-- | llimits.h | 8 | ||||
-rw-r--r-- | ltable.c | 12 | ||||
-rw-r--r-- | lvm.c | 20 |
3 files changed, 30 insertions, 10 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llimits.h,v 1.104 2013/04/25 21:15:37 roberto Exp roberto $ | 2 | ** $Id: llimits.h,v 1.105 2013/05/23 21:27:06 roberto Exp roberto $ |
3 | ** Limits, basic types, and some other `installation-dependent' definitions | 3 | ** Limits, basic types, and some other `installation-dependent' definitions |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -36,6 +36,12 @@ typedef unsigned char lu_byte; | |||
36 | 36 | ||
37 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ | 37 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ |
38 | 38 | ||
39 | |||
40 | /* minimum and maximum values for lua_Integer */ | ||
41 | #define MAX_INTEGER ((lua_Integer)(~(lua_Unsigned)0 >> 1)) | ||
42 | #define MIN_INTEGER (~MAX_INTEGER) | ||
43 | |||
44 | |||
39 | /* | 45 | /* |
40 | ** conversion of pointer to integer | 46 | ** conversion of pointer to integer |
41 | ** this is for hashing only; there is no problem if the integer | 47 | ** this is for hashing only; there is no problem if the integer |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 2.74 2013/04/26 15:39:25 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.75 2013/04/29 17:12:50 roberto Exp roberto $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -65,6 +65,12 @@ | |||
65 | #define hashpointer(t,p) hashmod(t, IntPoint(p)) | 65 | #define hashpointer(t,p) hashmod(t, IntPoint(p)) |
66 | 66 | ||
67 | 67 | ||
68 | /* checks whether a float has a value representable as a lua_Integer | ||
69 | (and does the conversion if so) */ | ||
70 | #define numisinteger(x,i) \ | ||
71 | (((x) == l_mathop(floor)(x)) && luaV_numtointeger(x, i)) | ||
72 | |||
73 | |||
68 | #define dummynode (&dummynode_) | 74 | #define dummynode (&dummynode_) |
69 | 75 | ||
70 | #define isdummy(n) ((n) == dummynode) | 76 | #define isdummy(n) ((n) == dummynode) |
@@ -412,7 +418,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { | |||
412 | lua_Integer k; | 418 | lua_Integer k; |
413 | if (luai_numisnan(L, n)) | 419 | if (luai_numisnan(L, n)) |
414 | luaG_runerror(L, "table index is NaN"); | 420 | luaG_runerror(L, "table index is NaN"); |
415 | if (luaV_numtointeger(n, &k)) { /* index is int? */ | 421 | if (numisinteger(n, &k)) { /* index is int? */ |
416 | setivalue(&aux, k); | 422 | setivalue(&aux, k); |
417 | key = &aux; /* insert it as an integer */ | 423 | key = &aux; /* insert it as an integer */ |
418 | } | 424 | } |
@@ -494,7 +500,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { | |||
494 | case LUA_TNIL: return luaO_nilobject; | 500 | case LUA_TNIL: return luaO_nilobject; |
495 | case LUA_TNUMFLT: { | 501 | case LUA_TNUMFLT: { |
496 | lua_Integer k; | 502 | lua_Integer k; |
497 | if (luaV_numtointeger(fltvalue(key), &k)) /* index is int? */ | 503 | if (numisinteger(fltvalue(key), &k)) /* index is int? */ |
498 | return luaH_getint(t, k); /* use specialized version */ | 504 | return luaH_getint(t, k); /* use specialized version */ |
499 | /* else go through */ | 505 | /* else go through */ |
500 | } | 506 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.169 2013/05/06 17:17:09 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.170 2013/05/26 14:47:51 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -58,17 +58,25 @@ int luaV_tostring (lua_State *L, StkId obj) { | |||
58 | } | 58 | } |
59 | 59 | ||
60 | 60 | ||
61 | /* | ||
62 | ** Check whether a float number is within the range of a lua_Integer. | ||
63 | ** (The comparisons are tricky because of rounding, which can or | ||
64 | ** not occur depending on the relative sizes of floats and integers.) | ||
65 | ** This function is called only when 'n' has an integer value. | ||
66 | */ | ||
61 | int luaV_numtointeger (lua_Number n, lua_Integer *p) { | 67 | int luaV_numtointeger (lua_Number n, lua_Integer *p) { |
62 | lua_Integer k; | 68 | if (cast_num(MIN_INTEGER) <= n && n < (MAX_INTEGER + cast_num(1))) { |
63 | lua_number2integer(k, n); | 69 | *p = cast_integer(n); |
64 | if (luai_numeq(cast_num(k), n)) { /* 'k' is int? */ | 70 | lua_assert(cast_num(*p) == n); |
65 | *p = k; | ||
66 | return 1; | 71 | return 1; |
67 | } | 72 | } |
68 | return 0; | 73 | return 0; /* number is outside integer limits */ |
69 | } | 74 | } |
70 | 75 | ||
71 | 76 | ||
77 | /* | ||
78 | ** try to convert a non-integer value to an integer | ||
79 | */ | ||
72 | int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { | 80 | int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { |
73 | lua_Number n; | 81 | lua_Number n; |
74 | lua_assert(!ttisinteger(obj)); | 82 | lua_assert(!ttisinteger(obj)); |