[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: problem with formatting large integers
- From: "Joseph Manning" <manning@...>
- Date: Fri, 15 Apr 2011 21:21:31 +0100 (IST)
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.
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".
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
------------------------------------------------------------------------