开发者

How to interpret situations where Math.Acos() reports invalid input?

开发者 https://www.devze.com 2022-12-27 13:35 出处:网络
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, exactl

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;
        }
    }


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.


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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号