[R] Re: [Rd]Comparing fp numbers, was Bug in %in% (match)
Nicholas Lewin-Koh
nikko at hailmail.net
Mon Apr 7 05:10:24 CEST 2003
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