lua-users home
lua-l archive

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


Hello Lua maling list,
I have found an interesting bug. if you run

a = 0
b = -0
if (a == b) then
print("0 is -0!")
end


in the lua demo (http://www.lua.org/cgi-bin/demo), then you see that lua does'nt make a difference between 0 and -0.

Can can make -0 when you try this in the lua demo:

a = -20
b = 0
c = a * b
print(c)


This example shows, when you multiply -20 and 0 that lua gives -0. The use of (-20) has the same output. You can change the output from -0 to 0, when you run this:

a = (-20)
b = 0
c = a * b
if (c == -0) then
c = 0
end
print(c)


or this:

a = (-20)
b = 0
c = a * b
if (c == 0) then
c = 0
end
print(c)

The problem of the output from -0 is that -0 does'nt look good. The user (for example a wow addon user) can be disoriented, when he read the addon output: "(-20) * 0 = -0.

We know that 0 and -0 is the same for lua. But when you use a repeat and random numbers:

repeat
a = math.random(-20, 20);
b = math.random(-20, 20);
c = a * b
if (c == -0) then
c = 0
end
print(a..' -- a')
print(b..' -- b')
print(c..' -- c')
print('########')
until c == 0


then became I for example this block:

0 -- a
19 -- b
-0 -- c
########


but when I use this line if (c == 0) then for the line if (c == -0) then then I get this block:

0 -- a
19 -- b
0 -- c
########


The big problem is that lua make a -0. 0 * -0 isn't -0. The right solution is 0! Here my code for this problem:

a = 0
b = -a
c = a * b
print(c)


I hope that can help you to make Lua better. Lua is great!