[R] Re: [Rd]Comparing fp numbers, was Bug in %in% (match)
Nicholas Lewin-Koh
nikko at hailmail.net
Mon Apr 7 05:26:34 CEST 2003
Hi,
I am dense some days, the following worked;
tst<-as.integer(get.mult(precis)*new.t)%in%as.integer(get.mult(precis)*round(t,precis))
I didn't catch this part of the documentation
Factors are converted to character vectors, and then `x' and
`table' are coerced to a common type (the later of the two types
in R's ordering, logical < integer < numeric < complex <
character) before matching.
Nicholas
On Mon, 2003-04-07 at 11:10, Nicholas Lewin-Koh wrote:
> Hi,
> Thanks, I should have thought of that. If I do
> tst<-(10*seq(100,125,by=.2))%in%(10*seq(0,800,by=.1))
> > sum(tst)
> [1] 122
> > tst<-(seq(100,125,by=.2))%in%(seq(0,800,by=.1))
> > sum(tst)
> [1] 76
> The problem is corrected. However in my code, where I am comparing much
> longer sequences it doesn't always work, even with rounding and
> multiplying to an integer. Is there a more robust way to do this? Here
> is the function where things are going wrong
>
> spectots<-function(t,x,begin=min(t,na.rm=TRUE),end=max(t,na.rm=TRUE),
> by.inter=.5,precis=1){
> #browser()
> j<-!is.na(t)
> k<-!((t>end) | (t<begin))
> j<-j&k
> t<-t[j]
> x<-x[j]
> get.mult<-function(pre)return(10^pre)
> new.t<-seq(begin,end,by=by.inter)
> new.x<-rep(NA,length(new.t))
> #new.n<-length(new.t)
> t.err<-rep(NA,length(new.t))
> ######### Here is the matching ############
> tst<-(get.mult(precis)*new.t)%in%(get.mult(precis)*round(t,precis))
> check<-sum(tst)
> if(check!=length(t))warning("not all values matched!")
> new.x[tst]<-x
> t.err[tst]<-t-new.t[tst]
> x.impute<-ifelse(is.na(new.x),.5*min(new.x,na.rm=TRUE),new.x)
> ts(cbind(x.impute,new.x,t.err),start=begin,end=end,freq=1/by.inter)
> }
>
> On Sat, 2003-04-05 at 03:09, Peter Dalgaard BSA wrote:
> > Nicholas Lewin-Koh <nikko at hailmail.net> writes:
> >
> > > Hi,
> > > Am I hitting some limit in match? Consider the following example:
> > >
> > > > tst<-seq(100,125,by=.2)%in%seq(0,800,by=.1)
> > > > sum(tst)
> > > [1] 76
> >
> > > Gives the correct answer. Did I miss something?
> >
> > The fact that 1/5 and 1/10 are not represented exactly in a binary
> > computer?
> >
> > > sum(tst<-seq(100,125,by=.2)%in%seq(0,800,by=.1))
> > [1] 76
> > > sum(tst<-seq(100,125,by=.2)%in%round(seq(0,800,by=.1),1))
> > [1] 126
> >
> > And, just to be sure:
> >
> > > sum(round(seq(100,125,by=.2),1)%in%round(seq(0,800,by=.1),1))
> > [1] 126
> >
> >
> > --
> > O__ ---- Peter Dalgaard Blegdamsvej 3
> > c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
> > (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
> > ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
> >
>
More information about the R-help
mailing list