# Category: Trigonometry

# Arccosine versus Arctangent for Vectors

In my previous programming years, when determining the angle given three right sides of a right triangle, I always use arctangent (`Math.Atan`

or `Math.Atan2`

in .NET). Arctangent is always available for me especially when the length of the hypotenuse of a right triangle is not given.

Until I came across with vectors.

Everyone knows that the output angle of the arctangent ranges between `-PI/2`

and `PI/2`

. However, It cannot be equal to either `-PI/2`

or `PI/2`

. Just looking at the graph of arctangent would be clear enough.

In linear algebra, say for example, two vectors are given, both from the origin but at different directions. In determining the angle between two vectors, many would think it will be

**Angle = Arctangent (Cross Product of two vectors / Dot product of two vectors)**

However, when these two vectors are perpendicular to each other, this is the point that the arctangent will fail. Why is that so? Getting the dot product of two perpendicular vectors will eventually lead to a value of zero, which will fail when used to divide a number.

With this in mind, many would think we have to set first a condition whether the angle will be `-PI/2`

or `PI/2`

when the computed dot product of these two vectors is zero. Then, we can determine if the angle is `-PI/2`

or `PI/2`

depending of the sign returned by the cross product of two vectors.

In addition to a lengthy code that will result with these conditions, we may sometimes want to determine the angle of two vectors when these vectors product an obtuse triangle, which is impossible using arctangent.

To comply with the above statement, we can use the formula below.

**Angle = Arccosine (Dot product of two vectors / Product of lengths of two vectors)**

Although there is a division involved here, vectors will always have lengths so producing a zero divisor will never happen, and so no failure in division. Also, the result of the arccosine is ranging from `0`

and `PI`

, so you won’t need to make additional painstaking work just to come up with an angle greater than or equal to `PI/2`

using arctangents.

*Tip: To provide an angle that is greater than PI (or less than -PI), use the cross product and check whether the vector produced by the cross product is pointing upward or downward normal to the plane. Then adjusting the angle by this condition can be straightforward.*