RE: [R] strange result of acos

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Tue 29 Mar 2005 - 21:59:28 EST


On 29-Mar-05 Klaus-Peter Pleissner wrote:
> Hi all,
> I have to calculate an expression using acos -function.
> A strange result of acos appears
>
> *1. case with error*
>
> ss <- sin(10.74*pi/180)**2
> +(cos(10.74*pi/180)*cos(10.74*pi/180)*cos(0*pi/180))
> ss
> acos(ss)
>
> [1] NaN
> Warning message:
> NaNs produced in: acos(ss)
>
> *2. case without error*
>
> ss <- sin(10.7*pi/180)**2
> +(cos(10.7*pi/180)*cos(10.7*pi/180)*cos(0*pi/180))
> ss
> acos(ss)
>
> In both cases the variable ss equals 1. I think there
> is a problem with precision. If I multiply ss by
> 0.999999999 (ss*0.99999999) no error occurs. How the
> results can be explained ?

The explanation is:

sin(10.74*pi/180)**2
+(cos(10.74*pi/180)*cos(10.74*pi/180)
*cos(0*pi/180))
-1

[1] 2.220446e-16

I.e. the expression, as internally evaluated, is very slightly greater than 1. When you multiply ss by 0.999999999, you bring it back down a bit.

Theoretically, of course, it is sin(t)^2 + cos(t)^2, which should be exactly 1, but you can't count on it in digital computation. As you suspected, it is indeed a question of precision.

Best wishes,
Ted.



E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 29-Mar-05                                       Time: 11:59:28
------------------------------ XFMail ------------------------------

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Mar 29 21:16:16 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:56 EST