[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: problem with formatting large integers
- From: <jgiors@...>
- Date: Fri, 15 Apr 2011 17:28:05 -0700
> ------------------------------
> Date: Fri, 15 Apr 2011 21:21:31 +0100 (IST)
> From: "Joseph Manning" <manning@cs.ucc.ie>
> Subject: problem with formatting large integers
> To: lua-l@lists.lua.org
> Message-ID: <38987.193.203.157.45.1302898891.squirrel@www.cs.ucc.ie>
> Content-Type: text/plain;charset=iso-8859-1
>
> Consider this simple little snippet:
>
> $ lua
> Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> > for n = 2^31-2, 2^31+2 do
> >> print( string.format( "%12i %12.0f", n, n ) )
> >> end
> 2147483646 2147483646
> 2147483647 2147483647
> -2147483648 2147483648
> -2147483648 2147483649
> -2147483648 2147483650
>
> The "%i" conversion works correctly only for integers up to 2^31-1,
> whereas the "%.0f" conversion works for integers all the way up to
> 2^53-1, the maximum integer correctly represented on my platform
> ( Lua 5.1.4 as distributed with Debian 6.0 on an Intel x86 machine ).
>
> Here are some options on what to do about this situation, listed in
> increasing order of my own personal preference:
>
> 1) Do nothing. Accept this small but ugly little wart in Lua,
> whereby some supposedly behind-the-scenes detail from C's "sprintf"
> pokes its way through into Lua.
>
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).
> 2) Deprecate the "%i" (and "%d") formats, and provide "%.0f" as the
> only means to format integers. Although this would fix the problem,
> it could cause significant breakage of existing code; additionally,
> "%.0f" just looks uglier and clumsier than "%i".
>
-1 on this option.
> 3) Rewrite the implementation of "string.format" to be independent of
> C's "sprintf", giving complete and Lua-oriented control over all the
> format conversions. This may be a fair amount of work and may bloat
> the size of the Lua implementation.
>
> 4) Modify the current implementation of "string.format" so that all
> "%i" (and "%d") conversions are intercepted and replaced by the
> equivalent "%.0f" conversions, before then calling C's "sprintf".
> This, imho, is the best overall approach.
>
> Any opinions on these options? Or alternatives?
>
> ------------------------------------------------------------------------
> Joseph Manning / Computer Science / UCC Cork Ireland / manning@cs.ucc.ie
> ------------------------------------------------------------------------
I like option 4 the best. The string.format code already scans through
the string to check formats (looking for "%" tokens), determine the
appropriate casts, etc. It looks like translating %i or %d to %.0f would
work fine. The subroutine "addintlen" would either need to be modified,
or an alternate version added. This change would also potentially save
an unnecessary float to int conversion (probably not very significant in
this case, but nonetheless).
Note: Based on inspecting Lua 5.1 code.
John Giors
http://ThreeEyesSoftware.com
jgiors@ThreeEyesSoftware.com