|
Hello R Fans,
Another question for the community that really frightened me today. The following logical comparison produces a "false" as output: t = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,1,-1,-1,1)) tt = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,-1,1,1,-1)) t == tt This is really strange behavior. Most likely this has something to do how R represents numbers internally and the possible sensitivity of a computer? Does anyone know when this strange behavior occurs and how to fix it? Thank you all! This list is pleasure!!! Marc ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
Marc Jekel <feuerwald <at> gmx.de> writes:
> Another question for the community that really frightened me today. The > following logical comparison produces a "false" as output: > > t = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,1,-1,-1,1)) > tt = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,-1,1,1,-1)) > > t == tt > > This is really strange behavior. Most likely this has something to do > how R represents numbers internally and the possible sensitivity of a > computer? Does anyone know when this strange behavior occurs and how to > fix it? > > Thank you all! This list is pleasure!!! > > Marc > But, try all.equal(tt, t) [1] TRUE and see the R FAQ 7.31 -- Ken Knoblauch Inserm U846 Stem-cell and Brain Research Institute Department of Integrative Neurosciences 18 avenue du Doyen Lépine 69500 Bron France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: +33 (0)6 84 10 64 10 http://www.sbri.fr/members/kenneth-knoblauch.html ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by kognDisso
On Sun, Jan 23, 2011 at 11:13:11PM +0100, Marc Jekel wrote:
> Hello R Fans, > > Another question for the community that really frightened me today. The > following logical comparison produces a "false" as output: > > t = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,1,-1,-1,1)) > tt = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,-1,1,1,-1)) > > t == tt > > This is really strange behavior. Most likely this has something to do > how R represents numbers internally and the possible sensitivity of a > computer? Does anyone know when this strange behavior occurs and how to > fix it? The number 0.7 has infinite expansion in binary 0.1011001100110011001100110011... so is rounded in the standard numeric data type, which is used for speed needed in complex computations. If you know in advance that the result has at most 2 decimal positions, then round(, digits=2) yields the correct comparison round(t, 2) == round(tt, 2) # [1] TRUE athough 0.2 is also not exactly representable. Both sides are rounded to the same representable number. See also http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy for other examples. Petr Savicky. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by kognDisso
Hi again,
I have checked the same code (see below) using MATLAB. It produces the same error (i.e., equal numbers are evaluated as unequal). Do I miss something? Thanks for help! Marc Marc Jekel schrieb: > Hello R Fans, > > Another question for the community that really frightened me today. > The following logical comparison produces a "false" as output: > > t = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,1,-1,-1,1)) > tt = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,-1,1,1,-1)) > > t == tt > > This is really strange behavior. Most likely this has something to do > how R represents numbers internally and the possible sensitivity of a > computer? Does anyone know when this strange behavior occurs and how > to fix it? > > Thank you all! This list is pleasure!!! > > Marc > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
Marc
You have been given the answer already and a solution. See the R FAQ 7.31. As you have discovered this issue is not specific to R. In order to eliminate this "problem" entirely, you will need a computer system with infinite precision. Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204 > -----Original Message----- > From: [hidden email] [mailto:r-help-bounces@r- > project.org] On Behalf Of Marc Jekel > Sent: Monday, January 24, 2011 11:48 AM > Cc: [hidden email] > Subject: Re: [R] sensitivity logical operators in R > > Hi again, > > I have checked the same code (see below) using MATLAB. It produces the > same error (i.e., equal numbers are evaluated as unequal). Do I miss > something? > > Thanks for help! > > Marc > > Marc Jekel schrieb: > > Hello R Fans, > > > > Another question for the community that really frightened me today. > > The following logical comparison produces a "false" as output: > > > > t = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,1,-1,-1,1)) > > tt = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,-1,1,1,-1)) > > > > t == tt > > > > This is really strange behavior. Most likely this has something to do > > how R represents numbers internally and the possible sensitivity of a > > computer? Does anyone know when this strange behavior occurs and how > > to fix it? > > > > Thank you all! This list is pleasure!!! > > > > Marc > > > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by kognDisso
1. It is NOT an error 2. The numbers are NOT equal 3. Please read FAQ 7.31. 3. Do t - tt and you will see something like [1] -2.220446e-16 as answer. Berend |
| Powered by Nabble | Edit this page |
