Issues With For Loops

So I currently needed to use for loops for something I created and the code looked like this:

for i = 1, 0, -0.1 do
    print(i)
end

And when I ran the code I noticed something strange. The output looked like this:

--[[
Output
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
1.387543.. <--here I got confused
--]]

At first I was confused. I did not know what I was doing wrong. Then I asked some people about what was wrong with my script and they said that that is an issue with for loop float points. The thing is I still need to use for loops. Is there any way to make this work? I mean of course I could do print(1) print(0.9) etc… but definitely not a good way.

1 Like

Sadly this isn’t something wrong with Roblox, but with Lua it self. lua-users wiki: Floating Point

I am aware of that but I want to know how people deal with these issues. I tried checking if i was larger than 1, and if it was it returned but sadly it did not work.

You can do the loop in normal increments. Loop from 10 to 0 and divide by 10 in the print.

5 Likes

Well I guess I could go with that, is there any simpler way?

Floating point imprecision is a pain. But I honestly think @AMD_chan’s solution is best. Here is what his solution will look like in code (make sure to mark his as the solution, not mine):

for i = 10, 0, -1 do
    print(i/10); -- You could also do i*0.1 if you wanted this loop to be really efficient (as multiplication is way cheaper than division)
end

See, it’s not that bad. Only one more calculation (the division) needed (from an efficiency view). But hey, I honestly think another calculation is way better than having those wrong floating points!

I hope you find your solution! :smile:

I guess that is what I am going to do. It is not like I have a better alternative option.

2 Likes

You’re treading into microoptimisation territory here. There’s nothing complex about dividing by 10 and decrementing by an integer increment. The operation is only a negligible amount of ms longer that you can reasonably ignore.

I am not saying that dividing by 10 is complex but alright.

For curiosity’s sake, would multiplying by 0.1 be more efficient than dividing by 10? Would it have a good boost or just a negligible change? I’ve always learned multiplication is cheaper but I’m wondering how much cheaper it is in practical situations.

Thanks in advance.

Wouldn’t dividing by 10 be the same thing as multiplying by 0.1?

@vamik64

Made an assumption.

Directly addressing the question: no.


@DatOneRandomDude

I personally don’t know because I haven’t bothered to check nor do I actually know (I’m unfamiliar with any technical workings of Lua). You can run a test like this in Studio by checking tick before and after the operation.

The difference it takes to complete the operation is negligible to me and hardly makes a difference. The difference is small enough not to notice it. Therefore, I ignore it.

1 Like

The end result is the same, but multiplication is way “cheaper” (easier for computer to compute) than division. The only exception is division by 2 where a computer can just shift bits down (just like in our division by ten we just shift the numbers down a decimal place). I recommend Googling it if you want to learn more.

I was just wondering if in practical situations it had a difference. Thanks @colbert2677, I’ll test it and I’ll let you know after (if you want).

Well it appears you are right! Just ran a test as @colbert2677 said and it appears multiplying is actually slightly faster.
image
Well I guess I learned something new today. I guess I will try to use multiplicaiton instead of division when I can, even tho using multiplication instead of division won’t have a huge impact on performance.

3 Likes

Nice! Thanks for testing it!

With some situations (like dividing by a variable) you can’t avoid it but, starting from now, wherever I divide by a constant I think I’ll use multiplication instead (except when dividing by numbers like 3 where you deal with infinite decimals).

For anyone in the future watching down on us, to find that constant that can replace your division, you do this simple calculation:

1/(number you dividing by)

So I’ll use the 10 example:

1/10 = 0.1 <- This is the multiplication equivalent.

You would do this optimization while writing your code and optimizing it and not on runtime by the way (it would defeat the purpose of it) :stuck_out_tongue:

Edit: After some further testing and time, I do not recommend doing this. In my testing, division is not always slower than multiplication; sometimes, it even beats it. So in this case, I prefer the readability of division instead of the super small (and sometimes non-existent) speed boost of multiplication.

This only works on the integer number format. In Lua we use doubles. Also, it is only plausible in a compiled language because you must know ahead of time whether to shr or div.

1 Like