[R] identical values not so identical? newbie help please!

Petr Savicky savicky at praha1.ff.cuni.cz
Thu Mar 10 21:01:38 CET 2011


On Thu, Mar 10, 2011 at 10:34:21AM -0800, maiya wrote:
> Thanks Josh and Dan!
> 
> I did figure it had something to do with the machine epsilon...
> 
> But so what do I do now? I'm calculating the total absolute error over
> thousands of tables e.g.:
> tae<-sum(abs(obs-exp))
> Is there any easy way to I keep these ignorable errors from showing up?
> 
> And furthermore, why does this happen only sometimes? The two (2D) tables I
> attached are actually just one 'layer' in a 3D table. And only 2 out of
> about 400 layers had this happen, all the other ones are identical -
> perfectly!

Hi.

Two numbers cannot differ by an amount less than machine epsilon. If the
difference of two numbers is smaller, then they are exactly equal. So, if
a computation has very low error, then it shows up only sometimes. A simple
example is

  i <- 6:55
  i/100*100 - i

   [1]  0.000000e+00  8.881784e-16  0.000000e+00  0.000000e+00  0.000000e+00
   [6]  0.000000e+00  0.000000e+00  0.000000e+00  1.776357e-15  0.000000e+00
  [11]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
  [16]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
  [21]  0.000000e+00  0.000000e+00  3.552714e-15 -3.552714e-15  0.000000e+00
  [26]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
  [31]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
  [36]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
  [41]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
  [46]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  7.105427e-15

The "machine epsilon" is a relative quantity depending on the number, so the
errors here are different from what you observed. The numbers in your
tables "obs" and "exp" differ by 1 or 2 units in the order of the least
significant bit. This is hard to avoid unless you can work only with integers.

Hope this helps.

Petr Savicky.



More information about the R-help mailing list