如何解释 Math.Acos() 报告无效输入的情况?

发布于 2024-08-29 12:08:32 字数 918 浏览 8 评论 0原文

嘿大家。我正在计算两个向量之间的角度,有时当 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);
    }

这是我对 DotLen 的实现:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久光 2024-09-05 12:08:32

您的向量 divOperation 结果是 divOperation divOperation divOperation divOperation -1或> 1?那么我认为你应该检查你的 DotLen 的实现。

You have vectors for which divOperation turns out to be < -1 or > 1? Then I think you should check your implementations of Dot and Len.

霊感 2024-09-05 12:08:32

由于角度的 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.

懵少女 2024-09-05 12:08:32

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文