For loop acting weird?

A friend and I were messing around with for loops when we realized that if you use increments of 0.01, the for loop never actually reaches its end.

I.E.

for i = 0, 5 do
    print(i)
end

Output for that loop will result in
0
1
2
3
4
5

But if you were to use

for i = 0, 1, 0.01 do
    print(i)
end

The output for this for loop results in printing 0 through 0.99 but never actually reaches 1.

Does anyone have any clue as to why this happens? Is it something to do with Roblox Studio or am I just being dumb about this? (Yes I am aware I can just set it to reach 1.01 if I want to have it print 1 but my concern is with regards to why it happens)

Apparently this is a rounding error from using floating point numbers according to StackOverflow:

That said, try avoiding using floating point numbers in for loops.

2 Likes