alexandre.courtiol at gmail.com
2009-Feb-24 13:55 UTC
[Rd] invalid comparison in numeric sequence (PR#13551)
Full_Name: alex Version: 2.8.1 OS: Ubuntu / MacOSX Submission from: (NULL) (162.38.183.51)> 0.6==0.6[1] TRUE> seq(0,1,0.1)==0.4[1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE> seq(0,1,0.1)==0.6[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE> seq(0,1,0.1)==0.8[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE What is wrong with 0.6 ??? (TRUE is missing) I tried 3 differents computers (2 Ubuntu with R 2.8.1, and one Mac with R 2.8).
Peter Dalgaard
2009-Feb-25 10:30 UTC
[Rd] invalid comparison in numeric sequence (PR#13551)
alexandre.courtiol at gmail.com wrote:> >> 0.6==0.6 > [1] TRUE >> seq(0,1,0.1)==0.4 > [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE >> seq(0,1,0.1)==0.6 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE >> seq(0,1,0.1)==0.8 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE > > > What is wrong with 0.6 ??? (TRUE is missing) > I tried 3 differents computers (2 Ubuntu with R 2.8.1, and one Mac with R 2.8).FAQ 7.31, not a bug. Expect about 10 people to tell you so. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
This is one of the joys of floating point arithmetic. Look at the 7th element of seq(0,1,.1) - 0.6 Also, check the all.equal(x,y,tolerance=epsilon ) function. John -----r-devel-bounces@r-project.org wrote: ----- To: r-devel@stat.math.ethz.ch From: alexandre.courtiol@gmail.com Sent by: r-devel-bounces@r-project.org Date: 02/24/2009 08:55AM cc: R-bugs@r-project.org Subject: [Rd] invalid comparison in numeric sequence (PR#13551) Full_Name: alex Version: 2.8.1 OS: Ubuntu / MacOSX Submission from: (NULL) (162.38.183.51)> 0.6==0.6[1] TRUE> seq(0,1,0.1)==0.4[1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE> seq(0,1,0.1)==0.6[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE> seq(0,1,0.1)==0.8[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE What is wrong with 0.6 ??? (TRUE is missing) I tried 3 differents computers (2 Ubuntu with R 2.8.1, and one Mac with R 2.8). ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel [[alternative HTML version deleted]]
> > seq(0,1,0.1)==0.4 > [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE > > seq(0,1,0.1)==0.6 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > > seq(0,1,0.1)==0.8 > [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE > > What is wrong with 0.6 ??? (TRUE is missing) > I tried 3 differents computers (2 Ubuntu with R 2.8.1, and one Mac with R 2.8).If you know that all the numbers in a sequence should have a given decimal precision, then you obtain a better result using round(,digits=...). For example, x <- round(seq(0,1,0.1), digits=1) rbind(x == 0.4, x == 0.6, x ==0.8) produces [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [1,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE [2,] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE [3,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE This does not mean, that x[7] is now equal to 0.6, however both x[7] and 0.6 are represented by the same 53-bit floating point number formatC(x[7], digits=20) # "0.5999999999999999778" formatC(0.6, digits=20) # "0.5999999999999999778" Petr.
Possibly Parallel Threads
- Unexpected behaviour of seq(from,to,by) (PR#14057)
- small bug in base::formatC (PR#13474)
- Problem with R, staged installation for packages, and samba share
- Problem with R, staged installation for packages, and samba share
- Problem with R, staged installation for packages, and samba share