Skip to content

numeric equality

5 messages · Yu, Changhong, Sarah Goslee, David Smith +1 more

#
This is a FAQ:
http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
and also covered nicely in Burns' R Inferno:
www.burns-stat.com/pages/Tutor/R_inferno.pdf

Sarah
On Wed, Mar 18, 2009 at 11:58 AM, Yu, Changhong <YuC at ccf.org> wrote:

  
    
#
On Wed, Mar 18, 2009 at 8:58 AM, Yu, Changhong <YuC at ccf.org> wrote:
This is expected behaviour.  The bottom line is that you shouldn't
rely on the equality operator when dealing with floating point values
in any numerical software, including R.

I write about this in detail at:

http://blog.revolution-computing.com/2009/03/when-is-a-zero-not-a-zero.html

which will hopefully clear up your questions.

# David Smith
#
Thanks very much.

-----Original Message-----
From: David M Smith [mailto:david at revolution-computing.com] 
Sent: Wednesday, March 18, 2009 12:55 PM
To: Yu, Changhong
Cc: r-help at r-project.org
Subject: Re: [R] numeric equality
On Wed, Mar 18, 2009 at 8:58 AM, Yu, Changhong <YuC at ccf.org> wrote:
This is expected behaviour.  The bottom line is that you shouldn't
rely on the equality operator when dealing with floating point values
in any numerical software, including R.

I write about this in detail at:

http://blog.revolution-computing.com/2009/03/when-is-a-zero-not-a-zero.h
tml

which will hopefully clear up your questions.

# David Smith
1 day later
#
On Wed, 2009-03-18 at 11:58 -0400, Yu, Changhong wrote:
Hi Yu,

First of all read FAQ 7.31 (Why doesn't R think these numbers are
equal?)

Second, in this case, use all.equal

a <- 1 - 0.8

a == 0.2
[1] FALSE

all.equal(a,0.2)
[1] TRUE