[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lua_number2integer
- From: Mike Pall <mikelu-0809@...>
- 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
> process:
>
> 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:
struct.pack(">H", 0x7000+remaining)
Get it from: http://www.inf.puc-rio.br/~roberto/struct/
--Mike