Rainer Johannes
2016-Apr-05 11:34 UTC
[R] Problem with <= (less than or equal): not giving the expected result
Dear All, I have the following problem: I have a function in which I check if the difference between values is smaller or equal to a certain threshold. I however realized that I might get there some unexpected results:> abs(1 - 0.95) >= 0.05[1] TRUE ## So that?s fine, but:> abs(1 - 0.95) <= 0.05[1] FALSE Apparently, abs(1 - 0.95) is not equal to 0.05, which I find however quite disturbing. Along these lines:> abs(0.95 - 1) > 0.05[1] TRUE> abs(0.95 - 1) < 0.05[1] FALSE I guess that has to do with the floating point representation of the data? Is there something I miss or is there any solution to this? Thanks for any help! cheers, jo I tried this on different R-version (including 3.2.3 and 3.3.0 alpha); The R-version I used for the code above is:> sessionInfo()R version 3.0.2 (2013-09-25) Platform: x86_64-unknown-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base
Thierry Onkelinx
2016-Apr-05 12:30 UTC
[R] Problem with <= (less than or equal): not giving the expected result
FAQ 7.31 ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey 2016-04-05 13:34 GMT+02:00 Rainer Johannes <Johannes.Rainer at eurac.edu>:> Dear All, > > I have the following problem: > > I have a function in which I check if the difference between values is > smaller or equal to a certain threshold. I however realized that I might > get there some unexpected results: > > > abs(1 - 0.95) >= 0.05 > [1] TRUE > ## So that?s fine, but: > > abs(1 - 0.95) <= 0.05 > [1] FALSE > > Apparently, abs(1 - 0.95) is not equal to 0.05, which I find however quite > disturbing. > > Along these lines: > > abs(0.95 - 1) > 0.05 > [1] TRUE > > abs(0.95 - 1) < 0.05 > [1] FALSE > > I guess that has to do with the floating point representation of the data? > > Is there something I miss or is there any solution to this? > Thanks for any help! > > cheers, jo > > > > I tried this on different R-version (including 3.2.3 and 3.3.0 alpha); The > R-version I used for the code above is: > > > sessionInfo() > R version 3.0.2 (2013-09-25) > Platform: x86_64-unknown-linux-gnu (64-bit) > > locale: > [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C > [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 > [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 > [7] LC_PAPER=en_US.UTF-8 LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.[[alternative HTML version deleted]]
Adrian Dușa
2016-Apr-05 12:31 UTC
[R] Problem with <= (less than or equal): not giving the expected result
Yes, that does have to do with floating point representation. I use this function for these types of comparisons (works with values as well as with vectors): check.equal <- function(x, y) { check.vector <- as.logical(unlist(lapply(x, all.equal, y))) check.vector[is.na(check.vector)] <- FALSE return(check.vector) } See: ?all.equal Hth, Adrian On Tue, Apr 5, 2016 at 2:34 PM, Rainer Johannes <Johannes.Rainer at eurac.edu> wrote:> Dear All, > > I have the following problem: > > I have a function in which I check if the difference between values is > smaller or equal to a certain threshold. I however realized that I might > get there some unexpected results: > > > abs(1 - 0.95) >= 0.05 > [1] TRUE > ## So that?s fine, but: > > abs(1 - 0.95) <= 0.05 > [1] FALSE > > Apparently, abs(1 - 0.95) is not equal to 0.05, which I find however quite > disturbing. > > Along these lines: > > abs(0.95 - 1) > 0.05 > [1] TRUE > > abs(0.95 - 1) < 0.05 > [1] FALSE > > I guess that has to do with the floating point representation of the data? > > Is there something I miss or is there any solution to this? > Thanks for any help! > > cheers, jo > > > > I tried this on different R-version (including 3.2.3 and 3.3.0 alpha); The > R-version I used for the code above is: > > > sessionInfo() > R version 3.0.2 (2013-09-25) > Platform: x86_64-unknown-linux-gnu (64-bit) > > locale: > [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C > [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 > [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 > [7] LC_PAPER=en_US.UTF-8 LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Adrian Dusa University of Bucharest Romanian Social Data Archive Soseaua Panduri nr.90 050663 Bucharest sector 5 Romania [[alternative HTML version deleted]]
Rainer Johannes
2016-Apr-05 12:46 UTC
[R] Problem with <= (less than or equal): not giving the expected result
Thanks Adrian and Thierry (from the previous answer). I was aware of the all.equal function, but there is nothing similar for <= (e.g. all.smallerEqual)? cheers, jo On 05 Apr 2016, at 14:31, Adrian Du?a <dusa.adrian at unibuc.ro<mailto:dusa.adrian at unibuc.ro>> wrote: Yes, that does have to do with floating point representation. I use this function for these types of comparisons (works with values as well as with vectors): check.equal <- function(x, y) { check.vector <- as.logical(unlist(lapply(x, all.equal, y))) check.vector[is.na<http://is.na/>(check.vector)] <- FALSE return(check.vector) } See: ?all.equal Hth, Adrian On Tue, Apr 5, 2016 at 2:34 PM, Rainer Johannes <Johannes.Rainer at eurac.edu<mailto:Johannes.Rainer at eurac.edu>> wrote: Dear All, I have the following problem: I have a function in which I check if the difference between values is smaller or equal to a certain threshold. I however realized that I might get there some unexpected results:> abs(1 - 0.95) >= 0.05[1] TRUE ## So that?s fine, but:> abs(1 - 0.95) <= 0.05[1] FALSE Apparently, abs(1 - 0.95) is not equal to 0.05, which I find however quite disturbing. Along these lines:> abs(0.95 - 1) > 0.05[1] TRUE> abs(0.95 - 1) < 0.05[1] FALSE I guess that has to do with the floating point representation of the data? Is there something I miss or is there any solution to this? Thanks for any help! cheers, jo I tried this on different R-version (including 3.2.3 and 3.3.0 alpha); The R-version I used for the code above is:> sessionInfo()R version 3.0.2 (2013-09-25) Platform: x86_64-unknown-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base ______________________________________________ R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. -- Adrian Dusa University of Bucharest Romanian Social Data Archive Soseaua Panduri nr.90 050663 Bucharest sector 5 Romania [[alternative HTML version deleted]]
S Ellison
2016-Apr-06 10:34 UTC
[R] Problem with <= (less than or equal): not giving the expected result
> Apparently, abs(1 - 0.95) is not equal to 0.05, which I find however quite > disturbing.It's normal.* See R FAQ 7.31 in the html help system. S Ellison *... and common to all computers that use binary. ******************************************************************* This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
PIKAL Petr
2016-Apr-07 08:43 UTC
[R] Problem with <= (less than or equal): not giving the expected result
Hi> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of S Ellison > Sent: Wednesday, April 6, 2016 12:35 PM > To: Rainer Johannes <Johannes.Rainer at eurac.edu>; r-help at r-project.org > Subject: Re: [R] Problem with <= (less than or equal): not giving the expected > result > > > Apparently, abs(1 - 0.95) is not equal to 0.05, which I find however > > quite disturbing. > > It's normal.* See R FAQ 7.31 in the html help system. > > S Ellison > > *... and common to all computers that use binary.The main issue is that when using Excel you do not experience normally such issues, probably because Excel silently decide how to express precision of decimal values. =+ABS(1-0.995) is equal to 0.005, however =+ABS(1-0.9995) is not equal to 0.0005 So it when Excel (Excel 2016) users do not use numbers smaller than 0.005 they do not find this behaviour. Cheers Petr> > > > ********************************************************** > ********* > This email and any attachments are confidential. Any use, copying or > disclosure other than by the intended recipient is unauthorised. If you have > received this message in error, please notify the sender immediately via > +44(0)20 8943 7000 or notify postmaster at lgcgroup.com and delete this > message and any copies from your computer and network. > LGC Limited. Registered in England 2991879. > Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
Reasonably Related Threads
- Problem with <= (less than or equal): not giving the expected result
- Problem with <= (less than or equal): not giving the expected result
- Problem with <= (less than or equal): not giving the expected result
- Problem with <= (less than or equal): not giving the expected result
- formula argument evaluation