Value being set to 1.3877787807814e-16 instead of 0

I am trying to create a button that you press and has a changeable cooldown depending on modifiers. When this finishes running and sets the value to 0, it actually sets it to 1.3877787807814e-16 and then -0.1.

local cooldown = game.Players.LocalPlayer.values.cooldownTime
local newcooldown = cooldown.Value
while newcooldown > 0 do
	local one = 1
	newcooldown = newcooldown - 0.1
	script.Parent.Text = one - 0.1 --this ends up having the same thing happen
	print(newcooldown)
	wait(0.1)
end

image

I’ve been looking for anyone else that’s been having the same problem but I found nothing on this. I can’t figure out what’s going on that causes this.

1.3877787807814e-16 is actually a really small decimal number (with 16 zeroes!)
0.00000000000000013877787807814
So its really close to 0, nothing to worry about.

Luau’s floating point storage often can cause miniscule errors which is to be expected.

Basically you shouldnt have any issue, with the code you have.

If you are having an issue, consider taking your value and multiplying it by a set accuracy, then rounding that number, and then dividing by that set accuracy again, this should fix most cases of these errors.

local function FloatingPointFixer(Number)
   return math.round(Number*100)/100 --100 is 0.01 accuracy which is enough for your usage if you require it to be semi accurate.
end

4 Likes

That value is actually 0.0000000000000013877787807814.
You could use math.round(newcooldown) to print the integer value (no decimals) of newcooldown.
edit lol @Artoshia , beat me to it!

1 Like