For loop bug printing wrong number?

Running this for loop makes it print 0.0099999999999997 followed by a huge negative number at the final step when it’s clearly supposed to print 0.01 followed by 0. Is this a bug or am I doing something wrong?

local yVal = 0
local lastY	

for i = 1,100 do
	if i <= 50 then
		yVal = i/100
		lastY = yVal
	else
		lastY = lastY - 0.01
		yVal = lastY
	end
	print(yVal)
end

That isn’t a huge negative number. It’s -3 / 10^16. That is a very small negative number (about 0.0099999999999997 - 0.01)

floats do this. do lastY=math.floor(lastY*100-1+.5)/100 instead of lastY=lastY-0.01 to get rid of float problems.

1 Like

Yeah, I know, guess I just described it incorrectly. (it’s hugely small?)
On topic, you saved my bacon. Thanks!

“For loop bug printing wrong number?”

It took me 6 hours to come up with this title for you. You’re welcome.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.