Can someone please explain this?

Can someone explain why the display object coordinates does this? When you set a number for an object’s coordinates, the number changes to something else.

It kept causing a bug in my code and took  extremely  long  to find T_T. Can someone please explain why the coordinate changes on its own?

CODE:

local number = 0.1 player = display.newRect( number, 100, 100, 100 ) print(player.x, number)

CONSOLE:

0.10000000149012 0.1

Is this a glitch???

Not a glitch but how floating point numbers are represented in computer languages. It can also be cause by content scaling that occurs when rending the newRect on the device/skin. Lua doesn’t use integers so numbers may lose a little accuracy when math is applied. You also need to be careful because display object resolution is whole pixels and not a fractional pixel as in your example.

ok thanks for the answer. I’ll round off the numbers from now on. It also affected whole pixels btw, when I did 750*(1/6) it went to 24.999… instead of 125, probably because 1/6 is a fraction

I found this out when I was tearing my hair out one time, which peaked when this test returned false:

if 1 == 1 then

Presumably the floats were actually 1.00000001 and 0.99999999 or something like that.

This is why I rarely do this:

if 1+1 == 2 then

because the floating point issue can cause errors, so instead I do:

if 1+1 \> 1 and 1=1 \< 3 then

Only where it’s necessary of course.

Cool I’ll try that next time. I know exactly how hair-tearing-ly frustrating this is too

Not a glitch but how floating point numbers are represented in computer languages. It can also be cause by content scaling that occurs when rending the newRect on the device/skin. Lua doesn’t use integers so numbers may lose a little accuracy when math is applied. You also need to be careful because display object resolution is whole pixels and not a fractional pixel as in your example.

ok thanks for the answer. I’ll round off the numbers from now on. It also affected whole pixels btw, when I did 750*(1/6) it went to 24.999… instead of 125, probably because 1/6 is a fraction

I found this out when I was tearing my hair out one time, which peaked when this test returned false:

if 1 == 1 then

Presumably the floats were actually 1.00000001 and 0.99999999 or something like that.

This is why I rarely do this:

if 1+1 == 2 then

because the floating point issue can cause errors, so instead I do:

if 1+1 \> 1 and 1=1 \< 3 then

Only where it’s necessary of course.

Cool I’ll try that next time. I know exactly how hair-tearing-ly frustrating this is too