Skip to content
Prev 304054 / 398513 Next

R minimal calculation error

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: