Does anybody know why it prints the values in the Vector3 not roundded corretly ?
I mean 1.09999…
insted of 1.01,1.01,1.01 ?
Is this a glitch or not can somebody help ?
I think this is a “floating point” error.
The numbers will be slightly different, but you shouldnt notice a difference if you use the vector for position or size
I’ve also been having this problems with decimals not being rounded. I think it might be a calculation error on roblox’s end. I did, however, figure out how to fix this. In your case, if you want the number 1.01 for a variable, set the variable to 101/100.
No It’s just example of the error, It breaks more complexed script that I made.
Also It doesn’t work
the difference is below 0.0000001, how in the world does that break anything?
Believe it or not, this is ‘intended’ behavior. Luau numbers use a double type which aren’t always 100% accurate due to how they work under the hood. You can see other instances of these inaccuracies in situations like (0.1 + 0.2) == 0.3
which produces false
despite technically being true.
Let me introduce you to the concept of floating-point imprecision:
You see, computers store information in bits which is represented as a binary digit (either 0 or 1). Working with integers like 67 are just fine since these can be accurately represented in binary, but what about decimals? Looking at a simple case of using 0.1, the binary representation of it is 0.000110011 where the 0011 digits at the end are infinitely repeated. We can’t possibly represent this in binary accurately because this would require infinite amounts of space, so this is where the need for approximation comes in
Computers are able to represent it accurately enough to the point where the difference is negligible, but doing something as simple as 0.3 + 0.2 == 0.5
will evaluate to false because all of the numbers are slightly off
So this is what is called floating-point errors or imprecision, which can’t be prevented, but can be mitigated. Take a simple case where you have a while loop incrementing a value and desire for it to be stopped once it reaches a certain decimal. A naive approach would be to use the comparison operator:
local val = 0
while true do
if val == 7.5 then
break
end
val += 0.5
task.wait(0.5)
end
At a point in time, you may think that val
should be at 7.5 and the loop should terminate, but it would be virtually impossible in the reality of computing. val and 7.5 may be slightly off, like 7.4999994324
, so the loop will never stop
You can use the greater than operator instead:
while true do
if val >= 7.5 then break end
val += 0.5
task.wait(0.5)
end
Thanks for telling me about this. Never hear of it before.
This is factually incorrect.
Roblox Luau VM number type uses IEEE binary64. I emphasise the term binary because you are implying that addition by 0.5 can cause floating-point errors despite 0.5 being 1 * 2-1 and its binary representation is 0.1
.
Finite values represented in the IEEE 754 double precision floating point format should be able to do arithmetic with 2k without error albeit it rounds them when it has too many significant binary digits.
Correct. Thanks for pointing that out. Perhaps it would’ve been fine if I had used as an increment of 0.2 and a target of 7.4 as this seems to achieve the problem stated
local val = 0
while true do
if val == 7.4 then break end
val += 0.2
task.wait(.5)
end