Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I was expecting a real angle using VectorAngle when passing real valued vectors, however I obtained a complex angle.

VectorAngle[{-2.7432000000000016`, 0.`, 0.`},{2.743199999999973`, 0.`, 0.`}]


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?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

I believe this is one of many manifestations of machine precision arithmetic in Mathematica, and it appears because VectorAngle also works with complex values. If you use the arbitrary precision engine these cancellation errors should not occur:

 VectorAngle[{-2.7432`12, 0, 0}, {2.7432`12, 0, 0}]

Note the `12 on the numbers; this sets a precision of 12 digits. Perhaps more applicable to your situation you can use SetPrecision (or SetAccuracy) on existing data to convert it to arbitrary precision:

in = {{-2.7432000000000016`, 0.`, 0.`}, {2.743199999999973`, 0.`, 0.`}};

VectorAngle @@ SetPrecision[in, 12]

Likewise using exact arithmetic will avoid this problem as you already note:

VectorAngle @@ Rationalize[in]

Please see the second part of this answer for an introduction to machine and arbitrary precision in Mathematica in my own words:

Recommended reading:

share|improve this answer
Thank you Mr.Wizard! –  Adam Law Aug 14 '14 at 22:20
@Adam You're welcome, and thanks for the Accept. –  Mr.Wizard Aug 14 '14 at 22:22

I think this is a bug.

You get the correct result from

vectorAngle[vec1_, vec2_] :=
 ArcCos[vec1.vec2/(Norm[vec1] Norm[vec2])]

vectorAngle[{-2.7432000000000016`, 0.`, 
  0.`}, {2.743199999999973`, 0.`, 0.`}]

(* ==> 3.14159 *)

This function is just a manual implementation of the definition as stated in the documentation of VectorAngle. Therefore, the current built-in VectorAngle does not appear to be implemented as described in the documentation.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.