Dear All, I have a seemingly very simple question, but I just cannot figure out the answer. I attempted to run the following:a=0.1*(1:9);which(a==0.3);it returns integer(0). But obviously, the third element of a is equal to 0.3. I must have missed something. Can someone kindly explain why? Thanks a lot. Regards,Dajiang [[alternative HTML version deleted]]
On Mar 18, 2012, at 4:43 PM, Dajiang Liu wrote:> > Dear All, > I have a seemingly very simple question, but I just cannot figure > out the answer. I attempted to run the > following:a=0.1*(1:9);which(a==0.3);it returns integer(0). But > obviously, the third element of a is equal to 0.3. > I must have missed something. Can someone kindly explain why? Thanks > a lot.It has already been explained on this list ... "frequently" in FAQt. Locate the FAQ and search for a question about why R doesn't think two numbers are equal. The FAQ should be part of a standard instalL on the main help page.> Regards,Dajiang > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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 Winsemius, MD Heritage Laboratories West Hartford, CT
As to the reasons, David as given you the necessary hints. In order to get around the issue, here is what I do:> a <- round( 0.1 * ( 1:9 ), 1 ) > a[1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9> which( a == 0.3 )[1] 3 Rgds, Rainer -------- Original-Nachricht --------> Datum: Sun, 18 Mar 2012 21:43:54 +0000 > Von: Dajiang Liu <ldjstudy at hotmail.com> > An: r-help at r-project.org > Betreff: [R] a very simple question> > Dear All, > I have a seemingly very simple question, but I just cannot figure out the > answer. I attempted to run the following:a=0.1*(1:9);which(a==0.3);it > returns integer(0). But obviously, the third element of a is equal to 0.3. > I must have missed something. Can someone kindly explain why? Thanks a > lot. > Regards,Dajiang > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.-- ------- Gentoo Linux with KDE
On Sun, Mar 18, 2012 at 09:43:54PM +0000, Dajiang Liu wrote:> > Dear All, > I have a seemingly very simple question, but I just cannot figure out the answer. I attempted to run the following:a=0.1*(1:9);which(a==0.3);it returns integer(0). But obviously, the third element of a is equal to 0.3. > I must have missed something. Can someone kindly explain why? Thanks a lot.Hi. A simple way to detect rounding problems is subtracting the numbers. a = 0.1*(1:4) a - 0.3 [1] -2.000000e-01 -1.000000e-01 5.551115e-17 1.000000e-01 Use rounding to avoid it as suggested by others. Petr Savicky.