lua-users home
lua-l archive

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


On Jul 16, 2015 8:06 AM, "Roberto Ierusalimschy" <roberto@inf.puc-rio.br> wrote:
>
> > On Tue, Jul 14, 2015 at 9:20 PM, Parke <parke.nexus@gmail.com> wrote:
> > > On Tue, Jul 14, 2015 at 5:58 PM, Rena <hyperhacker@gmail.com> wrote:
> > >> This is something I've been stubbing my toe on a lot lately and it's
> > >> rather annoying:
> > >>
> > >> Lua 5.3.1  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> > >> ; print(string.format('%d', 1.1))
> > >
> > >> In previous versions - and in C - it would just silently truncate the
> > >> number (drop the fractional part).
> > >
> > > In C?  Really?  What compiler?
> > >
> >
> > Maybe it was only the one in my imagination... :)
>
> Any correct C compiler will truncate as you said (towards zero), when
> it converts a float to an integer. 'printf' is an exception, because
> it is a vararg function and, therefore, without type checking. (With
> no type checking, the compiler does not know it has to convert.)
>
> -- Roberto
>

Right, I think I mixed up the automatic casting when assigning to an int and the automatic promotion to int when passing through a vararg list.

In any case, I'm still wishing Lua would truncate for %d. I keep ending up with seemingly harmless UI code crashing with a "number has no integer representation" error, and ugly floor() littered everywhere. It reminds me of having to use tostring() with %s in earlier versions (a similar case which annoyed me as well). But perhaps I'm alone here...