When I add 0.09 and 0.01, I get the wrong result. What is the reason for this? Using math.round is not the solution.
It’s a normal floating point error that is quite inevitable part of storing numbers digitally.
If you need the number displayed, you could round or transform it into a string and omit decimal places.
Edit.
@ZEDDxR sorry, wasn’t racing or anything lol, just had the whole editor window extended and didn’t see you replying too.
That is a floating point error, since decimal values don’t necessarily have an exact binary reference, it sometimes results in loss of precision.
@icy_oasis beat me to it.
As others mentioned, this is a floating-point error. A quick fix for it is the following code:
local function round(x: number, y: number?): number
local y = y or 10
local mul = 10^y
return math.round(x*mul)/mul
end
print(0.09+0.01) --0.09999999999999999
print(round(0.09+0.01)) --0.1
It’s also worth noting that for most calculations it’s best if you don’t round up numbers and keep the precision instead(for example if you’re making physics calculations). However the above is useful for certain use cases like showing visually appealing text to the user or reducing the amount of bytes needed to store numbers in a datastore.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.