If statement saying .9 does not equal .9

Hello, I’m trying to check if these two numbers are the same but it says they aren’t when they really are.

for i = .1,1, 1 /  math.floor(howmanypillars) do
		
		if tonumber(i) == tonumber(.9) then
			print(true, i, .9)	
		else
			print(false, i, .9)	
		end
end

output:
Screen Shot 2021-08-24 at 10.35.36 PM

Screen Shot 2021-08-24 at 10.32.08 PM
Can someone please tell me why this is happening

I think it’s because you are doing tonumber when you don’t have to. The variable in a standard for loop will always be a number.

for i = .1,1, 1 /  math.floor(howmanypillars) do
		
		if i == .9 then
			print(true, i, .9)	
		else
			print(false, i, .9)	
		end
end

Nope, I did that just to make sure that they were both numbers, and when I remove the to number() I get the same result

I see, it’s probably because of floating point errors.

for i = 0.1,1, 0.1 do
	print(0.9 - i)
end
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
1.1102230246252e-16 (aka ~0.00000000000000011102230246252)
-0.1

Since Luau has a math.round function, you could try use that, just multiply then divide by whatever you want the precision to be (100 is hundredth for example).

print(math.round(i * 100) / 100 == 0.9)
2 Likes

When I did:

if i*10 == .9*10 then
			print(true, i*10, .9*10)	
		else
			print(false, i*10, .9*10)	
		end

it worked perfectly

1 Like