[R] Difference
Martyn Plummer
plummer at iarc.fr
Tue Apr 19 10:30:49 CEST 2005
On Tue, 2005-04-19 at 10:19 +0200, Ralf Strobl wrote:
> Dear List,
> can anyone explain me this result (Windows XP, R 2.0.1):
>
> > (0.2-0.1)==0.1
> [1] TRUE
> > (0.3-0.2)==0.1
> [1] FALSE
Yes. Floating point arithmetic isn't as accurate as you think. Your
numbers have a simple representation in decimal format, but are stored
in the computer in binary, so there is some rounding error.
> (0.3-0.2) - 0.1
[1] -2.775558e-17
You probably want to use "all.equal" to test for "near" equality
> all.equal(0.3-0.2,0.1)
[1] TRUE
> identical(all.equal(0.3-0.2,0.1), TRUE)
[1] TRUE
See the help pages for all.equal and identical for more details.
Martyn
More information about the R-help
mailing list