(Disregard) The unit of a blank Vector3 is NAN, NAN, NAN?

I’m not sure if this is intended behavior. If it is, it doesn’t seem like it should be.
If you read the unit value of a Vector3 whose axis values are all 0, it gives you a NAN Vector3.

> print(Vector3.new().unit)
NAN, NAN, NAN

I was trying to write a function that returns the Velocity of the character’s HumanoidRootPart along the XZ axis, with a maximum speed of 16. When the character stood still, it would return NAN values for everything.

local function getPlayerMotion()
	local velocity = root.Velocity * Vector3.new(1,0,1)
	if velocity == blankV3 then -- Causes a NAN error.
		return blankV3 
	else
		local unit = velocity.unit
		local speed = min(16,velocity.magnitude)
		return unit * speed
	end
end

Na Na Na
Ba Na Na

9 Likes

What do you think the behaviour should be? The unit vector of (0, 0, 0) is undefined because its magnitude can not be scaled to one unit.

3 Likes

It’s exactly what it’s supposed to be? It’s just the result of dividing zero by zero, which is undefined.

(0, 0, 0) / |(0, 0, 0)| == (0/0, 0/0, 0/0) == (NaN, NaN, NaN)

From a logical standpoint, I can understand why it returns what it does, but I don’t think anything useful can come of using a NAN Vector3. It causes parts to disappear entirely, and I think it would make sense if there was a safe-guard in place for this specific instance.

If this isn’t a good enough reason, so be it, but it seems silly that we have to implement safe guards for behavior that isn’t desirable or usable.

2 Likes

What else could it be, though? A unit vector has to have magnitude 1, so returning <0, 0, 0> would be incorrect, and if it did return that, then whenever you accessed .unit you’d need to make sure it has a magnitude of 1, which would also be a silly safeguard.

The underlying issue here is that the zero vector has no direction, so “the same direction as the original but a magnitude of 1” doesn’t make any sense.

Ah, that makes sense.
Well, as annoying as it is, I guess I can’t fight logic.
I just wish Roblox would provide us with built-in API to combat NAN errors.

2 Likes

Looks like this is a job for…NAN-Man!

I’ll…i’ll just go…

4 Likes

How about simply avoiding divide by zero?

3 Likes

I… can’t argue with that. ಠ_ಠ

2 Likes

ell oh ell

2 Likes

Comparing NaN against anything always returns false.

x = x==x and x or 0

But really, it’s easier to just check if the vector is zeroed first.

1 Like

Yup, I get the idea now.
I didn’t consider that a unit vector with a length of zero does not have a definable direction.

u = (v.magnitude > 0) and v.unit or Vector3.new(0,1,0) -- some default

Something like that should work if your computation always needs a defined vector. Default vector can be chosen depending on what you’re doing.

i.e. in your example you can set it to the last valid unit vector that you encountered.

1 Like

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