lua-users home
lua-l archive

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


On 2011-Apr-16 (Sat) at 11:23 (-0300), Roberto Ierusalimschy wrote:

>> > Umm ... what does "%i.4" do?
>> 
>> man sprintf

Ah -- thanks for the pointer to enlightenment :-)

I was quite unaware of this conversion -- it's neither in my K&R (1978)
nor in my trusty "C in a (600-page!) Nutshell" -- and had always used
"%04i" instead.

And yes, it does somewhat complicate my suggestion of simply translating
"%i" into "%.0f" in the implementation of "string.format".

For anyone else who may have shared my previous state of ignorance(!),
a precision specifier in a "%i" conversion gives the minimum number of
digits to be produced, left-padding with '0's as needed; thus

   string.format( "%.4i", 37 )  -->  "0037"

>> > There's hardly a genuine situation in which
>> > 
>> >    string.format( "%i", 2^31 )  -->  -2147483648
>> > 
>> > is a reasonable or desirable outcome?
>> 
>> Lua (as everything else) is a tradeoff between several conflicting
>> goals. I do not think this problem is worth the complexity of its
>> fix. If you want %.0f when you write "%i", write "%.0f".

Hmm, "this problem" is that only 0.000025% of representable integers
( 2^31 / 2^53 ) are formatted correctly by "%i", while 99.999975%
of them remain horribly b0rken.  Just teasing, never mind :-)

OK, while I might disagree philosophically with the acceptance of this
small but ugly wart, I guess it's now up to me to see if I can write
a correct, complete, compact, and efficient patch (setting a high bar)
to format all representable integers with "%i" ...

------------------------------------------------------------------------
Joseph Manning / Computer Science / UCC Cork Ireland / manning@cs.ucc.ie
------------------------------------------------------------------------