[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Conversion of strings to numbers and numbers to strings.
- From: Mike Pall <mikelu-0605@...>
- Date: Tue, 30 May 2006 14:03:30 +0200
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():
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).