lua-users home
lua-l archive

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


On 24.03.2014 05:49, Dirk Laurie wrote:
2014-03-24 2:06 GMT+02:00 Bernd Eggink <monoped@sudrala.de>:

Is it really intended that

     a = math.max(1, 2)

returns a = 2.0 (a float)? This change in behaviour, besides being very
counterintuitive, kills a lot of my modules which rely on tostring(a) giving
"2" (without a decimal part). It means that I would have to surround each
such operation with an ifloor() call, which is pretty ineffective and
annoying.
If that can't be changed, I'd suggest an addition of imin(), imax() and
iabs(), witch return integers.

In Lua 5.2, a = math.max(1, 2) returned that same double.

Thanks, I wasn't aware of that.

The change in behaviour is that a double with the integer value 2 gets
tostring-ed to "2.0", not to "2". You can restore the original behaviour thus:

$ lua53
Lua 5.3.0 (work2)  Copyright (C) 1994-2014 Lua.org, PUC-Rio
math.max(1,2)
2.0
debug.setmetatable(0,{__tostring = function(x) return ("%.14g"):format(x) end})
0
math.max(1,2)
2

OK, but I guess I rather replace "math.max(a, b)" by "a > b and a or b",
which is less tricky, but about 4 times faster.

Bernd

--
http://sudrala.de