Hello, I don't know if the result of acos(0.5) == pi/3 is a bug or not. It looks strange to me. Inaki Murillo
On Mon, 2006-09-18 at 19:31 +0200, I?aki Murillo Arcos wrote:> Hello, > > I don't know if the result of > > acos(0.5) == pi/3 > > is a bug or not. It looks strange to me. > > Inaki MurilloSeems reasonable to me:> acos(0.5) == pi/3[1] FALSE> print(acos(0.5), 20)[1] 1.0471975511965978534> print(pi/3, 20)[1] 1.0471975511965976313 See R FAQ 7.31 Why doesn't R think these numbers are equal? HTH, Marc Schwartz
How close do you think it should be, given finite resolution with digital computing?> acos(0.5) - pi/3[1] 2.220446e-16 Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax: 614-455-3265 http://www.StatisticalEngineering.com -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of I?aki Murillo Arcos Sent: Monday, September 18, 2006 1:32 PM To: r-help at stat.math.ethz.ch Subject: [R] acos(0.5) == pi/3 FALSE Hello, I don't know if the result of acos(0.5) == pi/3 is a bug or not. It looks strange to me. Inaki Murillo ______________________________________________ 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.
If you had looked at help("==") you would have seen the following: For numerical and complex values, remember == and != do not allow for the finite representation of fractions, nor for rounding error. Using all.equal with identical is almost always preferable. Then if you had tried> all.equal(acos(0.5),pi/3)[1] TRUE On 18/09/06, I?aki Murillo Arcos <inaki.murillo at ehu.es> wrote:> Hello, > > I don't know if the result of > > acos(0.5) == pi/3 > > is a bug or not. It looks strange to me. > > Inaki Murillo > > ______________________________________________ > 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. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP
I?aki Murillo Arcos <inaki.murillo at ehu.es> writes:> Hello, > > I don't know if the result of > > acos(0.5) == pi/3 > > is a bug or not. It looks strange to me.Have a look in the R FAQ:> acos(0.5) - pi/3[1] 2.220446e-16 -- 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
On Mon, 18 Sep 2006, [iso-8859-1] I?aki Murillo Arcos wrote:> Hello, > > I don't know if the result of > > acos(0.5) == pi/3 > > is a bug or not. It looks strange to me.Real numbers are strange. Would the result of: acos(0.5) - pi/3 close enough to zero for you? Try help('Comparison') , ?'==' for an explanation, or 'abs(acos(0.5) - pi/3) < 1e-9' for a solution. Dave -- Dr. David Forrest drf at vims.edu (804)684-7900w drf5n at maplepark.com (804)642-0662h http://maplepark.com/~drf5n/
On 9/18/06, I?aki Murillo Arcos <inaki.murillo at ehu.es> wrote:> Hello, > > I don't know if the result of > > acos(0.5) == pi/3 > > is a bug or not. It looks strange to me.However,> all.equal(acos(0.5), pi/3)[1] TRUE so you may want to check the FAQ entry on comparisons involving floating point numbers.
I?aki Murillo Arcos said the following on 9/18/2006 12:31 PM:> Hello, > > I don't know if the result of > > acos(0.5) == pi/3 > > is a bug or not. It looks strange to me. > > Inaki Murillo > > ______________________________________________ > 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.This is a FAQ: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f > acos(.5) [1] 1.047198 > pi/3 [1] 1.047198 > acos(0.5) == pi/3 [1] FALSE > all.equal(acos(0.5), pi/3) [1] TRUE --sundar
On 18-Sep-06 I?aki Murillo Arcos wrote:> Hello, > > I don't know if the result of > > acos(0.5) == pi/3 > > is a bug or not. It looks strange to me. > > Inaki MurilloIt is not a bug, but a feature, in that acos(0.5) and pi/3 are not computed in the same way, so (because of the small inaccuracies inevitable in their finite representations) they are indeed not equal; and this is what R is telling you. acos(0.5) - pi/3 [1] 2.220446e-16 The difference, as you can see is very small. However, if for some reason (e.g. in the logic of a programming branch which depends on such a comparison) then you could make it work by computing them in the same way: pi<-3*acos(0.5) acos(0.5) - pi/3 [1] 0 I.e. redefine pi in R so that its computation is compatible with that of acos(0.5) (but this pi is only in the current environment). However, the usual way of comparing two numbers which should theoretically be equal, but might differ slightly when computed in R, is to use all.equal(x, y) See "?all.equal". Also see "?identical". all.equal(acos(0.5),pi/3) [1] TRUE (Here I'm using R's "default" value of pi, not the value I defined above). all.equal(x,y) tests whether x and y differ by more than a tolerance which by default is .Machine$double.eps but which can be set to something else if you wish. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 18-Sep-06 Time: 23:05:01 ------------------------------ XFMail ------------------------------