lua-users home
lua-l archive

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


On Tue, Jul 21, 2020 at 1:38 AM Andrew Gierth wrote:
How about,

local f, t = function()return function()end end, {nil, -1,
  [false] = {[-1/0] = 'Lua 5.1', [1/0] = 'Lua 5.4'},
  [true]  = {[-1/0] = 'Lua 5.2', [1/0] = 'Lua 5.3'},
  [1]     = 'LuaJIT'}
print(t[1] or t[f()==f()][1/(t[2]*0)])

Not sure how reliably the t[2]*0 evaluates to -0 on 5.1/5.2, though. It
works on the few platforms I tried. (Using a constant -1 there doesn't
work.)

Your idea is correct: we have to calculate negative zero in runtime.
This is because of "zero-bug" in Lua 5.1:
both +0 and -0 can't coexist in the constant's table of Lua 5.1 bytecode.
Example:
z=-0;print(1/0)
The "zero-bug" is not listed in the official Lua bugs page, but it was fixed in Lua 5.2.0.

I've rewritten your code to make it a few bytes shorter:

local f, t = function()return function()end end, {0, [1] = -1,
   [false] = {[-1/0] = 'Lua 5.1', [1/0] = 'Lua 5.4', 'LuaJIT'},
   [true]  = {[-1/0] = 'Lua 5.2', [1/0] = 'Lua 5.3'}}
local version = t[f()==f()][1/-t[1]]