0

It is a bit embarrassing to ask this question, but I don't know how to make sense of this. While doing something else, I ran this calculation in R:

(-3/5*2) + (2/5*3)
[1] 2.220446e-16

The output should actually be 0, so I don't understand where R's output came from. I tried restarting R and expressing this calculation in different ways (e.g. with decimals instead of fractions). Also, if I replace the second term, now R gives me the expected output:

(-3/5*2) + 6/5
[1] 0

Any thoughts?

Sol
  • 724
  • 1
  • 5
  • 18
  • 2
    This has to do with the way a computer handles [floating point](http://en.wikipedia.org/wiki/Floating_point). You can check things like this for equality using `all.equal((-3/5*2) + (2/5*3), 0)` – Justin Jan 15 '14 at 19:04
  • As Justin pointed out, your problem is with issues that arise in performing arithmetic on computers using numbers in floating-point representation. But note that the number reported by R may not be zero, it is damn close to it. – Ned Jan 15 '14 at 19:07

1 Answers1

2

You are seeing those results because in general, computer arithmetic with floating point numbers is not exact (not just in R). 2.220446e-16 is essentially 0.

Here's a helpful pdf on this, specifically for R: https://www.stat.auckland.ac.nz/~stat782/downloads/04-Numerics.pdf

so13eit
  • 942
  • 3
  • 11
  • 22