Dear all, I am totally confused by the following R output, but don't have a clue for it.> a <- 1 - 0.2> a == 0.8[1] TRUE> a <- 1 - 0.8> a == 0.2[1] FALSE> a <- 1 - 0.5> a == 0.5[1] TRUE> a <- 1 - 0.6> a == 0.4[1] TRUE> a <- 1 - 0.9> a == 0.1[1] FALSE My R version is Windows XP R version 2.8.1 (2008-12-22). Thanks very much. Changhong ================================== P Please consider the environment before printing this e-mail Cleveland Clinic is ranked one of the top hospitals in America by U.S. News & World Report (2008). Visit us online at http://www.clevelandclinic.org for a complete listing of our services, staff and locations. Confidentiality Note: This message is intended for use\...{{dropped:16}}
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:> Dear all, > > > > I am totally confused by the following R output, but don't have a clue > for it. > > > >> a <- 1 - 0.2 > >> a == 0.8 > > [1] TRUE > >> a <- 1 - 0.8 > >> a == 0.2 > > [1] FALSE > >> a <- 1 - 0.5 > >> a == 0.5 > > [1] TRUE > >> a <- 1 - 0.6 > >> a == 0.4 > > [1] TRUE > >> a <- 1 - 0.9 > >> a == 0.1 > > [1] FALSE > > > > > > My R version is Windows XP R version 2.8.1 (2008-12-22). > > > > Thanks very much. > > > > > > Changhong > >-- Sarah Goslee http://www.functionaldiversity.org
On Wed, Mar 18, 2009 at 8:58 AM, Yu, Changhong <YuC at ccf.org> wrote:> Dear all, > I am totally confused by the following R output, but don't have a clue > for it. > >> a <- 1 - 0.2 > >> a == 0.8 > > [1] TRUE > >> a <- 1 - 0.8 > >> a == 0.2 > > [1] FALSEThis 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 -- David M Smith <david at revolution-computing.com> Director of Community, REvolution Computing www.revolution-computing.com Tel: +1 (206) 577-4778 x3203 (Seattle, USA) Check out our upcoming events schedule at www.revolution-computing.com/events
On Wed, 2009-03-18 at 11:58 -0400, Yu, Changhong wrote:> Dear all, > > > > I am totally confused by the following R output, but don't have a clue > for it. > > > > > a <- 1 - 0.2 > > > a == 0.8 > > [1] TRUE > > > a <- 1 - 0.8 > > > a == 0.2 > > [1] FALSEHi 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 -- Bernardo Rangel Tura, M.D,MPH,Ph.D National Institute of Cardiology Brazil
Seemingly Similar Threads
- How to generate a prediction equation for a stratified survival model that was fitted by cph() in Design package
- Identifying graphics files produced by R
- News on R "s largest corporate partner REVolution Computing and SPSS CEO:
- does R plus 3.3 need vista ?
- multicore - no parallel