g.toedt@dkfz-heidelberg.de
2005-Oct-27 13:40 UTC
[Rd] Strange behaviour of type conversion (PR#8256)
Where is my error?? I have a strange behaviour in R, looks like type conversions are messed up. Maybe i just make a stupid mistake, but help would be appreciated. To reproduce: expected:> typeof(3)[1] "double"> as.integer(3)[1] 3> typeof((0.3/0.1))[1] "double" !!!! strange:> as.integer((0.3/0.1))[1] 2 also for trunc:>trunc(c(5,7))[1] 5 7>c(0.5,0.7)/c(0.1,0.1)[1] 5 7 and now watch:>trunc((c(0.5,0.7)/c(0.1,0.1)))[1] 5 6 My R-Version: platform i386-pc-mingw32 arch i386 =20 os mingw32 =20 system i386, mingw32 =20 status =20 major 2 =20 minor 1.1 =20 year 2005 =20 month 06 =20 day 20 =20 language R Cheers, Grischa T=F6dt
g.toedt at dkfz-heidelberg.de schreef op de 27e dag van de wijnmaand van het...:> Where is my error?? > I have a strange behaviour in R, looks like type conversions are messed > up. > Maybe i just make a stupid mistake, but help would be appreciated.Not maybe. You definately make a stupid mistake. There is no error here in the behavior of R. -- Peter Kleiweg
> Where is my error??You are assuming that numbers are represented exactly: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f> 0.3/0.1 == 3[1] FALSE Hadley
g.toedt at dkfz-heidelberg.de writes:> Where is my error?? > I have a strange behaviour in R, looks like type conversions are messed > up. > Maybe i just make a stupid mistake, but help would be appreciated. > > To reproduce: > > expected: > > typeof(3) > [1] "double" > > > as.integer(3) > [1] 3 > > > typeof((0.3/0.1)) > [1] "double" > > !!!! strange: > > as.integer((0.3/0.1)) > [1] 2 > > also for trunc: > >trunc(c(5,7)) > [1] 5 7 > > >c(0.5,0.7)/c(0.1,0.1) > [1] 5 7 > > and now watch: > >trunc((c(0.5,0.7)/c(0.1,0.1))) > [1] 5 6None of this is bugs. Please read up on floating point representations on a binary computer. Specifically:> 0.3/0.1 - 3[1] -4.440892e-16> 0.7/0.1 - 7[1] -8.881784e-16> 0.5/0.1 - 5[1] 0> My R-Version: > platform i386-pc-mingw32 > arch i386 =20 > os mingw32 =20 > system i386, mingw32 =20 > status =20 > major 2 =20 > minor 1.1 =20 > year 2005 =20 > month 06 =20 > day 20 =20 > language R > > > Cheers, > Grischa T=F6dt > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- 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