大家好。我正在计算两个向量之间的角度,有时 Math.Acos() 在余弦的输入超出范围(-1 > 输入 && 输入 > 1)时返回 NaN。这到底是什么意思?有人能解释发生了什么吗?任何帮助表示赞赏!
这是我的方法:
public double AngleBetween(vector b)
{
var dotProd = this.Dot(b);
var lenProd = this.Len*b.Len;
var divOperation = dotProd/lenProd;
// http://msdn.microsoft.com/en-us/library/system.math.acos.aspx
return Math.Acos(divOperation) * (180.0 / Math.PI);
}
这是我对Dot
and的实现Len
:
public double Dot(vector b)
{
// x's and y's are lattitudes and longitudes (respectively)
return ( this.From.x*b.From.x + this.From.y*b.From.y);
}
public double Len{
get
{
// geo is of type SqlGeography (MS SQL 2008 Spatial Type) with an SRID of 4326
return geo.STLength().Value;
}
}