sin(pi)?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

sin(pi)?

Nguyen Dinh Nguyen
Dear all,
I found something strange when calculating sin of pi value
sin(pi)
[1] 1.224606e-16

 pi
[1] 3.141593

 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


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: sin(pi)?

Olivier Delaigue
> sin(3.141592653589793)
[1] 1.224606e-16

Regards,

Olivier Delaigue


Nguyen Dinh Nguyen wrote
Dear all,
I found something strange when calculating sin of pi value
sin(pi)
[1] 1.224606e-16

 pi
[1] 3.141593

 sin(3.141593)
[1] -3.464102e-07

Any help and comment should be appreciated.
Regards
Nguyen
Reply | Threaded
Open this post in threaded view
|

Re: sin(pi)?

Peter Dalgaard
In reply to this post by Nguyen Dinh Nguyen
Nguyen Dinh Nguyen wrote:

> Dear all,
> I found something strange when calculating sin of pi value
> sin(pi)
> [1] 1.224606e-16
>
>  pi
> [1] 3.141593
>
>  sin(3.141593)
> [1] -3.464102e-07
>
> Any help and comment should be appreciated.
> Regards
> Nguyen
>  
Well, sin(pi) is theoretically zero, so you are just seeing zero at two
different levels of precision.

The built-in pi has more digits than it displays:

 > pi
[1] 3.141593
 > pi - 3.141593
[1] -3.464102e-07
 > print(pi, digits=20)
[1] 3.141592653589793

> ____________________________
> Nguyen Dinh Nguyen
> Garvan Institute of Medical Research
> Sydney, Australia
>
>  
> ------------------------------------------------------------------------
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>  


--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: sin(pi)?

Simon Blomberg-4
In reply to this post by Nguyen Dinh Nguyen
Umm. pi has been rounded to 6 decimal places in the second example. So
it isn't surprising that the results differ. sin(pi) is not zero, as it
also has been rounded, and you can't represent irrational numbers
exactly in a numerical form anyway. R agrees with Octave:

octave:1> sin(pi)
ans =  1.2246e-16
octave:2> sin(3.141593)
ans =  -3.4641e-07
octave:3>

To paraphrase someone else on this list: I think it is strange that you
think it is strange.

Simon.

As someone On Mon, 2007-09-03 at 16:43 +1000, Nguyen Dinh Nguyen wrote:

> Dear all,
> I found something strange when calculating sin of pi value
> sin(pi)
> [1] 1.224606e-16
>
>  pi
> [1] 3.141593
>
>  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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Simon Blomberg, BSc (Hons), PhD, MAppStat.
Lecturer and Consultant Statistician
Faculty of Biological and Chemical Sciences
The University of Queensland
St. Lucia Queensland 4072
Australia
Room 320 Goddard Building (8)
T: +61 7 3365 2506
email: S.Blomberg1_at_uq.edu.au

Policies:
1.  I will NOT analyse your data for you.
2.  Your deadline is your problem.

The combination of some data and an aching desire for
an answer does not ensure that a reasonable answer can
be extracted from a given body of data. - John Tukey.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: sin(pi)?

Prof Brian Ripley
In reply to this post by Nguyen Dinh Nguyen
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,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.