OVERFLOW ERROR for only this set of values

x = c(1, 8, 2, 6, 3, 8, 5, 5, 5, 5)
x
p=mean(x)
q=x-p
sum(q)
OUTPUT
1.776357e-15
Why am I getting such an output
is this a bug

x <- c(1, 8, 2, 6, 3, 8, 5, 5, 5, 5)
x
#>  [1] 1 8 2 6 3 8 5 5 5 5
p <- mean(x)
p
#> [1] 4.8
q <- x - p
sum(q)
#> [1] 1.776357e-15
float_ints <- function(x) {
  p <- mean(x)
  q <- x - p
  sum(q)
}

x <- c(1, 8, 2, 6, 3, 8, 7, 7, 7, 7)
float_ints(x) 
#> [1] 3.552714e-15

This is what is known as a floating point error. For typeof double R carries out the decimal places just so far. For example, 2.99 is represented as

2.99
#> [1] 2.99
options(digits = 22)
2.99
#> [1] 2.990000000000000213163

The number you expect is zero.
round(sum(q),1) will get you zero.

1 Like

Dplyr has a useful function for this

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Why are we getting such an output for these values only?