Skip to content
Prev 42033 / 63435 Next

bug in sum() on integer vector

Hi Duncan,
On 11-12-10 05:27 AM, Duncan Murdoch wrote:
It does, really? Seems like returning 0 would be a better approximation
;-) And with the argument that "R doesn't think of an integer as a
particular representation" then there is no reason why sum(x)
would get it wrong and sum(as.double(x)) would get it right. Also why
bother having an integer type in R?

Seriously, I completely disagree with your view (hopefully it's only
yours, and not an R "feature") that it's ok for integer arithmetic to
return an approximation. It should always return the correct value or
fail. This is one of the reasons why programmers use integers and not
floating point numbers (memory usage being another one). Integers are
used for indexing elements in an array or for shifting pointers at the
C-level. The idea that integer arithmetic can be approximate is scary.

Cheers,
H.