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:
Can someone please tell me why this is happening
7z99
(cody)
August 25, 2021, 2:38am
#2
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
7z99
(cody)
August 25, 2021, 2:45am
#4
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