Skip to content
Back to formatted view

Raw Message

Message-ID: <1113899449.5885.2.camel@seurat>
Date: 2005-04-19T08:30:49Z
From: Martyn Plummer
Subject: Difference
In-Reply-To: <4264BF02.5080808@ibe.med.uni-muenchen.de>

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