Lua for loop does not do all iterations -


i new lua, , using automate tasks in simulation program femm. in script have type of loop:

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

the problem iterates 0.1 0.2(it not enter i=0.3).i tried other values (for example 0.1 0.4) , works properly. why strange behaviour happen? floating point number problem?

this happens because adding 0.1 0.1 3 times produces number greater 0.3. hence loop stops before reaching target end number.

this danger of using floating point values loop iteration. rewrite loop in integers instead, , perform division required number:

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

demo.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -