UDim2 Position is not equal to its position

Okay title is confusing but I have a button to slide an inventory screen in. I set the GUI at position 1,0,.527,0 manually. Thats its place on my screen without scripts.

Now in my script I have this conditional statement to detect if its at its starting location:
if GUI.Position == UDim2.new(1,0,.527,0) then

and its not working so i printed its position manually, and even though im setting my gui’s position at 1,0,.527,0 manually, when i print its location from local script its {0.99999994, 0}, {0.527276039, 0}.

Like sorry sir your gui is actually at .99999994 not 1, so its not equal. I didn’t even set it at .99999994. Anyone know whats going on?

Floating point errors. Try multiplying the position by 100-1000 and then rounding it, then dividing it by 1000, then it should give you a normalized UDim2
ex.

local function normalizeUDim2(udim2)
    return UDim2.new(
        math.round(udim2.X.Scale * 1000) / 1000,
        udim2.X.Offset,
        math.round(udim2.Y.Scale * 1000) / 1000,
        udim2.Y.Offset
    )
end
--- ...
if normalizeUDim2(gui.Position) == UDim2.new(1,0,0.527,0) then

Also FYI, even the most basic of floats have floating point errors, the thing is that either Lua or Roblox (not too sure) rounds them most of the time for our convenience, ex:

print(string.format('%.99f', 0.1+0.1))

Outputs 0.200000000000000011102230246251565404236316680908203125000000000000000000000000000000000000000000000
So if it wasn’t rounded, we would have trouble because what we see as the exact same, the computer doesn’t.

For example, 0.1 + 0.2 = 0.15 + 0.15, right?

Wrong, not according to the computer at least:

print(string.format('%.99f', 0.2+0.1) == string.format('%.99f', 0.15+0.15))

Outputs false

In fact, merely declaring any decimal has a floating point error:

print(string.format('%.99f', 0.2))

outputs
0.200000000000000011102230246251565404236316680908203125000000000000000000000000000000000000000000000

1 Like