[vtkusers] Angle between two vectors

Gib Bogle g.bogle at auckland.ac.nz
Mon Jan 6 16:32:39 EST 2014


I repeated the test with Intel Fortran on Windows 7:

 acos max error:   1.772791329594716E-011
 atan2 max error:   4.440892098500626E-016
________________________________________
From: vtkusers-bounces at vtk.org [vtkusers-bounces at vtk.org] on behalf of David Gobbi [david.gobbi at gmail.com]
Sent: Tuesday, 7 January 2014 9:47 a.m.
To: Sean McBride
Cc: David Cole; VTK Users
Subject: Re: [vtkusers] Angle between two vectors

On Mon, Jan 6, 2014 at 11:48 AM, Sean McBride <sean at rogue-research.com> wrote:

> We have a little angle function as described here and it uses acos().
> I've changed it to use atan2 and the results are equal (in my typical
> usage) to at least 1e-12.

To estimate the error in atan2, and acos, I measured how accurately
each of them could recover an angle (using python).

from math import *
maxerr = 0.0
for i in range(0,10000000):
     t = i*pi/10000000
     e = acos(cos(t)) - t
     maxerr = max(maxerr, abs(e))

print maxerr
1.72315651329e-10

maxerr = 0.0
for i in range(0,10000000):
     t = i*pi/10000000
     e = atan2(sin(t), cos(t)) - t
     maxerr = max(maxerr, abs(e))

print maxerr
2.22044604925e-16

In summary:

atan2 has an accuracy that is close to machine precision.

acos has an accuracy that is much worse than machine precision, but I
should qualify that by saying that it is only bad in certain cases,
i.e. when x=+1 or x=-1.  The asin function is similarly afflicted when
x=0.

The maximum error of acos() is encountered when the angle is 1.05e-8,
because for angles of this size or less it will always return zero.

t = 1.05e-8
print abs(t - acos(cos(t))
1.05e-8

> I measured performance too.  On my Mac Pro, in debug, averaged
> after 1000 runs, the atan2 version is actually faster at 2.7 µs vs
> 3.7 µs for the acos version.  In release it's the opposite, with acos
> at 70 ns and atan2 at 379 ns.  I didn't look too closely; I'd say it's a wash.

I took a look at Apple's code for the atan() and acos() function...
it's pretty interesting.  Instead of using iterative algorithms, they
use 12th order polynomial approximation.  So both should be pretty
fast.

  David
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers


More information about the vtkusers mailing list