1

I am doing a simple row sum and two columns give me 0 (which is the number it should give), but the last one gives an epsilon, but not zero per se.

# generate the row values that their sumation should give zero.
d<-0.8
c<-1-d
a<-0.5
b<-0.5
e<-0.2
f<-1-e

Perc<-c(-1, a,b,c,-1,d,e,f,-1)

# Put them in a 3x3 matrix
div<-matrix(ncol = 3, byrow = TRUE,Perc)

# Do the row sum
rowSums(div)

# RESULT
[1] 0.000000e+00 0.000000e+00 5.551115e-17

rowSums(div)[3]==0
[1] FALSE

I am using this version of R: version 3.4.1 (2017-06-30) -- "Single Candle"

Any idea why ? and how i can fix this?

Jaap
  • 81,064
  • 34
  • 182
  • 193
donpresente
  • 1,230
  • 2
  • 13
  • 24

1 Answers1

3

This happens because the machines can't store decimal numbers exactly. There can be a small error for some numbers.

The fix here is to use the all.equal function. It takes the tolerance level of the machine into account when comparing two numbers.

all.equal(sum(div[3, ]), 0) 

TRUE

kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42