[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: lua_number2integer
- From: Mike Pall <mikelu-0809@<a href="/cgi-bin/echo.cgi?mike.de">...</a>>
- Date: Thu, 25 Sep 2008 14:56:53 +0200
Cuero Bugot wrote:
> Here is the code which revealed the unconsistency of the number to integer
> string.char(112+remaining/256, remaining%256)
> where remaining was taken from 0 to 4095.
> This code was done assuming that the string.char function was "correctly
> truncating" the value as C would do it in such a case.
Passing non-integral numbers to library functions expecting
integral numbers is "undefined". There is no "correct rounding
mode" in this case -- Lua is not C.
Rewrite your example as:
local r = remaining % 256
string.char(112 + (remaining-r)/256, r)
Or better use the struct library:
Get it from: http://www.inf.puc-rio.br/~roberto/struct/