Weird "greater than" bug that happens randomly

Hello all, i’ve been working on my game and i notice something weird. I tried to reproduce it but it only happens randomly.

Issue:
greater than won’t detect properly, it will detect 0.2 > 0.2 = true for example.
The weirdest thing is it happens 90% of the time in my game. (Its the only function that deducts that particular variable, double checked)

Here is the code to reproduce it (random chance):

[lua]local a = 0.3
local b = 0.2

local function temp( event )

if a > b then
print(a … “>” … b)
a = a - 0.1
end

end

local function touched( event )
if event.phase == “ended” then
a = a + 0.2
end
end

local function main()
timer.performWithDelay(1000, temp, 0)
Runtime:addEventListener(“touch”, touched)
end

main()[/lua]

any help would be appreciated, its really killing me ._. Thanks in advance
EDIT: Updated with full portion of code to reproduce the bug [import]uid: 74723 topic_id: 13081 reply_id: 313081[/import]

i tried running this code several times and the case like you said never happened.
is it called inside any event ? in that case there is chance that the event is called 2 times together… [import]uid: 71210 topic_id: 13081 reply_id: 48062[/import]

My bad, i forgot to copy a portion of the code to reproduce the bug.
Touch the screen once and it becomes really obvious

[lua]local a = 0.3
local b = 0.2

local function temp( event )

if a > b then
print(a … “>” … b)
a = a - 0.1
end

end

local function touched( event )
if event.phase == “ended” then
a = a + 0.2
end
end

local function main()
timer.performWithDelay(1000, temp, 0)
Runtime:addEventListener(“touch”, touched)
end

main()[/lua] [import]uid: 74723 topic_id: 13081 reply_id: 48064[/import]

seems like it is a bug
am getting the error even in this below code
[lua]local a = 0.4
local b = 0.2

local function temp( event )
if a > b then
print(a … “>” … b)
print(a>b)
a = a - 0.1
end
end

timer.performWithDelay(1000, temp, 0)[/lua] [import]uid: 71210 topic_id: 13081 reply_id: 48067[/import]

Thanks for the reply and confirmation!
I hope it gets fix soon, using alot of > in my code! [import]uid: 74723 topic_id: 13081 reply_id: 48069[/import]

Its not a corona bug, its to do with LUA’s floating point accuracy.

if you do this:

print(0.4 - 0.1)

you get the answer

0.30000000000000004

even when not using corona

try here for some ideas

here [import]uid: 50154 topic_id: 13081 reply_id: 48070[/import]

yes its even at the basic decimal subtraction…
see this
[lua]local a = 0.4
local b = 0.2

a = a - 0.1
a = a - 0.1

if a > b then
print(a … “>” … b)
end[/lua] [import]uid: 71210 topic_id: 13081 reply_id: 48071[/import]

It’s showing me 0.3, may i know how do you get it to print so many decimal values? [import]uid: 74723 topic_id: 13081 reply_id: 48072[/import]

I ran the code in another LUA interpreter (Kahlua) [import]uid: 50154 topic_id: 13081 reply_id: 48073[/import]

you could try this if you must use floating point numbers:

[code]

local function temp( event )

local aa = string.format(’%.02f’, a)
local bb = string.format(’%.02f’, b)
if (aa > bb) then
print(aa … “>” … bb)
print(aa>bb)
a = a - 0.1
end

end

[/code] [import]uid: 50154 topic_id: 13081 reply_id: 48077[/import]

Thanks for the reply and possible solution.

I am currently using tostring(a) > tostring(b) to compare, and it works perfectly [: [import]uid: 74723 topic_id: 13081 reply_id: 48079[/import]