# [R] R: Why this deosn't work?, matrix, rounding error?

Peter Ehlers ehlers at ucalgary.ca
Sat Oct 9 13:42:25 CEST 2010

```On 2010-10-09 4:47, skan wrote:
>
> Hello
>
> I've seen the answer at stackoverflow.
> They also said I must use zapsmall to avoid roundup problems.
> I didn't expect this behaviour when division gives an integer number.

The trouble is that your expectations may not coincide with reality.
That's why people refer you to FAQ 7.31.

Even replacing the rep(0, ....) with just 0 will not necessarily
give the expected result:

eps1 <- 1e-16
eps2 <- 1e-15

## try to generate a 3-by-4 matrix:

matrix(0, nrow = 3 - eps1, ncol = 4)
#     [,1] [,2] [,3] [,4]
#[1,]    0    0    0    0
#[2,]    0    0    0    0
#[3,]    0    0    0    0

matrix(0, nrow = 3 - eps2, ncol = 4)
#     [,1] [,2] [,3] [,4]
#[1,]    0    0    0    0
#[2,]    0    0    0    0

matrix(0, nrow = zapsmall(3 - eps2), ncol = 4)
#     [,1] [,2] [,3] [,4]
#[1,]    0    0    0    0
#[2,]    0    0    0    0
#[3,]    0    0    0    0

## Note that your calculation did _not_ yield an integer:
1 + ((1.5 - 0.1) / 0.05) - 29
#[1] -3.552714e-15

Such are the vagaries of floating-point arithmetic. Play it
safe; use zapsmall.

-Peter Ehlers

```