[R] R minimal calculation error

Rui Barradas ruipbarradas at sapo.pt
Fri Aug 24 15:04:53 CEST 2012


Hello,

This is FAQ 7.31 Why doesn't R think these numbers are equal?

As for your second question whether this behavior is desirable I think 
so, we should be aware that floating-point arithmetics has limits. In 
your case, a precision limit. At an R pompt run the instructions

?.Machine
.Machine$double.eps

to see the limits, and more.

As for your first question, you can't prevent but you can correct based 
on a chosen accuracy.
Your mesurements were made with how many digits? I doubt you have 16 
digits of accuracy. It's possible but it's not usual, it's rare. So you 
can round your final value using round().

set.seed(4130)
x <- runif(10)
#?scale
(s <- sum( scale(x, scale = FALSE) ))
#?round
round(s, digits = 10) # suppose you can only be sure of 10 digits.
round(s, digits = 15) # still zero
round(s, digits = 16) # precision loss

Hope this helps,

Rui Barradas

Em 24-08-2012 11:48, Frederik Bertling escreveu:
> Hi,
>
> I'm doing some easy calculations to normalize some values. This looks like
> this:
>
> x=mean(a+b+c+d ...)
> a=a-x
> b=b-x
> c=c-x
> d=d-x
> ...
> mean(a+b+c+d ...) ---> Should now be 0!
> However, I'm getting results like -2.315223e-18
> This is really near to 0 but not very aesthetic.
>
> Can I prevent this? Or is this behaviour desired?
> Thank you very much!
> Burtan
>




More information about the R-help mailing list