From andy_liaw at merck.com  Fri Apr  8 03:45:29 2005
From: andy_liaw at merck.com (Liaw, Andy)
Date: Fri Apr  8 03:46:02 2005
Subject: [Rd] a couple of things about ":"
Message-ID: <39B6DDB9048D0F4DAD42CB26AAFF0AFA076D76@usctmx1106.merck.com>

> From: Thomas Lumley 
> 
> On Thu, 7 Apr 2005, Liaw, Andy wrote:
> 
> > - It seems counter-intuitive to allow non-integer values as 
> operand for ":".
> > It also seems a bit odd that 1:2 returns something with storage.mode
> > integer, whereas 0.5:2 gives doubles.  Would it make sense 
> to disallow
> > non-integer operands to ":"?  I can't think of a situation 
> when non-integer
> > operands for ":" would be good...
> >
> 
> It depends on what you mean by non-integer.  You certainly 
> want to allow 
> operand with storage.mode "double".  You might want to exclude 
> non-integers, but then you have problems with rounding:
> 
>    a<-sqrt(2)
>    b<-sqrt(3)
>    (a^2):(b^2)
> 
> would fail.  Allowing non-integers up to some tolerance would 
> be needed, 

I think the fact that we don't need to do as.integer(1):as.integer(3) in
order for ":" to return an integer vector must means that there's some
tolerance built-in.  (I know, I should just check the source...)  Here are
some experiments (R-devel 2005-03-11 on WinXPPro):

> storage.mode((1 + .Machine$double.eps):3)
[1] "double"
> storage.mode((1 - .Machine$double.eps/4):3)
[1] "integer"
> storage.mode((1 + .Machine$double.eps/4):3)
[1] "integer"
> storage.mode((1 + .Machine$double.eps/3):3)
[1] "integer"
> storage.mode((1 - .Machine$double.eps/3):3)
[1] "double"

> but then there isn't much reason to forbid 0.5:2, and someone 
> might have a use for it.

IMHO that's more of a trap than a `feature'.  At least to me it's not
immediately obvious what one should expect when the first operand is an
`obvious' non-integer such as 0.5.  For such sequences, I'd say one should
really use seq() instead of ":".  (I'd go as far as saying that something
like 0.5:2 is an abuse.)

BTW, here's something that surprised me:

> x <- 1:10
> i <- (1 + 1e-15):5
> i
[1] 1 2 3 4 5
> x[i]
[1] 1 2 3 4 5
> i <- (1 - 1e-15):5
> i
[1] 1 2 3 4 5
> x[i]
[1] 1 2 3 4

The reason for this, of course, is that (as ?"[" explains):

> as.integer(i)
[1] 0 1 2 3 4

My wish would be for ":" to coerce its operands to integers, perhaps with a
bit more tolerance, and always return object of integer storage mode.

Cheers,
Andy
 
> 
>  	-thomas
> 
> 
>


[Rd] a couple of things about ":"
Liaw, Andy