Skip to content
Prev 31895 / 63424 Next

Match .3 in a sequence

On 3/16/2009 9:36 AM, Daniel Murphy wrote:
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)).

Duncan Murdoch