[vtkusers] Angle between two vectors
David Gobbi
david.gobbi at gmail.com
Mon Jan 6 15:47:31 EST 2014
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
More information about the vtkusers
mailing list