Message-ID: <50377BF5.3030305@sapo.pt>
Date: 2012-08-24T13:04:53Z
From: Rui Barradas
Subject: R minimal calculation error
In-Reply-To: <CAPSkfAMdR5nO1P_D-7zYswv2poC8WbCyFU0tPygCgn8EoDNqbw@mail.gmail.com>
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
>