james.foadi at diamond.ac.uk
2010-Jun-09 11:16 UTC
[R] strange issue with "which" on "seq"
Dear R community, I am puzzled by the following lines:> v <- seq(-0.5,0.5,by=0.1) > v[1] -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5> which(v == -0.4)[1] 2> which(v == 0)[1] 6> which(v == 0.1)integer(0)> which(v == 0.2)integer(0)> which(v == 0.3)integer(0)> which(v == 0.4)[1] 10> which(v == 0.5)[1] 11 Why "which" can only match some of the values in "v"? Are the numbers generated by "seq" not exact fractional numbers? Please, help me to understand this. J Dr James Foadi PhD Membrane Protein Laboratory (MPL) Diamond Light Source Ltd Diamond House Harewell Science and Innovation Campus Chilton, Didcot Oxfordshire OX11 0DE Email : james.foadi at diamond.ac.uk Alt Email: j.foadi at imperial.ac.uk -- This e-mail and any attachments may contain confidential...{{dropped:8}}
Hi r-help-bounces at r-project.org napsal dne 09.06.2010 13:16:40:> Dear R community, > I am puzzled by the following lines: > > > v <- seq(-0.5,0.5,by=0.1) > > v > [1] -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 > > which(v == -0.4) > [1] 2 > > which(v == 0) > [1] 6 > > which(v == 0.1) > integer(0) > > which(v == 0.2) > integer(0) > > which(v == 0.3) > integer(0) > > which(v == 0.4) > [1] 10 > > which(v == 0.5) > [1] 11 > > Why "which" can only match some of the values in "v"? Are the numbers > generated by "seq" not exact fractional numbers? > Please, help me to understand this.Well FAQ 7.31 was not here some time. Computing in binary results in finite precision of fractional numbers. v <- seq(-0.5,0.5,by=0.1) v[7]-0.1 [1] 8.326673e-17 Regards Petr> > J > > Dr James Foadi PhD > Membrane Protein Laboratory (MPL) > Diamond Light Source Ltd > Diamond House > Harewell Science and Innovation Campus > Chilton, Didcot > Oxfordshire OX11 0DE > > Email : james.foadi at diamond.ac.uk > Alt Email: j.foadi at imperial.ac.uk > > -- > This e-mail and any attachments may contain confidential...{{dropped:8}} > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
which(abs(v - .1) <= .Machine$double.eps) seems to me too cumbersome to write. Any other easier way? all.equal does not quite work Nikhil On Jun 9, 2010, at 7:54 AM, Petr PIKAL wrote:> Hi > > r-help-bounces at r-project.org napsal dne 09.06.2010 13:16:40: > >> Dear R community, >> I am puzzled by the following lines: >> >>> v <- seq(-0.5,0.5,by=0.1) >>> v >> [1] -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 >>> which(v == -0.4) >> [1] 2 >>> which(v == 0) >> [1] 6 >>> which(v == 0.1) >> integer(0) >>> which(v == 0.2) >> integer(0) >>> which(v == 0.3) >> integer(0) >>> which(v == 0.4) >> [1] 10 >>> which(v == 0.5) >> [1] 11 >> >> Why "which" can only match some of the values in "v"? Are the numbers >> generated by "seq" not exact fractional numbers? >> Please, help me to understand this. > > Well FAQ 7.31 was not here some time. Computing in binary results in > finite precision of fractional numbers. > > v <- seq(-0.5,0.5,by=0.1) > v[7]-0.1 > [1] 8.326673e-17 > > Regards > Petr > > > >> >> J >> >> Dr James Foadi PhD >> Membrane Protein Laboratory (MPL) >> Diamond Light Source Ltd >> Diamond House >> Harewell Science and Innovation Campus >> Chilton, Didcot >> Oxfordshire OX11 0DE >> >> Email : james.foadi at diamond.ac.uk >> Alt Email: j.foadi at imperial.ac.uk >> >> -- >> This e-mail and any attachments may contain confidential... >> {{dropped:8}} >> >> ______________________________________________ >> R-help at r-project.org 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. > > ______________________________________________ > R-help at r-project.org 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.
Hi Well, then please explain what you really want to do. I use such fractional sequences only for evaluation of some models and in that case this finite precision issue is not important. Regards Petr r-help-bounces at r-project.org napsal dne 09.06.2010 14:20:16:> which(abs(v - .1) <= .Machine$double.eps) > > seems to me too cumbersome to write. Any other easier way? all.equal > does not quite work > > Nikhil > > On Jun 9, 2010, at 7:54 AM, Petr PIKAL wrote: > > > Hi > > > > r-help-bounces at r-project.org napsal dne 09.06.2010 13:16:40: > > > >> Dear R community, > >> I am puzzled by the following lines: > >> > >>> v <- seq(-0.5,0.5,by=0.1) > >>> v > >> [1] -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 > >>> which(v == -0.4) > >> [1] 2 > >>> which(v == 0) > >> [1] 6 > >>> which(v == 0.1) > >> integer(0) > >>> which(v == 0.2) > >> integer(0) > >>> which(v == 0.3) > >> integer(0) > >>> which(v == 0.4) > >> [1] 10 > >>> which(v == 0.5) > >> [1] 11 > >> > >> Why "which" can only match some of the values in "v"? Are the numbers > >> generated by "seq" not exact fractional numbers? > >> Please, help me to understand this. > > > > Well FAQ 7.31 was not here some time. Computing in binary results in > > finite precision of fractional numbers. > > > > v <- seq(-0.5,0.5,by=0.1) > > v[7]-0.1 > > [1] 8.326673e-17 > > > > Regards > > Petr > > > > > > > >> > >> J > >> > >> Dr James Foadi PhD > >> Membrane Protein Laboratory (MPL) > >> Diamond Light Source Ltd > >> Diamond House > >> Harewell Science and Innovation Campus > >> Chilton, Didcot > >> Oxfordshire OX11 0DE > >> > >> Email : james.foadi at diamond.ac.uk > >> Alt Email: j.foadi at imperial.ac.uk > >> > >> -- > >> This e-mail and any attachments may contain confidential... > >> {{dropped:8}} > >> > >> ______________________________________________ > >> R-help at r-project.org 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. > > > > ______________________________________________ > > R-help at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
use ?round is the easier way v <- round(seq(-0.5,0.5,by=0.1),1) On Wed, Jun 9, 2010 at 2:20 PM, Nikhil Kaza <nikhil.list at gmail.com> wrote:> which(abs(v - .1) <= .Machine$double.eps) > > seems to me too cumbersome to write. Any other easier way? all.equal does > not quite work > > Nikhil > > On Jun 9, 2010, at 7:54 AM, Petr PIKAL wrote: > >> Hi >> >> r-help-bounces at r-project.org napsal dne 09.06.2010 13:16:40: >> >>> Dear R community, >>> I am puzzled by the following lines: >>> >>>> v <- seq(-0.5,0.5,by=0.1) >>>> v >>> >>> ?[1] -0.5 -0.4 -0.3 -0.2 -0.1 ?0.0 ?0.1 ?0.2 ?0.3 ?0.4 ?0.5 >>>> >>>> which(v == -0.4) >>> >>> ?[1] 2 >>>> >>>> which(v == 0) >>> >>> ?[1] 6 >>>> >>>> which(v == 0.1) >>> >>> integer(0) >>>> >>>> which(v == 0.2) >>> >>> integer(0) >>>> >>>> which(v == 0.3) >>> >>> integer(0) >>>> >>>> which(v == 0.4) >>> >>> ?[1] 10 >>>> >>>> which(v == 0.5) >>> >>> ?[1] 11 >>> >>> Why "which" can only match some of the values in "v"? Are the numbers >>> generated by "seq" not exact fractional numbers? >>> Please, help me to understand this. >> >> Well FAQ 7.31 was not here some time. Computing in binary results in >> finite precision of fractional numbers. >> >> v <- seq(-0.5,0.5,by=0.1) >> v[7]-0.1 >> [1] 8.326673e-17 >> >> Regards >> Petr >> >> >> >>> >>> J >>> >>> Dr James Foadi PhD >>> Membrane Protein Laboratory (MPL) >>> Diamond Light Source Ltd >>> Diamond House >>> Harewell Science and Innovation Campus >>> Chilton, Didcot >>> Oxfordshire OX11 0DE >>> >>> Email ? ?: ?james.foadi at diamond.ac.uk >>> Alt Email: ?j.foadi at imperial.ac.uk >>> >>> -- >>> This e-mail and any attachments may contain confidential...{{dropped:8}} >>> >>> ______________________________________________ >>> R-help at r-project.org 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. >> >> ______________________________________________ >> R-help at r-project.org 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. > > ______________________________________________ > R-help at r-project.org 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. >-- Joris Meys Statistical consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control tel : +32 9 264 59 87 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php