0.1 + 2.8 ~= 1.1 + 1.8

You’re right, I don’t really know the deep reasons, because it’s boring to study it,I just want to make games with intuitive functions

1 Like

Your method is more concise, I decided to use your method in my game, thank you :grin:

1 Like


This is not just a double * double problem, it’s also a double * int problem.Sometimes, if we don’t pay attention to these details, it can have a big impact on our if-else logic

I would’ve done string.format('%.3f', a)

You can control the precision, plus you might be using stuff compiled from native C to perform the calculations.

I think the method you mentioned is really used a lot, and maybe the Roblox official want us to use this method to solve this problem, but I’m concerned about the performance, if I need real-time high-frequency calculation, for example, I’m shooting a monster, and I’m converting to string and then to double every time, Whether this performance cost is superfluous in the case of multiplayer online

1 Like

I have tested the string.format performance loss to be more than ten times the answer I adopted

local t = tick()
for i=1, 9999 do
	if tonumber(string.format('%.3f',0.1 + 0.2))==0.3 then
		
	end
end
print(tick() - t)

local t = tick()
for i=1, 9999 do
	if math.round((0.1+0.2)*1000)/1000==0.3 then
		
	end
end
print(tick() - t)

Above is the code I tested, and below is the output

When I tested your solution in studio, there was no change. The function float didn’t work

Oh! I was thinking you were trying to print the string represenation of the number and not much else. In that case, I would recommend changing 1000 to 1e3, or using math.abs(0.1+0.2-0.3)<1e-3

I ran this snippet:

local t = tick()
for i=1, 9999 do
	if math.round((0.1+0.2)*1000)/1000==0.3 then
		
	end
end
print(tick() - t)

local t = tick()
for i=1, 9999 do
	if math.abs(0.1+0.2-0.3)<1/1000 then
		
	end
end
print(tick() - t)
0.00030851364135742190 VS 0.00023102760314941406 
0.00030684471130371094 VS 0.00017261505126953125

Performance difference is not negligible, and I recommend using the latter as you’re including epsilon only once.

This method has been established as the de-facto for professional programmers everywhere.

But I’ll go with the first function because I like functions that are straightforward and I’m afraid I’ll get a headache reading my own code after a long time. :rofl: But thank you for your answer. I think someone else could use it.

1 Like