Path: news.mathworks.com!newsfeed-00.mathworks.com!news.kjsl.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread3.news.pas.earthlink.net.POSTED!5f968bd6!not-for-mail
From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)
Newsgroups: comp.soft-sys.matlab
Subject: Re: Angle between two vectors
Message-ID: <ellieandrogerxyzzy-1007071622370001@dialup-4.232.15.192.dial1.losangeles1.level3.net>
References: <ef5ce9c.-1@webcrossing.raydaftYaTP> <1184052626.324470.175540@n2g2000hse.googlegroups.com> <ellieandrogerxyzzy-1007070127090001@dialup-4.232.57.130.dial1.losangeles1.level3.net> <1184104526.774530.54800@n60g2000hse.googlegroups.com>
Organization: -
Lines: 50
Date: Tue, 10 Jul 2007 23:22:36 GMT
NNTP-Posting-Host: 4.232.15.192
X-Complaints-To: abuse@earthlink.net
X-Trace: newsread3.news.pas.earthlink.net 1184109756 4.232.15.192 (Tue, 10 Jul 2007 16:22:36 PDT)
NNTP-Posting-Date: Tue, 10 Jul 2007 16:22:36 PDT
Xref: news.mathworks.com comp.soft-sys.matlab:418366


In article <1184104526.774530.54800@n60g2000hse.googlegroups.com>, Greg
Heath <heath@alumni.brown.edu> wrote:

> For some problem in the past (probably single precision?) I got better
> accuracy using
> 
> sign(sintheta)*acos(costheta)
> 
> instead of atan2.
---------------
  I don't know why that would be better, Greg.  The acos(costheta) is
subject to the same problem as before.  When costheta is very near 1, the
accuracy is very much inferior to that of 'atan2'.  I would think this
would be equally true in single precision.

  Here's a concrete example of what I am referring to:

 format long
 a = pi*(1-1/10^8); % Choose an angle a little below pi
 a2 = atan2(sin(a),cos(a)); % Use atan2
 a3 = acos(cos(a));  % Use acos
 [a;a2;a3;a-a2;a-a3] % Compare results

 a = pi/2*1.123;  % Now select an angle near pi/2
 a2 = atan2(sin(a),cos(a)); % atan2 again
 a3 = acos(cos(a));  % Then acos
 [a;a2;a3;a-a2;a-a3] % Make the same comparisons

 ans =

   3.14159262217387
   3.14159262217387
   3.14159262378747
                  0
  -0.00000000161360

 ans =

   1.76400427499067
   1.76400427499067
   1.76400427499067
   0.00000000000000
   0.00000000000000

As you see, there is a very distinct loss of accuracy in 'acos' for angles
near pi.  Some seven entire decimal places have been lost - that is,
errors are several million times as large as normal.  On the other hand,
the angle near pi/2 yields the customary 1 in 2^52 accuracy.

Roger Stafford