Do you heve any idea why I get after this instruction everywhere false?> seq (0, 1, by=0.1) == 0.3[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE But after different step it's ok:> seq(0, 1, by=0.1) == 0.4[1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE -- View this message in context: http://www.nabble.com/seq%28...%29-strange-logical-value-tp23920374p23920374.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 08.06.2009 10:45:15:> > Do you heve any idea why I get after this instruction everywhere false? > > seq (0, 1, by=0.1) == 0.3 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > But after different step it's ok: > > seq(0, 1, by=0.1) == 0.4 > [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSEDo not use computer, it is known to be imprecise. See FAQ 7.31 Why doesn't R think these numbers are equal Regards Petr> > -- > View this message in context:http://www.nabble.com/seq%28...%29-strange-> logical-value-tp23920374p23920374.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
http://wiki.r-project.org/rwiki/doku.php?id=misc:r_accuracy:decimal_numbers http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f Grześ wrote:> Do you heve any idea why I get after this instruction everywhere false? > >> seq (0, 1, by=0.1) == 0.3 >> > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > But after different step it's ok: > >> seq(0, 1, by=0.1) == 0.4 >> > [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE > >[[alternative HTML version deleted]]
See http://wiki.r-project.org/rwiki/doku.php?id=misc:r_accuracy:decimal_numbers#sequences_of_decimal_numbers and also http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f Grześ wrote:> Do you heve any idea why I get after this instruction everywhere false? > >> seq (0, 1, by=0.1) == 0.3 >> > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > But after different step it's ok: > >> seq(0, 1, by=0.1) == 0.4 >> > [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE > >[[alternative HTML version deleted]]
How about this:> "%==%" <- function(x, y) {if (length(x) > 1) { sapply(x, function(z) isTRUE(all.equal(z, y))); } else { sapply(y, function(z) isTRUE(all.equal(z, x))); } }> seq(0, 1, by=0.1) %==% 0.1[1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE> seq(0, 1, by=0.1) %==% 0.2[1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE> seq(0, 1, by=0.1) %==% 0.3[1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE> seq(0, 1, by=0.1) %==% 0.4[1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE> 0.3 %==% seq(0, 1, by=0.1)[1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE On Mon, Jun 8, 2009 at 4:45 PM, Grze?<gregorio99 at gmail.com> wrote:> > Do you heve any idea why I get after this instruction everywhere false? >> seq (0, 1, by=0.1) == 0.3 > ?[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > But after different step it's ok: >> seq(0, 1, by=0.1) == 0.4 > ?[1] FALSE FALSE FALSE FALSE ?TRUE FALSE FALSE FALSE FALSE FALSE FALSE > > -- > View this message in context: http://www.nabble.com/seq%28...%29-strange-logical-value-tp23920374p23920374.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >