[R] bug or bizarre feature?
Erik Iverson
eiverson at NMDP.ORG
Mon Nov 30 20:23:55 CET 2009
Simply a manifestation of FAQ 7.31, i.e., a floating-point arithmetic issue.
On my machine,
> 1.16 * 100 == 116
[1] FALSE
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Rupert Mazzucco
> Sent: Monday, November 30, 2009 1:15 PM
> To: r-help at r-project.org
> Subject: [R] bug or bizarre feature?
>
> Hello,
>
> I'm running into a very strange problem:
>
> > xrange <- c(-2.5,2.5)
> > xdim <- 100
> > mobility <- 0.1
> > slope <- 1.16
> > urange <- slope*xrange
> > udim <- max(slope*xdim,5)
> > du <- (urange[2]-urange[1])/udim
> > uvec <- urange[1]+(1:udim-0.5)*du
> > # type dependent weight function
> > ckern <- array(0,dim=c(udim,udim))
> > diag(ckern) = wfun(uvec,slope,mobility)
> Error in `diag<-`(`*tmp*`, value = c(0.992300064325398, 0.990746129315703,
> :
> replacement diagonal has wrong length
>
> It turns out that the array ckern has the wrong size for some reason.
> Instead of 116x116, it is only 115x115.
>
> > udim
> [1] 116
> > length(uvec)
> [1] 116
> > length(wfun(uvec,slope,mobility))
> [1] 116
> > dim(ckern)
> [1] 115 115
>
> The "taint" or whatever that is, is even transferable
>
> > n <- udim
> > n
> [1] 116
> > ckern <- array(0,dim=c(n,n))
> > dim(ckern)
> [1] 115 115
> > m <- n
> > m
> [1] 116
> > ckern <- array(0,dim=c(m,m))
> > dim(ckern)
> [1] 115 115
>
> But when I set it explicitly, it does what it should:
>
> > n <- 116
> > n
> [1] 116
> > ckern <- array(0,dim=c(n,n))
> > dim(ckern)
> [1] 116 116
>
> Note that the funny behavior seems to be peculiar to this one value of
> slope <- 1.16,
> many others work fine, e.g.
>
> > slope <- 1.08
> > urange <- slope*xrange
> > udim <- max(slope*xdim,5)
> > du <- (urange[2]-urange[1])/udim
> > uvec <- urange[1]+(1:udim-0.5)*du
> > # type dependent weight function
> > ckern <- array(0,dim=c(udim,udim))
> > diag(ckern) = wfun(uvec,slope,mobility)
> > dim(ckern)
> [1] 108 108
>
> This is R 2.10.0, but also happened in 2.8.0. Can anybody tell me what
> is going on here, and how I can get my array to be the right size?
>
> Thanks,
> Rupert
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list