lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 53210d7e5b55494824a86d63e7f5f6f7ea1d8c17
parent 0394314c7ab2918c60d55d33906402a71a0e324c
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Fri, 14 Jun 2013 15:32:20 -0300

correct way (I hope) to convert floats to unsigned int

Diffstat:
Mlapi.c | 12++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/lapi.c b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.180 2013/05/14 16:00:11 roberto Exp roberto $ +** $Id: lapi.c,v 2.181 2013/06/04 19:34:51 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -389,13 +389,17 @@ LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { case LUA_TNUMFLT: { const lua_Number twop = (~(lua_Unsigned)0) + cast_num(1); lua_Number n = fltvalue(o); - n = l_mathop(fmod)(n, twop); + int neg = 0; n = l_mathop(floor)(n); + if (n < 0) { + neg = 1; + n = -n; + } + n = l_mathop(fmod)(n, twop); if (luai_numisnan(L,n)) /* not a number? */ break; /* not an integer, too */ - if (n < 0) - n += twop; /* correct 'mod' */ res = cast_unsigned(n); + if (neg) res = -res; isnum = 1; break; }