Hi everybody, this is a warning more than a question. I noticed that seq produces approximate results:> seq(0,1,0.05)[19]==0.9[1] TRUE> seq(0,1,0.05)[20]==0.95[1] FALSE> seq(0,1,0.05)[21]==1[1] TRUE> seq(0,1,0.05)[20]-0.95[1] 1.110223024625157e-16 I do not understand why 0.9 and 1 are correct (within some tolerance or strictly exact?) and 0.95 is not. this one works:> ((0:20)/20)[20]==0.95[1] TRUE Eric Elguero
FAQ 7.31 On 2/4/08, Eric Elguero <Eric.Elguero at mpl.ird.fr> wrote:> Hi everybody, > > this is a warning more than a question. > > I noticed that seq produces approximate results: > > > seq(0,1,0.05)[19]==0.9 > [1] TRUE > > seq(0,1,0.05)[20]==0.95 > [1] FALSE > > seq(0,1,0.05)[21]==1 > [1] TRUE > > > seq(0,1,0.05)[20]-0.95 > [1] 1.110223024625157e-16 > > I do not understand why 0.9 and 1 are correct (within some > tolerance or strictly exact?) and 0.95 is not. > > this one works: > > > ((0:20)/20)[20]==0.95 > [1] TRUE > > Eric Elguero > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
FAQ Q7.31 and ?"==" should enlighten you. None of 0.05, 0.1 ... 0.9, 0.95 are exactly representable on a binary computer. On Mon, 4 Feb 2008, Eric Elguero wrote:> Hi everybody, > > this is a warning more than a question. > > I noticed that seq produces approximate results: > >> seq(0,1,0.05)[19]==0.9 > [1] TRUE >> seq(0,1,0.05)[20]==0.95 > [1] FALSE >> seq(0,1,0.05)[21]==1 > [1] TRUE > >> seq(0,1,0.05)[20]-0.95 > [1] 1.110223024625157e-16 > > I do not understand why 0.9 and 1 are correct (within some > tolerance or strictly exact?) and 0.95 is not. > > this one works: > >> ((0:20)/20)[20]==0.95 > [1] TRUE > > Eric Elguero > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, stats.ox.ac.uk/~ripley University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595