Folks, I have got a strange behaviour when testing this: sum(x) != 1 let us set x<-c(70,134,1,5,0) and transform it in a vector of probabilities x<-x/sum(x) One expect sum(x) should be equal to 1, which is apparently the case> sum(x)[1] 1 However, when I try to test it I get:> if(sum(x) !=1) print("lost") else ("OK")[1] "lost" Which means that actually sum(x) is NOT considered equal to 1... Any idea about what is going wrong? Patrick
Patrick, the problem arises because computers can't exactly represent real numbers. Try this for your test: if(!all.equal(sum(x),1)) print("lost") else ("OK") see FAQ http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f I hope that this helps Andrew On Sat, Oct 21, 2006 at 11:04:13AM +0200, Patrick Giraudoux wrote:> Folks, > > I have got a strange behaviour when testing this: > > sum(x) != 1 > > let us set > > x<-c(70,134,1,5,0) > > and transform it in a vector of probabilities > > x<-x/sum(x) > > One expect sum(x) should be equal to 1, which is apparently the case > > > sum(x) > [1] 1 > > However, when I try to test it I get: > > > if(sum(x) !=1) print("lost") else ("OK") > [1] "lost" > > Which means that actually sum(x) is NOT considered equal to 1... > > Any idea about what is going wrong? > > Patrick > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Andrew Robinson Department of Mathematics and Statistics Tel: +61-3-8344-9763 University of Melbourne, VIC 3010 Australia Fax: +61-3-8344-4599 http://www.ms.unimelb.edu.au/~andrewpr http://blogs.mbs.edu/fishing-in-the-bay/
On Sat, 21 Oct 2006, Patrick Giraudoux wrote:> Folks, > > I have got a strange behaviour when testing this: > > sum(x) != 1FAQ 7.31 Why doesn't R think these numbers are equal?: print(sum(x), digits=20) identical(sum(x), 1) all.equal(sum(x), 1)> > let us set > > x<-c(70,134,1,5,0) > > and transform it in a vector of probabilities > > x<-x/sum(x) > > One expect sum(x) should be equal to 1, which is apparently the case > > > sum(x) > [1] 1 > > However, when I try to test it I get: > > > if(sum(x) !=1) print("lost") else ("OK") > [1] "lost" > > Which means that actually sum(x) is NOT considered equal to 1... > > Any idea about what is going wrong? > > Patrick > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
On Sat, Oct 21, 2006 at 11:04:13AM +0200, Patrick Giraudoux wrote:> Which means that actually sum(x) is NOT considered equal to 1... > > Any idea about what is going wrong?Others have already pointed out the problem and I would like to add a reference to a classical paper on this topic: David Goldberg What Every Computer Scientist Should Know About Floating-Point Arithmetic Computing Surveys, 1991 Reprints can be found in many places online - e.g. here: http://www.physics.ohio-state.edu/~dws/grouplinks/floating_point_math.pdf cu Philipp -- Dr. Philipp Pagel Tel. +49-8161-71 2131 Dept. of Genome Oriented Bioinformatics Fax. +49-8161-71 2186 Technical University of Munich Science Center Weihenstephan 85350 Freising, Germany and Institute for Bioinformatics / MIPS Tel. +49-89-3187 3675 GSF - National Research Center Fax. +49-89-3187 3585 for Environment and Health Ingolst?dter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/staff/pagel
On Sat, 21 Oct 2006, Philipp Pagel wrote:> On Sat, Oct 21, 2006 at 11:04:13AM +0200, Patrick Giraudoux wrote: >> Which means that actually sum(x) is NOT considered equal to 1... >> >> Any idea about what is going wrong? > > Others have already pointed out the problem and I would like to add a > reference to a classical paper on this topic: > > David Goldberg > What Every Computer Scientist Should Know About Floating-Point Arithmetic > Computing Surveys, 1991 > > Reprints can be found in many places online - e.g. here: >Yes, and one place is linked from the FAQ entry. -thomas