Weird decimals got printed in a loop script

I just made a simple loop script where each loop makes (clones) a frame and adds 0.05 to Position.Y.Scale, so each frame is in different Y (starts from 0).

image

I printed frames’ positions and it somehow managed to make some long decimals. I don’t know how could this happen.

image

I appreciate your help.
Thanks!

You’re getting floating point errors. In short, computers can’t accurately represent decimals with full precision all the time.

1 Like

As long as you don’t rely on the values being exact, it won’t cause any harm whatsoever. As @Kampfkarren explained, it’s just a floating point error. Essentially, just ignore it unless you need your decimals to be exact, which in this specific scenario doesn’t seem necessary.

2 Likes

Thanks to both of you but I think it’s kinda necessary in visual.

I guess I’m gonna use something else.

What about using a UIGridLayout?

You can do this sort of thing automatically and change values really easily.

1 Like

Gonna check it out, thanks!

An alternative could be an ImageLabel with the ScaleType as Tile, which I think could work pretty nicely.

1 Like

ListLayout seems more apt for this situation.

Also, you could try rounding your numbers to the nearest .05 with code like this. Not sure if this rounding will cause floating point imprecision either though.

function round20ths(num)
return math.floor(num*20+.5)/20
end)

Edit: I realize now that rounding will not help the situation.

1 Like

Regardless of rounding, the value is still a number that has to be represented in binary, and therefore is still partial to floating point errors.

2 Likes

My understanding was that any number with a 5 or 0 at the end could be completely represented in binary. Might be wrong haha but from all the calculations I did in A level computing papers any number ending with a 5 wasn’t subject to errors.

Use math.abs on the final number. Instant solution to float point errors, no magic or long explanations needed. Tried it when I had float point errors when using NumberValue objects.

A UIListLayout object will automatically handle positioning for you, it’s up to you to decide in the layout order, size and whatnot though.

https://docs.python.org/2/tutorial/floatingpoint.html

Most (if not all - afaik) whole numbers can be represented precisely in binary. It is specifically certain decimals that cannot be represented accurately.

From what I’ve seen there the representation errors / floating point errors occur when the answers cannot be represented in binary. A representation of, let’s say, 3.5 (assume 8 bit at fixed point binary) would be 0011.1000 which is accurately 3.5.

The link you provided states “Unfortunately, most decimal fractions cannot be represented exactly as binary fractions” which suggests that there are some. I believe it is any fraction that can be exactly represented (any whole number or any decimal ending in a 5) that have no errors. You’ll notice if you change a number value to 0.4 it becomes 0.399999999 or something insidiously close. However, 0.5 is just 0.5. No (apparent, anyway) floating point errors.

Of course. Many different decimals can be accurately represented in binary. Pretty much, any rational and “terminating” number in base 10 can be represented in base two (i.e 1/2, 1/4 - not 1/3) and any number with a denominator with a power of n^2 (1/2, 1/4, 1/8, 1/16 etc.) can be represented in base two. AFAIK any numbers that don’t follow those rules are susceptible to floating point errors.

edit: the number most follow both of those rules, not one or the other.