Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LUA (Corona SDK) math.floor() return wrong value with increment of 0.1

Tags:

lua

coronasdk

I have a strange bug when i use Math.floor function on double values incremented of 0.1 each time.

It happens only with an increment of 0.1 and start at value 5

Exemple : math.floor(4) return 4 but math.floor(5) return 4 !!!

You can try it yourself, with this simple piece of code :

for i=3,7,0.1 do 
    print("val : "..i.." floor : "..math.floor(i))
end

Here is the result of the above code :

 Corona Simulator[1494:707] val : 3 floor : 3
 Corona Simulator[1494:707] val : 3.1 floor : 3
 Corona Simulator[1494:707] val : 3.2 floor : 3
 Corona Simulator[1494:707] val : 3.3 floor : 3
 Corona Simulator[1494:707] val : 3.4 floor : 3
 Corona Simulator[1494:707] val : 3.5 floor : 3
 Corona Simulator[1494:707] val : 3.6 floor : 3
 Corona Simulator[1494:707] val : 3.7 floor : 3
 Corona Simulator[1494:707] val : 3.8 floor : 3
 Corona Simulator[1494:707] val : 3.9 floor : 3
 Corona Simulator[1494:707] val : 4 floor : 4
 Corona Simulator[1494:707] val : 4.1 floor : 4
 Corona Simulator[1494:707] val : 4.2 floor : 4
 Corona Simulator[1494:707] val : 4.3 floor : 4
 Corona Simulator[1494:707] val : 4.4 floor : 4
 Corona Simulator[1494:707] val : 4.5 floor : 4
 Corona Simulator[1494:707] val : 4.6 floor : 4
 Corona Simulator[1494:707] val : 4.7 floor : 4
 Corona Simulator[1494:707] val : 4.8 floor : 4
 Corona Simulator[1494:707] val : 4.9 floor : 4
 Corona Simulator[1494:707] val : 5 floor : 4
 Corona Simulator[1494:707] val : 5.1 floor : 5
 Corona Simulator[1494:707] val : 5.2 floor : 5
 Corona Simulator[1494:707] val : 5.3 floor : 5
 Corona Simulator[1494:707] val : 5.4 floor : 5
 Corona Simulator[1494:707] val : 5.5 floor : 5
 Corona Simulator[1494:707] val : 5.6 floor : 5
 Corona Simulator[1494:707] val : 5.7 floor : 5
 Corona Simulator[1494:707] val : 5.8 floor : 5
 Corona Simulator[1494:707] val : 5.9 floor : 5
 Corona Simulator[1494:707] val : 6 floor : 5
 Corona Simulator[1494:707] val : 6.1 floor : 6
 Corona Simulator[1494:707] val : 6.2 floor : 6
 Corona Simulator[1494:707] val : 6.3 floor : 6
 Corona Simulator[1494:707] val : 6.4 floor : 6
 Corona Simulator[1494:707] val : 6.5 floor : 6
 Corona Simulator[1494:707] val : 6.6 floor : 6
 Corona Simulator[1494:707] val : 6.7 floor : 6
 Corona Simulator[1494:707] val : 6.8 floor : 6
 Corona Simulator[1494:707] val : 6.9 floor : 6
 Corona Simulator[1494:707] val : 7 floor : 6

If it can help, if we applied a modulo 1 on the result of a buggy value, we get this as result : 0.99999999999999

like image 996
chip Avatar asked Mar 20 '13 12:03

chip


2 Answers

Floating point numbers are devilish. Don't use them as loop counters.

for i = 30, 70 do 
    local i = i / 10
    print("val : "..i.." floor : "..math.floor(i))
end
like image 145
Egor Skriptunoff Avatar answered Oct 19 '22 03:10

Egor Skriptunoff


indeed it's a common float problem,

Here is the official response i've received from corona support :

Hello,

What you are seeing is a general problem with floating point numbers. All numbers in Lua are floating point numbers and as such are subject to rounding errors. Adding 0.1 to another floating point number is not exactly 0.1 and the error accumulative when used over and over again. When you think the number is 5.0, it may be 4.99999999999999999, so floor returns 4. The print statement may be rounding up before printing.

The math library is standard Lua code (v5.1) and has not been modified for Corona.

You can google the subject or floating point rounding problems, but in general you need to be careful when using floating point math.

Regards, Tom

-- Corona Labs - Support [email protected]

like image 31
chip Avatar answered Oct 19 '22 01:10

chip