I can see where you’re coming from, but its something you have to deal with as a developer.
What happens here is the floating-point error. Super interesting subject in mathematics and computer science. Long story short computers cant store the exact value of some decimal numbers.
As someone stated already you’ll have to use .format when working with float values.
Unfortunately, these anomalies are ‘normal’ when it comes to floating-point and you will have to live with them.
You can use my ‘Round’ function described in this topic, to avoid this problem.
Floating point errors were mentioned in a fairly recent Announcement about how Roblox was handling numbers.
I can’t remember which post it was, but maybe try searching Floating point error and search in the Announcements forum.
Since nobody ever mentions it, this is why it happens. In our number system, base 10, what is the answer to 1/3?
The answer is 0.3333333333 repeating infinitely. We don’t have infinite space on the paper, so let’s round it off at 5 decimal places.
0.33333
Multiply by 3 again and you get 0.99999. it’s not 1.
This is a similar rounding error that exists in our every day base 10 number system.
What’s more is that 1/3*2 and 2/3 are different numbers.
(1/3 = 0.33333) * 2 = 0.66666
2/3 = 0.66667
In binary or base 2, this type of error caused by numbers repeating infinitely is a lot more common. Like tape measures in Inches, binary numbers work best when dividing by 2. Halves, quarters, eighths, sixteenths, thirty-seconds, sixty-fourths, etc. If your number can’t be represented with these inverse powers of two, it must get rounded.
I think that’s the simplest way to do it, thank you, but I hope Roblox staff can handle it for us. We can just focus on game development and not focus on the details
so mathematically on a base 10 number system youre right. Computers use binary. This is the product of that. That value is what value the computer is using. It shouldnt matter what a human thinks it should be, what matters is what the value ACTUALLY is for the machine.
function Round(Number, Digits)
local Num = Number * 10 ^ Digits
Num = Num>=0 and math.floor(Num+0.5) or math.ceil(Num-0.5)
Num = Num / (10 ^ Digits)
return Num
end
This method comes from @rogeriodec_games, Should be built into Roblox as a friendly game engine
I really don’t think you understand how the floating point error happens and why it happens as a byproduct. Do some research on it and you will understand why they can’t just “fix” it.
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 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
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
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.