Issue with .Unit() and very small vectors

wait(4)

local brokenVec3 = Vector3.new(0.000000000000000000000000000000001,0,0)

print(brokenVec3, " : ", brokenVec3.Magnitude) -- prints 1.00000002e-33, 0, 0  :  0
print(brokenVec3 == Vector3.new()) --prints false

if brokenVec3 ~= Vector3.new() then
	game.Players.LocalPlayer:Move(brokenVec3) -- this sets the position to NaN
end

There is a very rare problem with Vector3. Basically, what happens is that very small Vector3 are not defaulted to (0,0,0), but their magnitudes are. This makes it so calling BrokenVector3.Unit on a very small Vector will cause the game to do BrokenVector3 / BrokenVector3.Magnitude which translates to (1.00000002e-33, 0, 0) / 0 which creates a NaN. The solutions to this are two:

First, make it so Vector3.Unit cannot return a NaN, instead, have it default to (0,0,0) or (1,0,0)

Second, make it so very small numbers in vectors are also turned into 0, this way, checking if a very small vectors equals (0,0,0) will return true and prevent you from creating a NaN you did not expect.

I’m sorry if this isn’t the right category, but i don’t have the permissions to post into the Engine Bugs section of the forum. Thank you.

2 Likes

Personally I’ve been posting my platform feedback (only 1 thread so far) to the Bulletin Board section. You should probably move this thread there just to serve as a note to yourself for when they finish their post approval renovation.

1 Like

I might keep this here just so there’s a chance that someone in the future will encounter a similar problem and will be able to fix his game using the information shared here

This is probably due to floating-point precision (correct me if I’m wrong) The Floating-Point Guide - Floating Point Numbers

Exactly, but it seems that the checks in place for single numbers are not there for Vector3 components