lua-users home
lua-l archive

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


Hi,

David Jones wrote:
> By the way, I was surprised by the result of
> 
> return '-0x1' + 0
> 
> I was even more surprised when it gave a different answer on my PowerPC 
> OS X machine (where it gives -1) than on an Intel Windows XP machine 
> (where it gives 4294967295).

Conversion of negative hex numbers may lead to unpredictable
results. It works in the parser (because it strips off the unary
minus first), but not in implicit coercions or with tonumber():

  http://lua-users.org/lists/lua-l/2006-02/msg00227.html

It's hard to avoid unless your C library is C99 compliant.
Alas, MSVCRT is not and we have 2006 -- complain to M$. ;-)


Summary: Hex numbers only convert well with a C99 compliant libc.
But Lua does not rely on it and has a partial fallback.

The side-effects of the fallback are: Negative hex numbers are
undefined. Positive hex numbers are only defined up to the
minimum precision of an unsigned long and lua_Number (which may
lead to surprises esp. when lua_Number is a float).

Bye,
     Mike