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
> 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 > > >-- View this message in context: http://www.nabble.com/sin%28pi%29--tf4370556.html#a12457181 Sent from the R help mailing list archive at Nabble.com.
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 > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at 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 > 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 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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 > > ______________________________________________ > R-help at 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 > 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.
On Mon, 3 Sep 2007, Nguyen Dinh Nguyen wrote:> Dear all, > I found something strange when calculating sin of pi valueWhat exactly? Comments below on two guesses as to what.> sin(pi) > [1] 1.224606e-16That 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.141593That 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