Help with floating point errors

Basically, I have a chance variable which decreases by a certain amount every few seconds.
This works fine, except for once in a while it has an error with a bunch of decimals making the number very ugly. eg. 30% becomes 30.0000000002% or something.
I know there is no simple way to stop this, but is there a way I can display this number to other players without it looking ugly? (i can’t just round it to 0 decimals because then when i have low percentages such as 0.003% it will round to 0.)

Sorry if this is worded badly, I’m not great at explaining. Thanks for any help!

2 Likes

you could always the number multipliy it by how many decimals you want saved, then use math.round() on it, and divide back down to the original number.

local num = 30.00002
local anothernum = 3.1415926

print(math.round(num*10)/10) -- prints 30.0

print(math.round(anothernum*100)/100) -- prints 3.14
1 Like

There is a simple way to stop it.
Lots of posts have been done that mention floating point error.
Use math.round() to get rid of decimals.
Either that or use an intvalue.

Show us your code so we know what you’re working with.

So if I understand correctly, the goal is to round the numbers in the following pattern:

nn.nnn → e.g. 42.025

Floating point errors are very small errors raising the number of decimals, so the idea is to round correctly when they occur.

0.003 → 0.003
0.005000001 → 0.005

Here’s my approach with string patterns. 1. Omit to three decimal places without rounding. 2. Remove the trailing zeros if they exist.

local function FormatChance(chance: number): string
	local formatted = string.format("%.3f", chance) --> omit to three decimals
	formatted = formatted:gsub("%.?0+$", "") --> remove the dot and trailing zeroes
	return formatted
end

print(FormatChance(5.00000001)) --> 5
print(FormatChance(0.00500091)) --> 0.05
print(FormatChance(10)) --> 10
print(FormatChance(29.994000094)) --> 29.994

% - escape character; %. signifies the dot
? - modifier; either 0 or 1 occurances
0+ - antoher modifier; find 1 or more repetition of 0
$ - end of the string

6 Likes

This also happened to us with the math.floor function and the string.format(“%.3f”, num), the math library itself is unreliable.

The only solution we found we can trust was using the integral part of the modulo function even though it is from the math library.

local int, _ = math.modf(value)

Thank you so much! This is exactly what I am looking for!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.