for i = 5, 1, -0.2 do print(i) end
I can't wrap my head around it. changing the arguments for the for loop to "5, 1, -1" yields results that end in "1" as the last value of i. However, the aforementioned code's minimum value in the output is 1.2. Can someone explain why this is?
Possibly because 0.2 is a floating point that cannot be represented precisely in binary. Therefore, you're really subtracting 5 with a number that is very very /very/ slightly greater than 0.2. In fact, it's exact value is about:
0.200000000000000011102230246251565404236316680908203125
You'd notice that if you decrement it by 0.5 instead of 0.2, it actually prints out 1 in the end. This is because 0.5 can be precisely represented in memory.