Arcane.Artist's picture

Vector3.CalculateAngle() may return NaN

Project:The Open Toolkit library
Version:1.0-2010-10-06
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:open
Description
// source Vector3.cs
public static float CalculateAngle(Vector3 first, Vector3 second)
{
return (float)System.Math.Acos((Vector3.Dot(first, second)) / (first.Length * second.Length));
}

I've isolated my problem down to Vector3.CalculateAngle. If the differences between the value are small enough, Vector3.Dot(first, second) / (first.Length * second.Length) can result in a value greater then 1.0, causing Acos to return NaN. Can easily be fixed with below.

public static float CalculateAngle(Vector3 first, Vector3 second)
{
return (float)System.Math.Acos(Math.Min((Vector3.Dot(first, second)) / (first.Length * second.Length), 1.0f));
}