1.2 - 1 = 1.999999999996?


Why this result instead of 0.2 ? This caused a bug in my game. It’s surprising! Should we write math.round() for each operation of this type ?

4 Likes

try using 1,2 with comma not dot

2 Likes

if you use math.round() it’ll give you 0

1 Like

I think the issue here is that he wants the answer to be 0.2 not 0. Using math.round will not help

1 Like

yea ik what he’s trying to do im trying to fix maybe he should use returns

1 Like

try this:

I = 1.2
x = 1
return
	print(0.2)
1 Like

just computers being dumb

local Round = function(x)
    return (math.round(x*100)/100)    
end

print(Round(1.2 - 1))
2 Likes

One way to fix such an error is to just round to the nearest decimal.

2 Likes

I correct it with math.round(result*100)/100.
This reminds me of this math problem. 1 = 1/3 + 1/3 + 1/3 = 0.33333… + 0.33333… + 0.33333… = 0.9999999…
I didn’t think I’d see this on Roblox!
Thanks for the help.

It’s honestly on all coding languages, it’s just how computers work… a bit stupidly but still fascinating.

Glad I could help though!

1 Like

Little correction: It is similar but it’s actually two different things. The reason why this happens is because you cannot make an infinitely long number into an finite amount of memory, for example 1/3 which is 0.33333333… but you can’t write out all of it, and that’s basically the reason, so computers are designed to approximately get the answer.

Plus that computers aren’t very good with floating points since they are in binary. They cannot represent all base 10 float in base 2 (only a few exceptions, like 0.25).


Also, to summarize and answer the reason why 1.2 - 1 becomes 0.2, is because the binary would look like this

0.00110011001100...

Again, since computers do not have an infinite amount of memory, they are going to set the limit to let’s say a random arbitrary number like 30 floating point far. Unfortunately this is still not enough and it’s going to cause like a very tiny difference like 4 to the power of 10^10.

1 Like

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