Setting X Value Of A Display Object To A Decimal Results In Strange Values

Hi,

I’m giving a go to the AKTween library from https://github.com/ArdentKid/AK-Tween and I ran into a problem with corona.

When using translate function with a decimal I end up with a strange value set by Corona.

Because of that, after 100 of translates I end up with object not where I want it to be.

Take a look at this example:

-- obj is a display object obj.x, obj.y = 0, 0 obj.translate(0.02778, 0) print(obj.x) -- this is printed: 0.027780000120401

Why would this 0.000000000120401 be appended?

How can I get rid of it?

This basically makes AK-Tween library useless :confused:

I’ve never used AK-Tween, but seems to me you could just add a math.round() to get rid of the decimal :slight_smile:

C

Lua uses double-precision floating-point representation of any numbers that is why you get you are not getting the exact same value. You might

want to keep your counting in a separate variable and setting the x value with this…

If you just want fancy output the bottom of this page tells you how to get pretty printing:

http://lua-users.org/wiki/StringsTutorial

It’s not about printing values, it’s about the value in obj.x for example.

It definitely looks like this is a Corona issue: in lua you can do:

local t = 0 - 0.01852 print(t) -- this results in -0.01852

Again, this is not about printing but about values assigned to the variables.

I’m rounding to 5 decimal digits already.

No earthly idea what’s happening…

Can someone from the staff check this out?

This does look like a bug to me.  It’s not even necessary to use translate – simply setting an object’s coordinate to a decimal value (other than 0.5, it seems) will result in some small epsilon value added to (or subtracted from) the coordinate.

Here’s my test case (tested only on the Mac simulator, not a device; no build.settings or config.lua necessary):

[lua]

obj = display.newRect(0,0,50,50)

obj.x, obj.y = 100.5, 100.4

print(obj.x, obj.y)

– Printout looks like this:

– 100.5    100.40000152588

[/lua]

Krystian, if you haven’t already, it would be great to submit a bug report (or I’d be happy to, but you were the first to point this out).

  • Andrew

It was submitted under case 

22141

I’ve never used AK-Tween, but seems to me you could just add a math.round() to get rid of the decimal :slight_smile:

C

Lua uses double-precision floating-point representation of any numbers that is why you get you are not getting the exact same value. You might

want to keep your counting in a separate variable and setting the x value with this…

If you just want fancy output the bottom of this page tells you how to get pretty printing:

http://lua-users.org/wiki/StringsTutorial

It’s not about printing values, it’s about the value in obj.x for example.

It definitely looks like this is a Corona issue: in lua you can do:

local t = 0 - 0.01852 print(t) -- this results in -0.01852

Again, this is not about printing but about values assigned to the variables.

I’m rounding to 5 decimal digits already.

No earthly idea what’s happening…

Can someone from the staff check this out?

This does look like a bug to me.  It’s not even necessary to use translate – simply setting an object’s coordinate to a decimal value (other than 0.5, it seems) will result in some small epsilon value added to (or subtracted from) the coordinate.

Here’s my test case (tested only on the Mac simulator, not a device; no build.settings or config.lua necessary):

[lua]

obj = display.newRect(0,0,50,50)

obj.x, obj.y = 100.5, 100.4

print(obj.x, obj.y)

– Printout looks like this:

– 100.5    100.40000152588

[/lua]

Krystian, if you haven’t already, it would be great to submit a bug report (or I’d be happy to, but you were the first to point this out).

  • Andrew

It was submitted under case 

22141