lua-users home
lua-l archive

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


Hi list,

a few hours ago I noticed for the first time that print(0*-2) prints
"-0" instead of "0" - because Lua supports
<https://en.wikipedia.org/wiki/Signed_zero> - and I tried to write a
function that would "fix" the "-0"s in the cases that I don't want
them... and well, it turns out that this doesn't work on Lua 5.1.5:

  fix0 = function (x) if (x == -0) then return  0 else return x end end

Here's a test:

--snip--snip--

cd

cat > /tmp/testminus0.lua <<'%%%'
f = function (x) if (x ==  0) then return  0 else return x end end
g = function (x) if (x == -0) then return  0 else return x end end --
buggy on Lua5.1
h = function (x) if (x == -0) then return 42 else return x end end
print(f(0), f(-0), f(99))
print(g(0), g(-0), g(99))
print(h(0), h(-0), h(99))
%%%

lua5.1 /tmp/testminus0.lua
lua5.2 /tmp/testminus0.lua
lua5.3 /tmp/testminus0.lua
lua5.4 /tmp/testminus0.lua

--snip--snip--

It prints this:

/home/edrx(edrx:sc)# lua5.1 /tmp/testminus0.lua
0       0       99
-0      -0      99
42      42      99
/home/edrx(edrx:sc)# lua5.2 /tmp/testminus0.lua
0       0       99
0       0       99
42      42      99
/home/edrx(edrx:sc)# lua5.3 /tmp/testminus0.lua
0       0       99
0       0       99
42      42      99
/home/edrx(edrx:sc)# lua5.4 /tmp/testminus0.lua
0       0       99
0       0       99
42      42      99
/home/edrx(edrx:sc)#

So it seems that this doesn't work,

  fix0 = function (x) if (x == -0) then return  0 else return x end end

but this does:

  fix0 = function (x) if (x ==  0) then return  0 else return x end end

Where is that bug documented? I would like to put in my code a link
that explains why the "more natural" version with (x == -0) doesn't
work, and why we need the "weird" version with (x == 0)...

Thanks in advance!
  Eduardo Ochs
  http://angg.twu.net/#eev


P.S.: I am using Debian Stable.