lua-users home
lua-l archive

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

Peter Hickman <> writes:

> On 9 June 2011 11:15, David Kastrup <> wrote:
>> Dirk Laurie <> writes:
>> By the way, I also don't consider it a particularly endearing language
>> feature that %d and %x formatting produces garbage when you are leaving
>> the range of C integers but are still well within the domain of Lua
>> integers.
> Just a little nitpick here. Lua does not have integers, they are all floats
> "In the interest of simplicity Lua supports only one type of number,
> floating point numbers."

A Wiki is hardly reference material.

"Programming in Lua" says:

"Lua has no integer type, as it does not need it.  There is a widespread
misconception about floating-point arithmetic errors; some people fear
that even a simple increment can go weird with floating-point numbers.
The fact is that, when you use a double to represent an integer, there
is no rounding error at all (unless the number is greater than 10^14)."

So Lua obviously strives to support integer values.

The reference manual says:

"string.format (formatstring, ...)
Returns a formatted version of its variable number of arguments
following the description given in its first argument (which must be a
string).  The format string follows the same rules as the printf family
of standard C functions.  The only differences are that the
options/modifiers *, l, L, n, p, and h are not supported and that there
is a n extra option, q. [...]  The options c, d, E, e, f, g, G, i, o, u,
X, and x all expect a number as argument, whereas q and s expect a
string.  This function does not accept string values containing embedded

Note that this description does _nowhere_ state that the string actually
is run through C's sprintf or similar, and since Lua has different types
and different modifiers and different format letters, this might well
come as a surprise.  The only clue to this is that strings with embedded
zeros don't work.  But there is no pointer that certain formatting
options produce garbage from well inside of Lua's valid (and proudly
touted) number ranges.

And even when the final stage is done in C: since modifiers like L are
explicitly mentioned, and since Lua obviously goes through both a format
string as well as a value conversion, it seems strange that it does not
convert to an integer type (as well as the corresponding format letter)
that supports a wider range of values.

David Kastrup