[R] sin(pi)?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Sep 3 09:07:03 CEST 2007


On Mon, 3 Sep 2007, Nguyen Dinh Nguyen wrote:

> Dear all,
> I found something strange when calculating sin of pi value

What exactly?  Comments below on two guesses as to what.

> sin(pi)
> [1] 1.224606e-16

That is non-zero due to using finite-precision arithmetic.  The number 
stored as pi is not exactly the mathematics quantity, and so 
sin(representation of pi) should be non-zero (although there is also 
rounding error in calculating what it is).

Note that sin() is computed by your C runtime, so the exact result will 
depend on your OS, compiler and possibly CPU.

> pi
> [1] 3.141593

That is the printout of pi to the default 7 significant digits.  R knows 
pi to higher accuracy:

> print(pi, digits=16)
[1] 3.141592653589793
> sin(3.141592653589793)
[1] 1.224606e-16

but note that printing to 16 digits and reading back in might not have 
given the same number, but happens to for pi at least on my system:

> 3.141592653589793 == pi
[1] TRUE


> sin(3.141593)
> [1] -3.464102e-07
>
> Any help and comment should be appreciated.
> Regards
> Nguyen
>
> ____________________________
> Nguyen Dinh Nguyen
> Garvan Institute of Medical Research
> Sydney, Australia
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list