Imprecise Decimals for Audio Volume

Adding or subtracting 0.1 from an original value of 0.5 leaves a decimal remainder. My code is attempting to rotate a nifty dial with the addition or subtraction of sound volume, yet I’ve ran into a seemingly impossible situation.

Not much to expand on, if I’m being honest.

જ⁀➴

plus.MouseButton1Click:Connect(function()
	
	if songSelection.Volume < 1 then
		print(songSelection.Volume)

		songSelection.Volume = songSelection.Volume + 0.10
		dial.Rotation = dial.Rotation + 28
	end
		
end)

minus.MouseButton1Click:Connect(function()
	
	if songSelection.Volume > 0 then
		print(songSelection.Volume)
		
		songSelection.Volume = songSelection.Volume - 0.10
		dial.Rotation = dial.Rotation - 28
	end
	
end)

જ⁀➴
Post Scriptum
Once implementing math.clamp(), the code works wonders. Still curious though, unless it is only a flaw in the system.

౨ৎ
With appreciation,
vamp

1 Like

This is due to how Lua/Luau stores numbers in memory and there is not much you can do about it.

Lua/Luau uses IEEE 754 double-precision binary floating-point format to store decimals and integers in memory. However, these numbers can’t just have arbitrary precision and results of operations are usually just approximations because of that.

If you were to put numbers like 0.1 or 0.2 to an online decimal to floating-Point converter you will see that they are not stored as they are in memory but as 0.1000000000000000055511151231257827021181583404541015625 and 0.200000000000000011102230246251565404236316680908203125.

2 Likes

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