Skip to content
Prev 70610 / 398502 Next

trouble with cumsum?

On 29 May 2005 at 20:17, Makram Talih wrote:
| Dear R users,
| 
| I am using R version 2.0.1 (2004/11/15) on an i386-pc-mingw32 platform. I
| encounter the following problem while using cumsum:
| 
| > a <- rep(0.01, 100)
| > b <- cumsum(a)
| > sum(a) == 1
| [1] TRUE
| > b[100] == 1
| [1] FALSE
| 
| Am I missing something? 

Yes, FAQ section 7.31 entitled "Why doesn't R think these numbers are equal?".

| Should cumsum have such an outcome?
[1] FALSE
[1] TRUE
[1] TRUE
On my Linux system with R 2.1.0, the first equality fails whereas it "worked"
for you. Morale: don't forget about floating-point math ...

Hth, Dirk