Dear All, I have a seemingly very simple question, but I just cannot figure out the answer. I attempted to run the following:a=0.1*(1:9);which(a==0.3);it returns integer(0). But obviously, the third element of a is equal to 0.3. I must have missed something. Can someone kindly explain why? Thanks a lot. Regards,Dajiang [[alternative HTML version deleted]] ______________________________________________ [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. |
On Mar 18, 2012, at 4:43 PM, Dajiang Liu wrote: > > Dear All, > I have a seemingly very simple question, but I just cannot figure > out the answer. I attempted to run the > following:a=0.1*(1:9);which(a==0.3);it returns integer(0). But > obviously, the third element of a is equal to 0.3. > I must have missed something. Can someone kindly explain why? Thanks > a lot. It has already been explained on this list ... "frequently" in FAQt. Locate the FAQ and search for a question about why R doesn't think two numbers are equal. The FAQ should be part of a standard instalL on the main help page. > Regards,Dajiang > > [[alternative HTML version deleted]] > > ______________________________________________ > [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. David Winsemius, MD Heritage Laboratories West Hartford, CT ______________________________________________ [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 Dajiang Liu
As to the reasons, David as given you the necessary hints.
In order to get around the issue, here is what I do: > a <- round( 0.1 * ( 1:9 ), 1 ) > a [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 > which( a == 0.3 ) [1] 3 Rgds, Rainer -------- Original-Nachricht -------- > Datum: Sun, 18 Mar 2012 21:43:54 +0000 > Von: Dajiang Liu <[hidden email]> > An: [hidden email] > Betreff: [R] a very simple question > > Dear All, > I have a seemingly very simple question, but I just cannot figure out the > answer. I attempted to run the following:a=0.1*(1:9);which(a==0.3);it > returns integer(0). But obviously, the third element of a is equal to 0.3. > I must have missed something. Can someone kindly explain why? Thanks a > lot. > Regards,Dajiang > > [[alternative HTML version deleted]] > > ______________________________________________ > [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. -- ------- Gentoo Linux with KDE ______________________________________________ [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 Dajiang Liu
On Sun, Mar 18, 2012 at 09:43:54PM +0000, Dajiang Liu wrote:
> > Dear All, > I have a seemingly very simple question, but I just cannot figure out the answer. I attempted to run the following:a=0.1*(1:9);which(a==0.3);it returns integer(0). But obviously, the third element of a is equal to 0.3. > I must have missed something. Can someone kindly explain why? Thanks a lot. Hi. A simple way to detect rounding problems is subtracting the numbers. a = 0.1*(1:4) a - 0.3 [1] -2.000000e-01 -1.000000e-01 5.551115e-17 1.000000e-01 Use rounding to avoid it as suggested by others. 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 Rainer Schuermann
Thanks a lot for the clarification. I just find it very bizarre that if you run a=0.1*(1:9);which(a==0.4) it returns the right answer. Anyway, I will pay attention next time. Thanks a lot. > Date: Mon, 19 Mar 2012 08:59:59 +0100 > From: [hidden email] > Subject: Re: [R] a very simple question > To: [hidden email]; [hidden email] > > As to the reasons, David as given you the necessary hints. > > In order to get around the issue, here is what I do: > > > a <- round( 0.1 * ( 1:9 ), 1 ) > > a > [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 > > which( a == 0.3 ) > [1] 3 > > Rgds, > Rainer > > > -------- Original-Nachricht -------- > > Datum: Sun, 18 Mar 2012 21:43:54 +0000 > > Von: Dajiang Liu <[hidden email]> > > An: [hidden email] > > Betreff: [R] a very simple question > > > > > Dear All, > > I have a seemingly very simple question, but I just cannot figure out the > > answer. I attempted to run the following:a=0.1*(1:9);which(a==0.3);it > > returns integer(0). But obviously, the third element of a is equal to 0.3. > > I must have missed something. Can someone kindly explain why? Thanks a > > lot. > > Regards,Dajiang > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > [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. > > -- > ------- > > Gentoo Linux with KDE > [[alternative HTML version deleted]] ______________________________________________ [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. |
On 19-03-2012, at 13:47, Dajiang Liu wrote: > > Thanks a lot for the clarification. I just find it very bizarre that if you run a=0.1*(1:9);which(a==0.4) > it returns the right answer. Anyway, I will pay attention next time. Thanks a lot. > Look at > a = 0.1*(1:4) > a - 0.4 [1] -0.3 -0.2 -0.1 0.0 > Berend >> Date: Mon, 19 Mar 2012 08:59:59 +0100 >> From: [hidden email] >> Subject: Re: [R] a very simple question >> To: [hidden email]; [hidden email] >> >> As to the reasons, David as given you the necessary hints. >> >> In order to get around the issue, here is what I do: >> >>> a <- round( 0.1 * ( 1:9 ), 1 ) >>> a >> [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 >>> which( a == 0.3 ) >> [1] 3 >> >> Rgds, >> Rainer >> >> >> -------- Original-Nachricht -------- >>> Datum: Sun, 18 Mar 2012 21:43:54 +0000 >>> Von: Dajiang Liu <[hidden email]> >>> An: [hidden email] >>> Betreff: [R] a very simple question >> >>> >>> Dear All, >>> I have a seemingly very simple question, but I just cannot figure out the >>> answer. I attempted to run the following:a=0.1*(1:9);which(a==0.3);it >>> returns integer(0). But obviously, the third element of a is equal to 0.3. >>> I must have missed something. Can someone kindly explain why? Thanks a >>> lot. >>> Regards,Dajiang >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> [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. >> >> -- >> ------- >> >> Gentoo Linux with KDE >> > > [[alternative HTML version deleted]] > > ______________________________________________ > [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 Dajiang Liu
On 19-Mar-2012 Dajiang Liu wrote:
> Thanks a lot for the clarification. I just find it very bizarre > that if you run > a=0.1*(1:9);which(a==0.4) > it returns the right answer. Anyway, I will pay attention next time. > Thanks a > lot. The basic explanation is that, for an integer r (0<r<10), what is stored in binary representation by R for 0.1*r or for "0.r" or for r/10 is always an approximation to the exact value (with the possible exception of r=5). The exact detail of the binary representation may depend on how it was obtained, by any of several different methods of calculation which, mathematically, are exactly equivalent but, in the binary representations stored in the computer, may be slightly different. Examples: 0.1*(1:9) - (1:9)/10 # [1] 0.000000e+00 0.000000e+00 5.551115e-17 0.000000e+00 # [5] 0.000000e+00 1.110223e-16 1.110223e-16 0.000000e+00 # [8] 0.000000e+00 0.1*(1:9) - c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9) # [1] 0.000000e+00 0.000000e+00 5.551115e-17 0.000000e+00 # [5] 0.000000e+00 1.110223e-16 1.110223e-16 0.000000e+00 # [8] 0.000000e+00 # (1:9)/10 - c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9) # [1] 0 0 0 0 0 0 0 0 0 cumsum(rep(0.3,9))/3 - (1:9)/10 # [1] -1.387779e-17 -2.775558e-17 0.000000e+00 -5.551115e-17 # [5] 0.000000e+00 0.000000e+00 1.110223e-16 -1.110223e-16 # [9] -1.110223e-16 and so on ... The third example suggests that when R is given a decimal fraction "0.r" it recognises that this is equivalent to r/10 and calculates it accordingly, hence the agreement between (1:9)/10 and c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9). (I would need to check the source code to verify that statement, however). The short answer (as has been pointed out) is that you cannot count on exact agreement, within R (or most other numerical software), between a value calculated by one numerical method and the value calculated by another numerical method which is mathematically equivalent. Some numerical software will work by storing the expression given to it not as a number but as a sequence of operations performed on given digits, only evaluating this at the last moment along with other similar expressions, working within the scale (e.g. decimal scale for numbers given like "123.456") thus obtaining maximum accuracy within the allocated storage. An example it the arbitrary-precision calculator 'bc'. Many (most?) hand-held digital calculators work to an internal decimal representation such as BCD ("binary-coded decimal") where each byte is split into two half-bytes of 4 binary digits, each capable of storing a number from 0 to 9; then they can perform exact decimal arithmetic (to within the precision of storage) for decimal numbers, avoiding the imprecision resulting from conversion to binary (but may exhibit similar problems to the above for binary input). Ted. ------------------------------------------------- E-Mail: (Ted Harding) <[hidden email]> Date: 19-Mar-2012 Time: 15:02:03 This message was sent by XFMail ______________________________________________ [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 Dajiang Liu
On Mon, Mar 19, 2012 at 12:47:12PM +0000, Dajiang Liu wrote:
> > Thanks a lot for the clarification. I just find it very bizarre that if you run a=0.1*(1:9);which(a==0.4) > it returns the right answer. Anyway, I will pay attention next time. Thanks a lot. Hi. Yes, these things are bizarre sometimes. Compare print(0.1, digits=20) # [1] 0.10000000000000000555 print(4*0.1, digits=20) # [1] 0.4000000000000000222 print(0.4, digits=20) # [1] 0.4000000000000000222 Equality of the last two is the reason for which(0.1*(1:9) == 0.4) [1] 4 while for 0.3, we get print(3*0.1, digits=20) # [1] 0.30000000000000004441 print(0.3, digits=20) # [1] 0.2999999999999999889 See http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy for further hints. 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. |
Free forum by Nabble | Edit this page |