[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [Bug] converting from hex doesn't work for large numbers
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 15 Dec 2010 12:53:49 -0200
> I have a following bug report for you:
>
> Consider a big number
>
> a=math.pow(2, 32) + 14 --4294967310 in dec, 10000000e in hex
>
> print(tonumber("4294967310") -> 4294967310
> print(tonumber("10000000e", 16) -> 4294967295 (0xffffffff, that is LONG_MAX)
>
> tonumber() doesn't convert numbers greater than 2**31-1 from bases
> different than default decimal.
>
> [...]
>
> I see three ways out of this situation:
> (a) use strtoull(3) (disadvantage: AFAIK that is not in C89)
>
> (b) check errno for ERANGE and make a run-time error when it's set.
> (advantage: simple; disadvantage: doesn't make my program work, only
> shows the bug clearly)
>
> (c) make our own re-implementation of strtoull(3), which works for
> all possible lengths of lua_Number.
We will not make our own implementation for strtoull, but we already
made our own implementation for the hexa part of strtod. So, a call
like tonumber("0x10000000e") will work correctly. For the other cases,
we will consider the (b) option.
-- Roberto