[Rd] Strange behaviour of type conversion (PR#8256)

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Oct 27 16:01:50 CEST 2005


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 6


None 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



More information about the R-devel mailing list