如何解释 Math.Acos() 报告无效输入的情况?
嘿大家。我正在计算两个向量之间的角度,有时当 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
和 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;
}
}
Hey all. I'm computing the angle between two vectors, and sometimes Math.Acos() returns NaN when it's input is out of bounds (-1 > input && input > 1) for a cosine. What does that mean, exactly? Would someone be able to explain what's happening? Any help is appreciated!
Here's my method:
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);
}
Here's my implementation of 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的向量
divOperation
结果是divOperation
divOperation
divOperation
divOperation
-1或> 1?那么我认为你应该检查你的Dot
和Len
的实现。You have vectors for which
divOperation
turns out to be < -1 or > 1? Then I think you should check your implementations ofDot
andLen
.由于角度的 Cos 始终在 -1 和 +1 之间,因此无法计算该范围之外的值的反函数 (Acos),或者这意味着您将 NaN 传递给了 ACos 函数。
我怀疑在这种情况下是后者 - 你的长度之一可能为零。
Since the Cos of an angle is always between -1 and +1 there is no way to compute the inverse function (Acos) of a value outside that range OR it means you passed NaN to the ACos function.
I suspect in this case it's the latter - one of your lengths is probably zero.
NaN 表示“不是数字”。从数学上讲,你不能取 [-1, 1] 范围之外的数字的反余弦(或者也许你可以,但结果很复杂——我不记得了)所以尝试这样做的结果是根本没有任何数字。
NaN means "not a number". Mathematically, you can't take the arccosine of a number that is outside the range [-1, 1] (or maybe you can but the result is complex -- I don't remember) so the result of trying to do that is not any number at all.