Bug introduced in 10.0 or earlier and fixed in 12.0.0 or earlier
I was expecting a real angle using VectorAngle
when passing real valued vectors, but I obtained a complex angle:
VectorAngle[{-2.7432000000000016`, 0.`, 0.`},{2.743199999999973`, 0.`, 0.`}]
returns
3.14159 - 2.10734*10^-8 I
I was expecting 3.14159
($\pi$) as this is essentially a half circle. My guess is the precision of the numbers results in the imaginary portion.
3 possible solutions are:
using
Round[result, 10^-7]
, which works for this case, but future cases might have an imaginary part larger than $10^{-8}$using
Re[result]
, a better solution.
Interestingly, using Round
on the vectors to some sufficiently small value results in an exact solution. For example I rounded to the nearest 10^-100
and got the exact value of $\pi$.
Going forward I'll probably just take the real part of the result, but is this the expected behavior in Mathematica for this function?