Lua for loop does not do all iterations

I am new to lua and I use it to automate some tasks in the female simulation model. In my script, I have this type of for loop:

for i=0.1,0.3,0.1
do
  print(i)
end

The problem is only that it runs only from 0.1 to 0.2 (it is not included in i = 0.3). I tried to use other values ​​(for example, from 0.1 to 0.4) and it works correctly. Why is this strange behavior happening? Is this a floating point problem?

+4
source share
1 answer

This is due to the fact that adding 0.1-1.1 three times leads to a number that is slightly larger than 0.3. Therefore, the cycle stops until it reaches the final end number.

. , :

for j = 1,3
do
    i = j/10
    print(i)
end

+3

Source: https://habr.com/ru/post/1685450/


All Articles