Modulus (%) not working properly in loops

Hello, I recently encountered a really strange problem (possibly a bug). It’s where if you were to check if for example X % 1 == 0, it wouldn’t work properly. Better example:
let’s say we have a script with a ‘for loop’ and want to check if ‘i’ % 1 == 0.

for i = 0, 5, .1 do
	if i % 1 == 0 then
		print(i, true)
	else
		print(i, false)
	end
end

This would be the output:

0 true
0.1 false
0.2 false
0.3 false
0.4 false
0.5 false
0.6 false
0.7 false
0.8 false
0.9 false
1 false
1.1 false
1.2 false
1.3 false
1.4 false
1.5 false
1.6 false
1.7 false
1.8 false
1.9 false
2 false
2.1 false
2.2 false
2.3 false
2.4 false
2.5 false
2.6 false
2.7 false
2.8 false
2.9 false
3 false
3.1 false
3.2 false
3.3 false
3.4 false
3.5 false
3.6 false
3.7 false
3.8 false
3.9 false
4 false
4.1 false
4.2 false
4.3 false
4.4 false
4.5 false
4.6 false
4.7 false
4.8 false
4.9 false
5 false

But why is 1,2,3,4, and 5 not true? If I run this command in the command line:

print(3 % 1 == 0)

It says 3 % 1 is true.

When I run this script:

for i = 0, 5, .5 do
	if i % 1 == 0 then
		print(i, true)
	else
		print(i, false)
	end
end

It does work properly:

0 true
0.5 false
1 true
1.5 false
2 true
2.5 false
3 true
3.5 false
4 true
4.5 false
5 true

Why is it like that? And is there an alternative way of doing it?
I need .1 for a for loop’s increment in particular.

1 Like

Your issue is likely floating point imprecision with smaller numbers, so I recommend something like this.

for i = 0, 50 do
    print((i/10)%1 == 0)
end

which gives better results

I see. Thank you for helping! I wish my way was possible.