lua-users home
lua-l archive

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


On 16 April 2011 22:23,  <jgiors@threeeyessoftware.com> wrote:
>> -------- Original Message --------
>> Date: Fri, 15 Apr 2011 22:25:36 -0500
>> From: Javier Guerra Giraldez <javier@guerrag.com>
>> Subject: Re: problem with formatting large integers
>> To: Lua mailing list <lua-l@lists.lua.org>
>> Message-ID: <BANLkTi=st1LfWWBiTYSD7RGPE5a_53vvtw@mail.gmail.com>
>> Content-Type: text/plain; charset=UTF-8
>>
>> On Fri, Apr 15, 2011 at 7:28 PM,  <jgiors@threeeyessoftware.com> wrote:
>> > Point of note: The string.format code includes a cast from the floating
>> > point format to the integer format (i.e. it's not just a detail of
>> > sprintf).
>>
>> to be specific:  on %i, it uses luaL_checkint(), which casts from
>> lua_Integer to int.  since sprintf()'s %i assumes an int, to change it
>> to a long, or long long would mean inserting one or two 'l's on the
>> pattern before calling sprintf()
>>
>> so it _is_ a detail of sprintf, because %i requires an int.
>>
>> of course, the easiest solution is to just compile with a 64-bit int :-)
>>
>> --
>> Javier
>>
>
> I think we're looking at this from different points of view. From my
> point of view, string.format specifies format patterns which are often
> parallel to, but not necessarily identical to the ones used by sprintf
> (case in point: %q pattern which does not exist for sprintf). With this
> view, Lua is free to translate %i into some other format which works for
> the integer sizes Lua actually supports. A cast from floating point to
> integer would be unnecessary.
>
> If you take an alternate view that a string.format %i is necessarily
> translated into an equivalent sprintf %i, then the cast is necessary and
> there isn't really anything that can be done about Lua integers which
> are outside the sprintf %i range.

The second point of view is the one documented in the lua reference manual.

http://www.lua.org/manual/5.1/manual.html#pdf-string.format

    henk