Duncan Murdoch wrote: > On 3/16/2009 9:36 AM, Daniel Murphy wrote: >> Hello:I am trying to match the value 0.3 in the sequence seq(.2,.3). >> I get >>> 0.3 %in% seq(from=.2,to=.3) >> [1] FALSE >> Yet >>> 0.3 %in% c(.2,.3) >> [1] TRUE >> For arbitrary sequences, this "invisible .3" has been problematic. >> What is >> the best way to work around this? > > Don't assume that computations on floating point values are exact. > Generally computations on small integers *are* exact, so you could > change that to > > 3 %in% seq(from=2, to=3) > > and get the expected result. You can divide by 10 just before you use > the number, or if you're starting with one decimal place, multiply by > 10 *and round to an integer* before doing the test. Alternatively, > use some approximate test rather than an exact one, e.g. all.equal() > (but you'll need a bit of work to make use of all.equal() in an > expression like 0.3 %in% c(.2,.3)).
there's also the problem that seq(from=0.2, to=0.3) does *not* include 0.3 (in whatever internal form), simply because the default step is 1. however, 0.3 %in% seq(from=.2,to=.3, by=0.1) # FALSE so it won't help anyway. (but in general be careful about using seq and the like.) vQ ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel