Problems With Adding Numbers

Im working on something that changes time by adding .01 to the previous time and found out that the addition is not working. Anyone know what I’m doing wrong if anything? It should work because it is just basic addition but it is adding a random tiny decimal to the end of the number instead of just .01

Here is a video of the issue.

local Time = 14.5
while wait() do
	Time += .01
	print(Time)
end

Also it still does the same issue if i change the code to round to two decimals like this:

local Time = 14.5
while wait() do
	Time += (math.round(.01*100)/100)
	print(Time)
end
1 Like

I did find a slight solution to this but it most likely wont hold up on the long term.

local Time = 14.5
while wait() do
	AddTime = (math.round(0.01*100)/100)
	Time = (math.round(Time*100)/100) + (math.round(AddTime*100)/100)
	Time = (math.round(Time*100)/100)
	print(Time)
end

This is a common problem known as floating point imprecision. Without implementing a rounding technique, adding decimals together will output like that, because the computer cannot precisely store it in memory.

If you want to restrain the number of decimal places you can use format specifiers:

local decimal = 1.495930

print(string.format("%.2f", decimal))
3 Likes

That does the same thing as the thing i put. also it does not completely fix the issue.
for it to work i still have to do:

local Time = 14.5
while wait() do
	Time += string.format("%.2f", (0.01))	
	Time = string.format("%.2f", Time)
	print(Time)
end

this serves the same purpose as this code:

local Time = 14.5
while wait() do
	AddTime = (math.round(0.01*100)/100)
	Time = (math.round(Time*100)/100) + (math.round(AddTime*100)/100)
	Time = (math.round(Time*100)/100)
	print(Time)
end

except the one i made you change the number of zeros to change the decimal. (number of zeros = number decimal places)
also just putting just the first line in the loop wont work in either, it needs both lines to work.

Rounding or fomatting 0.1 isn’t necessary. Sharpie simply reduced the decimal places of the added number because it can be affected by the floating point error.

local formattedTime = 14.5
local flooredTime = 14.5

while true do
	formattedTime = tonumber(string.format("%.2f", formattedTime +.01))
	flooredTime = math.floor((flooredTime +.01) *100)/100
	
	print(flooredTime == formattedTime)
	task.wait()
end

We don’t know exactly what the purpose of your script is. Keep in mind that this is not a reliable real-time counter.

yes, i know, and even after i reduced the decimal places the problem continued to persist. it only goes away after i reduce the decimal places of both the number added and the result number. also Im using this for changing the time in the world because Im making a space game and wanted the time to change smoothly between day and night when you move through a planet’s atmosphere.

	if number < 1 then
					local thing = number
					while thing < 1 do
						thing = thing*10
						f = f + 1
					end
				end

local Kills = string.format("%.".. f.."f", number)

this is some code I just wrote to fix the exact some problem that you have. what it pretty much does is it checks how large the number is and round to the first decimal place that isn’t a 0. Hope it helps

other wise if the number is larger then 1 and you have a specific point you want to round to like the tenth place “0.1” then you can just do local variable = string.format(“%.1f”, number) and if you want it be be something else then replace the 1 with what ever decimal place you want it to round to.

1 Like