Hello, I found something strange and maybe I am going nuts but this does not make sense:> (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1) > 1[1] TRUE I tried it on my mac M1 (R v4.1.2) and my Linux box (R v4.0.4). If I use other values, it does not work (see below). It seems only that combination, granted I did not try them all.> (0.4 + 0.2 + 0 + 0.2 + 0 + 0.2) > 1[1] FALSE Am I missing something? Cheers -nb [[alternative HTML version deleted]]
This looks like a version of FAQ 7.31.> On Feb 1, 2022, at 8:45 AM, Nathan Boeger <nboeger at gmail.com> wrote: > > [You don't often get email from nboeger at gmail.com. Learn why this is important at http://aka.ms/LearnAboutSenderIdentification.] > > Hello, > > I found something strange and maybe I am going nuts but this does not make > sense: > >> (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1) > 1 > [1] TRUE > > I tried it on my mac M1 (R v4.1.2) and my Linux box (R v4.0.4). If I use > other values, it does not work (see below). It seems only that combination, > granted I did not try them all. > >> (0.4 + 0.2 + 0 + 0.2 + 0 + 0.2) > 1 > [1] FALSE > > Am I missing something? > > Cheers > > -nb > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Kevin E. Thorpe Head of Biostatistics, Applied Health Research Centre (AHRC) Li Ka Shing Knowledge Institute of St. Michael?s Hospital Assistant Professor, Dalla Lana School of Public Health University of Toronto email: kevin.thorpe at utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016
This a joke? The values differ between the TRUE and FALSE syntaxes. The FALSE computes to 1.0, which is not greater than 0. Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows From: Nathan Boeger<mailto:nboeger at gmail.com> Sent: Tuesday, February 1, 2022 12:28 PM To: r-help at r-project.org<mailto:r-help at r-project.org> Subject: [R] Funky calculations Hello, I found something strange and maybe I am going nuts but this does not make sense:> (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1) > 1[1] TRUE I tried it on my mac M1 (R v4.1.2) and my Linux box (R v4.0.4). If I use other values, it does not work (see below). It seems only that combination, granted I did not try them all.> (0.4 + 0.2 + 0 + 0.2 + 0 + 0.2) > 1[1] FALSE Am I missing something? Cheers -nb [[alternative HTML version deleted]] ______________________________________________ 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]]
On Tue, 1 Feb 2022 20:45:40 +0700 Nathan Boeger <nboeger at gmail.com> wrote:> I found something strange and maybe I am going nuts but this does not > make sense: > > > (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1) > 1 > [1] TRUEUnfortunately, this always happens when computers approximate real numbers with fractions in binary. See R FAQ 7.31 (RShowDoc('FAQ') or <https://cran.r-project.org/doc/FAQ/R-FAQ.html>). -- Best regards, Ivan
Sorry, last post I meant not > 1 Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows From: Nathan Boeger<mailto:nboeger at gmail.com> Sent: Tuesday, February 1, 2022 12:28 PM To: r-help at r-project.org<mailto:r-help at r-project.org> Subject: [R] Funky calculations Hello, I found something strange and maybe I am going nuts but this does not make sense:> (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1) > 1[1] TRUE I tried it on my mac M1 (R v4.1.2) and my Linux box (R v4.0.4). If I use other values, it does not work (see below). It seems only that combination, granted I did not try them all.> (0.4 + 0.2 + 0 + 0.2 + 0 + 0.2) > 1[1] FALSE Am I missing something? Cheers -nb [[alternative HTML version deleted]] ______________________________________________ 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]]
Yes, Nathan, you are missing something. You are missing a computer language that does infinite precision arithmetic. 0.1 cannot be stored in binary exactly just as numbers like pi and e cannot be stored exactly as they keep needing more digits. The last digit of the allowed number of digits in floating point may have to be chosen as either a 0 or 1 even though the value is sort of in between. Think of what happens if you add in decimal 1/3 + 1/3 + 1/3 You could add .333 .333 .333 And your handwritten calculation would be .999 And no matter how many more 3's you add, you never get beyond .999999999999999999999999999999999 And obviously the right answer is 1.0000000000000000000000000000 The order the numbers are added can matter. You interspersed a combination of floating point and integers so conversions are happening too. R has the ability to test for very near equality like this:> all.equal(1, (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1))[1] TRUE There are some built-in and sort of hidden variables stored in .Machine that provide some tolerances for the data storage on that machine.> str(.Machine)List of 28 $ double.eps : num 2.22e-16 $ double.neg.eps : num 1.11e-16 $ double.xmin : num 2.23e-308 $ double.xmax : num 1.8e+308 $ double.base : int 2 $ double.digits : int 53 $ double.rounding : int 5 $ double.guard : int 0 $ double.ulp.digits : int -52 $ double.neg.ulp.digits : int -53 $ double.exponent : int 11 $ double.min.exp : int -1022 $ double.max.exp : int 1024 $ integer.max : int 2147483647 $ sizeof.long : int 4 $ sizeof.longlong : int 8 $ sizeof.longdouble : int 16 $ sizeof.pointer : int 8 $ longdouble.eps : num 1.08e-19 $ longdouble.neg.eps : num 5.42e-20 $ longdouble.digits : int 64 $ longdouble.rounding : int 5 $ longdouble.guard : int 0 $ longdouble.ulp.digits : int -63 $ longdouble.neg.ulp.digits: int -64 $ longdouble.exponent : int 15 $ longdouble.min.exp : int -16382 $ longdouble.max.exp : int 16384 Functions like all.equal() can use these to determine a way to compare while ignoring the last. So yes, R shares this glitch with other programming languages and it is not really safe to compare floating point numbers at times. Here is just a suggestion on say rounding your sum to twelve digits to see how it compares to 1.0:> round((0.4 + 0.2 + 0 + 0.3 + 0 + 0.1),12) > 1.0[1] FALSE -----Original Message----- From: Nathan Boeger <nboeger at gmail.com> To: r-help at r-project.org Sent: Tue, Feb 1, 2022 8:45 am Subject: [R] Funky calculations Hello, I found something strange and maybe I am going nuts but this does not make sense:>? (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1) > 1[1] TRUE I tried it on my mac M1 (R v4.1.2) and my Linux box (R v4.0.4). If I use other values, it does not work (see below). It seems only that combination, granted I did not try them all.>? (0.4 + 0.2 + 0 + 0.2 + 0 + 0.2) > 1[1] FALSE Am I missing something? Cheers -nb ??? [[alternative HTML version deleted]] ______________________________________________ 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.
Hello, Like others have said, this is FAQ 7.31. Try (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1) - 1 on both systems, on one of them it's not zero. Hope this helps, Rui Barradas ?s 13:45 de 01/02/2022, Nathan Boeger escreveu:> Hello, > > I found something strange and maybe I am going nuts but this does not make > sense: > >> (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1) > 1 > [1] TRUE > > I tried it on my mac M1 (R v4.1.2) and my Linux box (R v4.0.4). If I use > other values, it does not work (see below). It seems only that combination, > granted I did not try them all. > >> (0.4 + 0.2 + 0 + 0.2 + 0 + 0.2) > 1 > [1] FALSE > > Am I missing something? > > Cheers > > -nb > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
I have not looked into this, but maybe this is related to floating point rounding? Kindest regards, Stephen Sefick, PhD On Tue, Feb 1, 2022, 11:28 Nathan Boeger <nboeger at gmail.com> wrote:> Hello, > > I found something strange and maybe I am going nuts but this does not make > sense: > > > (0.4 + 0.2 + 0 + 0.3 + 0 + 0.1) > 1 > [1] TRUE > > I tried it on my mac M1 (R v4.1.2) and my Linux box (R v4.0.4). If I use > other values, it does not work (see below). It seems only that combination, > granted I did not try them all. > > > (0.4 + 0.2 + 0 + 0.2 + 0 + 0.2) > 1 > [1] FALSE > > Am I missing something? > > Cheers > > -nb > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]