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…
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.
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?
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.
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)
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.
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.