# How to find angle between two vectors?

**URL:** <https://discourse.julialang.org/t/how-to-find-angle-between-two-vectors/68149>\
**Category:** General Usage\
**Tags:** linearalgebra\
**Created:** [September 14, 2021, 1:37pm UTC](https://discourse.julialang.org/t/how-to-find-angle-between-two-vectors/68149 "2021-09-14T13:37:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [September 14, 2021, 1:37pm UTC](https://discourse.julialang.org/t/how-to-find-angle-between-two-vectors/68149/1 "2021-09-14T13:37:44Z")

</div>

My current implementation is

```julia
function angle(a, b)
    return acosd(a⋅b/(norm(a)*norm(b)))
end

```

But, sometimes, it throws domain error as

```julia
LoadError: DomainError with 1.0000000000000002:
acos(x) not defined for |x| > 1

```

Is there any inbuilt function that provides such functionality? Or is there any method to ensure that `a⋅b/(norm(a)*norm(b))` stays within [-1, 1]?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 14, 2021, 1:40pm UTC](https://discourse.julialang.org/t/how-to-find-angle-between-two-vectors/68149/2 "2021-09-14T13:40:56Z")

</div>

The easiest way would be

```julia
function angle(a, b)
    return acosd(clamp(a⋅b/(norm(a)*norm(b)), -1, 1))
end

```

---

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [September 14, 2021, 1:48pm UTC](https://discourse.julialang.org/t/how-to-find-angle-between-two-vectors/68149/3 "2021-09-14T13:48:08Z")

</div>

Thank you! That works perfectly!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 14, 2021, 2:02pm UTC](https://discourse.julialang.org/t/how-to-find-angle-between-two-vectors/68149/4 "2021-09-14T14:02:25Z")

</div>

FWIW, [this post](https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#answer_185622) warns about the inaccuracy of the `acos` formula for small angles. Example reproduced below in Julia:

```julia
using LinearAlgebra
angled1(a, b) = acosd(clamp(a⋅b/(norm(a)*norm(b)), -1, 1))
angled2(a, b) = atand(norm(cross(a,b)),dot(a,b))
θ = 5e-9 # small angle in degrees
a = [1, 0, 0]
b = [cosd(θ), sind(θ), 0]

julia> angled1(a, b)
0.0
julia> angled2(a, b)
5.0e-9

```

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [September 14, 2021, 2:39pm UTC](https://discourse.julialang.org/t/how-to-find-angle-between-two-vectors/68149/5 "2021-09-14T14:39:56Z")

</div>

There’s a package for that which used arctangent of half angle (the upside is that the domain of arctangent is \mathbb{R})  
[https://github.com/JeffreySarnoff/AngleBetweenVectors.jl](https://github.com/JeffreySarnoff/AngleBetweenVectors.jl)
