Dealing with the Jungle that is Floating Point Math

Hello! I’m really confused by the intricacies of floating point math. I’ve discovered that because of the way floating point math works, 9.7 + 0.1 isn’t 9.8, it’s 9.699999999999999… I have no idea how it gets to that conclusion, but honestly, I just want the math to work right lol. All I’m doing is a simple addition and assigning to a value. This wasn’t a problem until I started adding a check for when the value is equal to 10, which it never is because of this floating point math. The closest it will get is 9.99999999999999…

	Ring.CL.Value += 0.1
	UpdateRingUIEvent:FireClient(player, Ring.CL.Value)

Any help would be appreciated:

You should try to use math.ceil/floor or round while adding number. ^^
I don’t remember which one, but i know there is a math thing to get the right decimal number.

1 Like

The only problem with this is that when I’m adding to, let’s say, 9.7, I’m expecting 9.8. math.ceil and math.floor would return 10 or 9.

I suppose I could just write a function to round the number to the nearest tenth every time the script runs, but… ugh, that’s tedious. Is there really no better solution?

You could have an if statement for magnitude

if (number-desiredNumber).Magnitude < 0.001 then 
    --stuff 
end

After a bit of rechearching, i found someone that encountered the same problem and it got solved, maybe you can try his solution ?

Ring.CL:GetPropertieSignalChanged("Value"):Connect(function()
   Ring.CL.Value = math.round(Ring.CL.Value / 0.1) * 0.1
end)

Maybe something like this ?

2 Likes

Decimals just be like that

math.ceil((9.7 + .1) *10)/10 -- 9.8 if im correct
1 Like

Yeah, so this is about what I said. I could write a function to round the number to one decimal place each time. It just kinda sucks that I have to do that in the first place. I haven’t had any issues with floating point math spitting out weird numbers until just now. Like, before I added the == 10 check, I had no issues at all. Kinda annoying.

I went ahead and wrote a quick rounding function:

function roundNumber(num, numDecimalPlaces)
	return tonumber(string.format("%." .. numDecimalPlaces .. "f", num))
end

And this solves the problem, for the most part.

thats not rounding, thats just applying a decimal to the number.

1 Like

Well, floating point numbers are designed to have high range, not necessarily high precision, so it’s not uncommon to get an irrational result to a rational equation… (Sorry if this made no sense, I’m bad at explaining things)

Here is a source to leaen more about this problem: 0.1 + 0.2 is NOT 0.3 in Most Programming Languages - YouTube It’s based around javascript but this issue pretty much works the same in almost every single programming langauge.

Anyway, to solve your problem, you need to look at the issue differently. Why do you need to compare these numbers?

If you want to check if a number reaches a little such as a loop that increments a number by some small fraction to 10, use <= instead as overshoots will yield in a true value.

If you want to display it to the user in a pretty format, you can use math.round. If you need decimal points you can add those too:

local yourNumberHere = 0.262 * 0.746
local result = math.round(yourNumberHere * 100) / 100 -- Not sure if this works
print(result)

It all really depends on your use-case and don’t be afraid to ask.

1 Like

Floating points are interesting! There isn’t really a question to answer here so general advice is
do not == floats.

What are floats?

Like all numbers in computing they are represented in binary, just like how integers have 32 bits so do floats. With 32 bits we can represent 4.9 billion different numbers, integers choose to represent 0 through 4.9 billion in a simple fashion. Floats use their 4.9 billion to represent a wider range of numbers from 0 to 1e203!

They do this by distributing numbers unevenly and in fact 1.3 billion (25%) of the numbers a float can represent are between 0 and 1. If we plot every number a float can represent we get a chart that looks like this, with a bunch of points at the center and wider and wider marks as we get away from 0.

A good long video on how floating points work, their downfall in videos game can be explained in this video using super mario 64:

3 Likes

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