Math addition(+) looks weird

I’ve been trying to play with the math function and I found something strange.


https://gyazo.com/38b125d10e3c0ebb26aa3728339f4667
this looks good
so basically you add 1 + 0.7 + 0.09 which makes it 1.79
Most of the time, it does work very well.

However,

https://gyazo.com/d34da32a5255406001b8ddc0c4ddca2f

2 + 0.8 + 0.01 = ???
This seemed weird so I thought I’d post this.

Are there any ways to make it 2.8 or 2.81??

this is my code:

				local randomnum = math.random(1, 100)
				local randomsecond = math.random(9) / 10
				local randomthird = math.random(9) / 100

Thats floating point imprecision, something about how computers handle numbers with decimals

These accuracies are more visible with decimal numbers since the entire float cannot replicate the number on the binary. This applies especially to non-integers. Without it, the machine will try to approximate as close to the number that can be done on the binary sequence.

1 Like