Dear userR, With the following results, are they correct or acceptable?> x <- c(1.4, 1.2, 2.8) > sum(x)[1] 5.4> sum(x) == 5.4[1] FALSE> (1.4 + 1.2 + 2.8) - 5.4[1] -8.881784e-16> (1.4 + 1.2) - 2.6[1] -4.440892e-16> 2.6 - 1.5 - 1.1[1] 0> version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 2.0 year 2005 month 10 day 06 svn rev 35749 language R What can I do to correct them if they are not correct? Thanks! -- C. Joseph Lu Department of Statistics National Cheng-Kung University Tainan, Taiwan, ROC
See the R FAQ list, section 7. Why doesn't R think these numbers are equal? Ted. On 11/10/05 15:42, Joe wrote,:> Dear userR, > > With the following results, are they correct or acceptable? > >>x <- c(1.4, 1.2, 2.8) >>sum(x) > [1] 5.4 >>sum(x) == 5.4 > [1] FALSE >>(1.4 + 1.2 + 2.8) - 5.4 > [1] -8.881784e-16 >>(1.4 + 1.2) - 2.6 > [1] -4.440892e-16 >>2.6 - 1.5 - 1.1 > [1] 0 > >>version > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 2.0 > year 2005 > month 10 > day 06 > svn rev 35749 > language R > > What can I do to correct them if they are not correct? > Thanks! > -- > C. Joseph Lu > Department of Statistics > National Cheng-Kung University > Tainan, Taiwan, ROC > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- Dr E.A. Catchpole Visiting Fellow Univ of New South Wales at ADFA, Canberra, Australia and University of Kent, Canterbury, England - www.ma.adfa.edu.au/~eac - fax: +61 2 6268 8786 - ph: +61 2 6268 8895
Joe a ??crit :> Dear userR, > With the following results, are they correct or acceptable? > What can I do to correct them if they are not correct?http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f see also ?round hih
Joe wrote:> Dear userR, > > With the following results, are they correct or acceptable?Yes. See the FAQ: 7.31 Why doesn't R think these numbers are equal? The only numbers that can be represented exactly in R's numeric type are integers and fractions whose denominator is a power of 2. Other numbers have to be rounded to (typically) 53 binary digits accuracy. As a result, two floating point numbers will not reliably be equal unless they have been computed by the same algorithm, and not always even then. For example R> a <- sqrt(2) R> a * a == 2 [1] FALSE R> a * a - 2 [1] 4.440892e-16 The function all.equal() compares two objects using a numeric tolerance of .Machine$double.eps ^ 0.5. If you want much greater accuracy than this you will need to consider error propagation carefully. For more information, see e.g. David Goldberg (1991), ?What Every Computer Scientist Should Know About Floating-Point Arithmetic?, ACM Computing Surveys, 23/1, 5?48, also available via http://docs.sun.com/source/806-3568/ncg_goldberg.html.> > >>x <- c(1.4, 1.2, 2.8) >>sum(x) > > [1] 5.4 > >>sum(x) == 5.4 > > [1] FALSE > >>(1.4 + 1.2 + 2.8) - 5.4 > > [1] -8.881784e-16 > >>(1.4 + 1.2) - 2.6 > > [1] -4.440892e-16 > >>2.6 - 1.5 - 1.1 > > [1] 0 > > >>version > > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 2.0 > year 2005 > month 10 > day 06 > svn rev 35749 > language R > > What can I do to correct them if they are not correct? > Thanks! > -- > C. Joseph Lu > Department of Statistics > National Cheng-Kung University > Tainan, Taiwan, ROC > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html