[Rd] Undocumented bahavior of as.integer() (PR#2430)

Duncan Murdoch murdoch@stats.uwo.ca
Wed Jan 8 14:49:05 2003

On Wed, 8 Jan 2003 14:19:20 +0100, you wrote in message

>The problem is that it can lead to bugs in code using X[y] with y being a
>vector of doubles, and these bugs are very difficult to track. It is teach
>to R users that they do not need to bother too much about the storage mode.
>For instance, in the 'Introduction to R' manual, p.8:

I agree that this should be mentioned in the help page, but I don't
know any real code where it's likely to be a problem.  The main place
where people calculate indices is in array indexing schemes, and those
calculations work only on integers.  Even if they're stored as
doubles, integer calculations are exact, so the truncation wouldn't be
a problem.

The other place where floating point values might be used as indices
would be in interpolation schemes, and there it probably doesn't
matter much whether you use i-1, i, or i+1 as the index, as long as
identities like

   as.integer(x)  - 1 == as.integer(x-1)

hold.  That particular identity fails for x in (0, 1), but I think it
holds everywhere else that as.integer works; variations on it also
hold just about everywhere except near 0.

Do you know real examples where this really matters?

Duncan Murdoch