[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua 5.3: functional version of // operator?
- From: Roberto Ierusalimschy <roberto@...>
- Date: Mon, 8 Jul 2013 10:32:26 -0300
> > If compatibility with 5.2 is that crucial, why insist on writing code
> > that in Lua 5.3 will return true integers? Why not just write in Lua
> > 5.2 and trust the developers to provide a compatibility option?
> The thing that prompted the exploration of this was that "tostring" in
> Lua 5.3 returns something different for floats than it does for
> integers, even for floats with integer values, and I had some code
> that got confused by that. This code uses floor(a/b) to get that
> value, and I changed it to use ifloor but along the way, realized that
> for some uses it might be nice to directly use "//" for efficiency
> reasons (e.g. Roberto's earlier post about changing some code to use
Probably 'a//b' should be faster than ifloor(a/b), but my specific
point was something else. In my case (and I supect this can be a common
pattern), the problem was not the performance of floor(a/b) per se, but
how its float result "contaminated" the rest of the algorithm. (The
algorithm became a mix of floats and integers, doing conversions all
the time to compare them, to index tables with floats, etc.) In my
particular case, using ifloor(a/b) solved the problem all the same.