Floating point number arithmetic Bug

Floating point number arithmetic Bug

Hello, I was working with decimal calculations in scripting, and I encountered a strange issue. For example, 40.2 turned into 40.3000…4. Is this a bug?

This bug causes inaccuracies. Does anyone know about this issue?

Help me…

This is really frustrating for me.

But what is your main goal in here, is it round the number cus how did it go to 4.3… can you show us the script?

The code for adding is:

local Acc = script.Parent.Parent.Parent.Parent.Acc

script.Parent.MouseButton1Click:Connect(function()
	if Acc.Value == 1 then
		
	else
		Acc.Value += 0.1
	end
end)

and the code for subtracting is:

local Acc = script.Parent.Parent.Parent.Parent.Acc

script.Parent.MouseButton1Click:Connect(function()
	if Acc.Value == 0 then

	else
		Acc.Value -= 0.1
	end
end)

can you print the Value to make sure it only has decimals?

The initial value is 40. However, each time those buttons are pressed, the value increases or decreases by 0.1.
image
It keeps adding, and then an unknown bug occurs.

so, Im assuming your Value is a NumberValue, well computers rlly have a strange behavior sometimes, such as that thing.


local Acc = script.Parent.Parent.Parent.Parent.Acc

script.Parent.MouseButton1Click:Connect(function()
	if Acc.Value == 1 then
		
	else
		Acc.Value += 0.1
        Acc.Value = tonumber(string.format("%.1f", Acc.Value))
	end
end)

maybe try this, it rounds the number to decimals since its a float

Thank you so much! The bug has been fixed!!"

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