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 /lvm.c | |
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
Diffstat (limited to 'lvm.c')
-rw-r--r-- | lvm.c | 20 |
1 files changed, 14 insertions, 6 deletions
@@ -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)); |