Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible Url : https://stat.ethz.ch/pipermail/r-help/attachments/20080409/28fc5778/attachment.pl
Petr PIKAL
2008-Apr-09 09:43 UTC
[R] Odp: problem with basic boolean selection in sequence
Hi Do not use computers, they are full of such traps and do not do what we think they do. See FAQ 7.31 Why doesn't R think these numbers are equal Regards Petr petr.pikal at precheza.cz r-help-bounces at r-project.org napsal dne 09.04.2008 11:32:00:> Dear R users, > > I have a surprising problem while selecting values in a sequence created> with the seq() function... > I've tried a lot of test before sending this here and hope I did not > disturb you for a foolish mistake from me > > Please, have a look at the following lines: > #_______________________________________________________________________ > > test2<-seq(from=0,to=1,by=.1) > > test2 > [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 > > test2==1 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE > > test2==0.9 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE > > test2==0.8 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE > > test2==0.7 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > test2==0.6 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > test2==0.5 > [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE > > test2==0.4 > [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE > > test2==0.3 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > test2==0.2 > [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > test2==0.1 > [1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > test2==0.0 > [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > > # there is a problem with values 0.3, 0.6 and 0.7 impossible to select > in such a vector... > # the same thing happens with other sequence including somme of thesevalues> > test3<-seq(from=0,to=1,by=0.01) > test3 > test3==0.3 # not working > test3==0.6 # working > test3==0.7 # not working >#________________________________________________________________________> > Does anyones has an explanation and a solution ? > > Kind regards > > Ben > > > > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Richard.Cotton at hsl.gov.uk
2008-Apr-09 09:45 UTC
[R] problem with basic boolean selection in sequence
> I have a surprising problem while selecting values in a sequence created> with the seq() function... >... > > test2<-seq(from=0,to=1,by=.1) >... > > test2==0.3 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE >... > Does anyones has an explanation and a solution ?I suspect that this is a problem with floating point rounding errors. The values in test2 are not exactly what you requested: format(test2, nsmall=20) #[1] "0.00000000000000000000" "0.10000000000000000555" "0.20000000000000001110" #[4] "0.30000000000000004441" "0.40000000000000002220" "0.50000000000000000000" #[7] "0.60000000000000008882" "0.70000000000000006661" "0.80000000000000004441" #[10] "0.90000000000000002220" "1.00000000000000000000" A workaround is to check that the difference between test2 and the target value is very small: abs(test2 - .3) < .Machine$double.eps # [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}