Hi everyone, I've tried the below on R 1.9.1 and the 2004-08-30 builds of R 1.9.1 Patched and R 2.0.0 on Windows 2000, and the results are consistent. > seq(0.5, 0, by = -0.1) [1] 0.5 0.4 0.3 0.2 0.1 0.0 > seq(0.7, 0, by = -0.1) [1] 7.000000e-01 6.000000e-01 5.000000e-01 4.000000e-01 3.000000e-01 2.000000e-01 1.000000e-01 -1.110223e-16 Is this really the intended behaviour? I ended up using > seq(0.7, 0, length = 8) [1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0 which does what I want. //Henric
On Fri, 3 Sep 2004, Henric Nilsson wrote:> Hi everyone, > > I've tried the below on R 1.9.1 and the 2004-08-30 builds of R 1.9.1 > Patched and R 2.0.0 on Windows 2000, and the results are consistent. > > > seq(0.5, 0, by = -0.1) > [1] 0.5 0.4 0.3 0.2 0.1 0.0 > > > seq(0.7, 0, by = -0.1) > [1] 7.000000e-01 6.000000e-01 5.000000e-01 4.000000e-01 3.000000e-01 > 2.000000e-01 1.000000e-01 -1.110223e-16 > > Is this really the intended behaviour? I ended up usingWell, you are using a floating point representation in a digital computer, so I don't think you should be surprised. Note that the internal representation is also modified by the print() functions, so what you see when an object is printed is not always exactly what is inside the object.> > > seq(0.7, 0, length = 8) > [1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0 > > which does what I want. > > //Henric > > ______________________________________________ > 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 >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
Hi! For IEEE floating point systems e_mach= 2^-53 ~= 10^-16 in double precision.> identical(seq(0.7, 0, by = -0.1),seq(0.7, 0, length = 8))[1] FALSE> a<-seq(0.7, 0, by = -0.1) > b<-seq(0.7, 0, length = 8) > a==b[1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE> (a-b)<10^-16[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE>Its in the range of machine accuracy (precision). /Eryk. *********** REPLY SEPARATOR *********** On 9/3/2004 at 1:23 PM Henric Nilsson wrote:>>>Hi everyone, >>> >>>I've tried the below on R 1.9.1 and the 2004-08-30 builds of R 1.9.1 >>>Patched and R 2.0.0 on Windows 2000, and the results are consistent. >>> >>> > seq(0.5, 0, by = -0.1) >>>[1] 0.5 0.4 0.3 0.2 0.1 0.0 >>> >>> > seq(0.7, 0, by = -0.1) >>>[1] 7.000000e-01 6.000000e-01 5.000000e-01 4.000000e-01 >>>3.000000e-01 >>>2.000000e-01 1.000000e-01 -1.110223e-16 >>> >>>Is this really the intended behaviour? I ended up using >>> >>> > seq(0.7, 0, length = 8) >>>[1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0 >>> >>>which does what I want. >>> >>>//Henric >>> >>>______________________________________________ >>>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.htmlDipl. bio-chem. Witold Eryk Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: witek96 at users.sourceforge.net ---W-W---- http://www.molgen.mpg.de/~wolski wolski at molgen.mpg.de
On 3 Sep 2004 at 13:41, Roger Bivand wrote:> On Fri, 3 Sep 2004, Henric Nilsson wrote: > > > Hi everyone, > > > > I've tried the below on R 1.9.1 and the 2004-08-30 builds of R 1.9.1 > > Patched and R 2.0.0 on Windows 2000, and the results are consistent. > > > > > seq(0.5, 0, by = -0.1) > > [1] 0.5 0.4 0.3 0.2 0.1 0.0 > > > > > seq(0.7, 0, by = -0.1) > > [1] 7.000000e-01 6.000000e-01 5.000000e-01 4.000000e-01 > > 3.000000e-01 2.000000e-01 1.000000e-01 -1.110223e-16 > > > > Is this really the intended behaviour? I ended up using > > Well, you are using a floating point representation in a digital > computer, so I don't think you should be surprised. Note that the > internal representation is also modified by the print() functions, so > what you see when an object is printed is not always exactly what is > inside the object. > > > > > > seq(0.7, 0, length = 8) > > [1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0 > > > > which does what I want.Hi Are you sure?> seq(0.7, 0, length = 8)-c(0.7,0.6,0.5,0.4,0.3,0.2,0.1,0)[1] 0.000000e+00 0.000000e+00 0.000000e+00 -5.551115e-17 0.000000e+00 [6] 0.000000e+00 -2.775558e-17 0.000000e+00>> sum(seq(0.7, 0, length = 8)-c(0.7,0.6,0.5,0.4,0.3,0.2,0.1,0))[1] -8.326673e-17> sum(seq(0.7, 0, length = 8)-c(0.7,0.6,0.5,0.4,0.3,0.2,0.1,0))==0[1] FALSE>Cheers Petr> > > > //Henric > > > > ______________________________________________ > > 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 > > > > -- > Roger Bivand > Economic Geography Section, Department of Economics, Norwegian School > of Economics and Business Administration, Breiviksveien 40, N-5045 > Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: > Roger.Bivand at nhh.no > > ______________________________________________ > 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.htmlPetr Pikal petr.pikal at precheza.cz
On 03-Sep-04 Henric Nilsson wrote:> Hi everyone, > > I've tried the below on R 1.9.1 and the 2004-08-30 builds of R 1.9.1 > Patched and R 2.0.0 on Windows 2000, and the results are consistent. > > > seq(0.5, 0, by = -0.1) > [1] 0.5 0.4 0.3 0.2 0.1 0.0 > > > seq(0.7, 0, by = -0.1) > [1] 7.000000e-01 6.000000e-01 5.000000e-01 4.000000e-01 > 3.000000e-01 > 2.000000e-01 1.000000e-01 -1.110223e-16 > > Is this really the intended behaviour? I ended up using > > > seq(0.7, 0, length = 8) > [1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0 > > which does what I want.Hi Henric, The replies you have already received explain what is going on. However, what you (and we all) need is a procedure which can guarantee that what you expect is what you get, For instance, a test that the 8th element of the "seq" was "==0" would give the result FALSE. What I'm in the habit of doing for this sort of thing is on the lines of 0.1*seq(5,0, by = -1) [1] 0.5 0.4 0.3 0.2 0.1 0.0 or even simply 0.1*(5:0) [1] 0.5 0.4 0.3 0.2 0.1 0.0 The difference is that here the sequence element is working with integers, which are (provided within bounds) handled exactly in a digitial computer. Floating point numbers with a fractional part generally are not, as already explained by others. Note however the result of 0.1*(5:0) == 0.3) [1] FALSE FALSE FALSE FALSE FALSE FALSE as opposed to "==0.5", "==0.4", "==0.2", "==0.1" or "==0"! I hope this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 167 1972 Date: 03-Sep-04 Time: 13:59:59 ------------------------------ XFMail ------------------------------
Henric Nilsson <henric.nilsson <at> statisticon.se> writes:> > Hi everyone, > > I've tried the below on R 1.9.1 and the 2004-08-30 builds of R 1.9.1 > Patched and R 2.0.0 on Windows 2000, and the results are consistent. > > > seq(0.5, 0, by = -0.1) > [1] 0.5 0.4 0.3 0.2 0.1 0.0 > > > seq(0.7, 0, by = -0.1) > [1] 7.000000e-01 6.000000e-01 5.000000e-01 4.000000e-01 3.000000e-01 > 2.000000e-01 1.000000e-01 -1.110223e-16 > > Is this really the intended behaviour? I ended up using > > > seq(0.7, 0, length = 8) > [1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0 > > which does what I want. >You could also try (7:0)/10 . The numerator will be an integer sequence, which can be performed exactly, so you will have fewer occasions for floating point representations and their associated approximations: R> class(7:0) [1] "integer" R> (7:0)/10 # or seq(7,0)/10 [1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0 R> class((7:0)/10) [1] "numeric"
> Note however the result of > > 0.1*(5:0) == 0.3) > [1] FALSE FALSE FALSE FALSE FALSE FALSE > > as opposed to "==0.5", "==0.4", "==0.2", "==0.1" or "==0"! > > I hope this helps, > Ted. > > > -------------------------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> > Fax-to-email: +44 (0)870 167 1972 > Date: 03-Sep-04 Time: 13:59:59At the risk of flailing at a dead and even dessicated horse ...>From the Help file of identical(), the constructionidentical(all.equal(x,y),TRUE) gives the desired test for equality for finite precision arithmetic. Hence: > sapply(.1*(0:5),function(x)identical(all.equal(x,.3),TRUE)) [1] FALSE FALSE FALSE TRUE FALSE FALSE which is, presumably, as desired. Cheers, -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box