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 -- Frederik Bertling Steinhausenstr. 37, 45147 Essen, Deutschland Mobil: 017661103480* * [[alternative HTML version deleted]]
On Aug 24, 2012, at 5:48 AM, Frederik Bertling <Frederik.Bertling at stud.uni-due.de> wrote:> 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! > BurtanRead this: cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f Regards, Marc Schwartz
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 >
On Fri, Aug 24, 2012 at 12:48:54PM +0200, Frederik Bertling wrote:> 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?Hi. This is a consequence of the limited precision, which is used for speed. Examples of this type exist also for the decimal arithmetic. Using 3 digit precision, the arithmetic mean of the three numbers 1.02, 1.01, 1.01 is 1.01. So, the sum of the differences 1.02 - 1.01 1.01 - 1.01 1.01 - 1.01 is not zero. See functions zapsmall(), all.equal(). Hope this helps. Petr Savicky.