lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

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: