diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-06-04 16:34:51 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-06-04 16:34:51 -0300 |
| commit | 932e7fb0e1b35758bd96361479ca3ddab3fc43da (patch) | |
| tree | 4f02a5e87226b0b0ebd9ad17e766f68ccee88bfb /lapi.c | |
| parent | 188192ce9a3d5d111d2c1ceed41d27ab6a5d57dc (diff) | |
| download | lua-932e7fb0e1b35758bd96361479ca3ddab3fc43da.tar.gz lua-932e7fb0e1b35758bd96361479ca3ddab3fc43da.tar.bz2 lua-932e7fb0e1b35758bd96361479ca3ddab3fc43da.zip | |
'lua_tounsigned' takes number modulo 2^numbits as a result when
number is a float
(That may change...)
Diffstat (limited to 'lapi.c')
| -rw-r--r-- | lapi.c | 30 |
1 files changed, 28 insertions, 2 deletions
| @@ -1,10 +1,11 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 2.179 2013/05/02 12:37:24 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.180 2013/05/14 16:00:11 roberto Exp roberto $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | #include <math.h> | ||
| 8 | #include <stdarg.h> | 9 | #include <stdarg.h> |
| 9 | #include <string.h> | 10 | #include <string.h> |
| 10 | 11 | ||
| @@ -376,7 +377,32 @@ LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { | |||
| 376 | 377 | ||
| 377 | 378 | ||
| 378 | LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { | 379 | LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { |
| 379 | return lua_tointegerx(L, idx, pisnum); /* at least for now... <<<< */ | 380 | lua_Unsigned res = 0; |
| 381 | const TValue *o = index2addr(L, idx); | ||
| 382 | int isnum = 0; | ||
| 383 | switch (ttype(o)) { | ||
| 384 | case LUA_TNUMINT: { | ||
| 385 | res = cast_unsigned(ivalue(o)); | ||
| 386 | isnum = 1; | ||
| 387 | break; | ||
| 388 | } | ||
| 389 | case LUA_TNUMFLT: { | ||
| 390 | const lua_Number twop = (~(lua_Unsigned)0) + cast_num(1); | ||
| 391 | lua_Number n = fltvalue(o); | ||
| 392 | n = l_mathop(fmod)(n, twop); | ||
| 393 | n = l_mathop(floor)(n); | ||
| 394 | if (luai_numisnan(L,n)) /* not a number? */ | ||
| 395 | break; /* not an integer, too */ | ||
| 396 | if (n < 0) | ||
| 397 | n += twop; /* correct 'mod' */ | ||
| 398 | res = cast_unsigned(n); | ||
| 399 | isnum = 1; | ||
| 400 | break; | ||
| 401 | } | ||
| 402 | default: break; | ||
| 403 | } | ||
| 404 | if (pisnum) *pisnum = isnum; | ||
| 405 | return res; | ||
| 380 | } | 406 | } |
| 381 | 407 | ||
| 382 | 408 | ||
