0

Where X = c(0.07825,-0.08177,0.00352), the numbers in X should sum to 0. Yet I get the following in R:

sum(X) 4.77049e-18

when I should get 0.

To re-edit my question, I think I understand why R does it, but what to do about it? In my particular case I need to input a matrix to a function (which I did not formulate) where all the rows sum to 0, otherwise the function will produce an error. Is there any way around this without editing the function?

Jason
  • 1
  • 1
  • 1
    Floating point numbers are often relative, as in this case. That is a "very small number close to zero". – user2864740 Mar 30 '15 at 20:27
  • `print(c(0.07825,-0.08177,0.00352), digits = 22)` – rawr Mar 30 '15 at 20:27
  • http://floating-point-gui.de/ This should be required reading for all programmers – chollida Mar 30 '15 at 20:27
  • 1
    there's a slightly more R-centric version of the generic floating-point-precision answer at http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal – Ben Bolker Mar 30 '15 at 20:31
  • It is not reasonable to expect a vector of double-precision floating point numbers to sum to exactly zero even if you try to enforce the requirement with, for example, `sum(X - mean(X)`. So I guess you need edit or re-write the function you are using. – bdemarest Mar 30 '15 at 23:30

0 Answers0