LinearAlgebra: unstable floating point operations?

So I know this isn’t what you asked for, but have you seen https://github.com/JeffreySarnoff/AngleBetweenVectors.jl? @JeffreySarnoff put a lot of thought into making this robust.

that said, you could just do something like

my_acos(x) = x ≈ 1 ? zero(x) : x ≈ -1 ? one(x)*π : acos(x)

julia> my_acos(x) = x ≈ 1 ? zero(x) : x ≈ -1 ? one(x)*π : acos(x)
my_acos (generic function with 1 method)

julia> my_acos(1 + 1e-10)
0.0

julia> my_acos(1 + 1e-5)
ERROR: DomainError with 1.00001:
acos(x) not defined for |x| > 1
Stacktrace:
 [1] acos_domain_error(::Float64) at ./special/trig.jl:671
 [2] acos(::Float64) at ./special/trig.jl:701
 [3] my_acos(::Float64) at ./REPL[3]:1
 [4] top-level scope at REPL[5]:1

You can also adjust the tolerance you use for checking if |x| is approximately 1.

6 Likes