[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: formatting long integers
- From: WU Jun <quark@...>
- Date: Wed, 17 Mar 2010 10:34:08 +0800
C programmers should know differences among "%d", "%u", "%.0f" well. Not
a lua issue.
On 03/17/2010 03:34 AM, Leo Razoumov wrote:
> On 32-bit platforms Lua-5.1.x incorrectly formats integers that are
> larger or equal to 2^31. For instance,
>
> ("%d"):format(2^31) --> -2147483648
>
> This behavior is due to the fact that %d formatting rule casts
> lua_Number (double) to the 32-bit signed integer (on 32-bit platform)
> and it overflows. In standard "C" library this problem is addressed
> with a "l" modifier. For example "%lld" would cast to a "long long".
> As it is clearly stated in the manual, Lua format function does not
> support "l" modifier. Thus, this behavior is not a bug but a feature.
>
> I think that the manual should explain in more details what
> consequences this limitation leads to.
>
> In meanwhile, if you need to format as a decimal integer a Lua number
> larger than 31 bit long, use the following trick
>
> ("%.0f"):format(2^48) --> 281474976710656
>
> Should this trick go into the FAQ?
>
> --Leo--
>