[R] a bug when subtracting vectors?
Raymond Wan
rwan at kuicr.kyoto-u.ac.jp
Mon Sep 24 12:18:40 CEST 2007
Samuel Kemp wrote:
> When subtracting, all numbers should be to 2 decimal places. Why is R
> calculating it to 15 decimal places -- the output is essentially wrong
> 112.47-112.30=0.17 NOT 0.170000000000002. I suspect I am encoding this
> incorrectly?
>
Well, two comments...one is not computer related. If you are
subtracting two numbers which have two digits after the decimal (i.e.,
two significant figures), you shouldn't be accepting a result of more
than two digits. So, the only thing wrong that you're doing is just
that you're not formatting the output properly by cutting the remaining
numbers.
As for the second comment, computers cannot represent decimal numbers
exactly and problems like this occurs very often. In the above example,
one of your numbers can be represented exactly as 112.47 or 112.30.
That's just something everyone has to be aware of when using computers
to do math. This, in my opinion, is an excellent paper on the
problem...it is quite technical, but at least worth a skim:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
I found this sentence near the beginning of the article to be perfect:
"Squeezing infinitely many real numbers into a finite number of bits
requires an approximate representation."
I presume there's an FAQ for R on this...not sure where it is, though.
But, I hope this helps!
Ray
More information about the R-help
mailing list