From macrakis at alum.mit.edu  Mon Mar 16 16:24:41 2009
From: macrakis at alum.mit.edu (Stavros Macrakis)
Date: Mon, 16 Mar 2009 11:24:41 -0400
Subject: [Rd] Match .3 in a sequence
In-Reply-To: <48f8cced0903160636k112500f6k4b8d46c1ac775d89@mail.gmail.com>
References: <48f8cced0903160636k112500f6k4b8d46c1ac775d89@mail.gmail.com>
Message-ID: <8b356f880903160824q4fb9068br1f22601064a8042d@mail.gmail.com>

Well, first of all, seq(from=.2,to=.3) gives c(0.2), so I assume you
really mean something like seq(from=.2,to=.3,by=.1), which gives
c(0.2, 0.3).

%in% tests for exact equality, which is almost never a good idea with
floating-point numbers.

You need to define what exactly you mean by "in" for floating-point
numbers.  What sort of tolerance are you willing to allow?

Some possibilities would be for example:

approxin <- function(x,list,tol) any(abs(list-x)<tol)   # absolute tolerance

rapproxin <- function(x,list,tol) (x==0 && 0 %in% list) ||
any(abs((list-x)/x)<=tol,na.rm=TRUE)
     # relative tolerance; only exact 0 will match 0

Hope this helps,

          -s

On Mon, Mar 16, 2009 at 9:36 AM, Daniel Murphy <chiefmurphy at gmail.com> 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?



[Rd] Match .3 in a sequence
Stavros Macrakis