g.toedt@dkfz.de
2005-Oct-27 13:24 UTC
[Rd] Critical Bug in type conversions: as.integer, trunc, ... (PR#8255)
Full_Name: Grischa T?dt Version: 2.1.1 OS: windows XP Submission from: (NULL) (192.108.25.32) I have a strange behaviour in R, looks like type conversions are messed up. To reproduce: expected:> typeof(3)[1] "double"> as.integer(3)[1] 3 !!!! strange:> typeof((0.3/0.1))[1] "double"> as.integer((0.3/0.1))[1] 2 also for trunc:>trunc(c(5,7)) >c(0.5,0.7)/c(0.1,0.1)and now watch the amazing!!!!:>trunc((c(0.5,0.7)/c(0.1,0.1)))My R-Version: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 1.1 year 2005 month 06 day 20 language R
Duncan Murdoch
2005-Oct-27 13:40 UTC
[Rd] Critical Bug in type conversions: as.integer, trunc, ... (PR#8255)
On 10/27/2005 9:24 AM, g.toedt at dkfz.de wrote:> Full_Name: Grischa T?dt > Version: 2.1.1 > OS: windows XP > Submission from: (NULL) (192.108.25.32) > > > I have a strange behaviour in R, looks like type conversions are messed up.This is not a bug, it's normal behaviour for floating point values, discussed in the FAQ item on "7.31 Why doesn't R think these numbers are equal?" Duncan Murdoch> > To reproduce: > > expected: >> typeof(3) > [1] "double" > >> as.integer(3) > [1] 3 > > !!!! strange: >> typeof((0.3/0.1)) > [1] "double" > >> as.integer((0.3/0.1)) > [1] 2 > > also for trunc: >>trunc(c(5,7)) >>c(0.5,0.7)/c(0.1,0.1) > > and now watch the amazing!!!!: >>trunc((c(0.5,0.7)/c(0.1,0.1))) > > > > > My R-Version: > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 1.1 > year 2005 > month 06 > day 20 > language R > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Marc Schwartz
2005-Oct-27 13:41 UTC
[Rd] Critical Bug in type conversions: as.integer, trunc, (PR#8255)
On Thu, 2005-10-27 at 15:24 +0200, g.toedt at dkfz.de wrote:> Full_Name: Grischa T?dt > Version: 2.1.1 > OS: windows XP > Submission from: (NULL) (192.108.25.32) > > > I have a strange behaviour in R, looks like type conversions are messed up. > > To reproduce: > > expected: > > typeof(3) > [1] "double" > > > as.integer(3) > [1] 3 > > !!!! strange: > > typeof((0.3/0.1)) > [1] "double"> > as.integer((0.3/0.1)) > [1] 2 > > also for trunc: > >trunc(c(5,7)) > >c(0.5,0.7)/c(0.1,0.1) > > and now watch the amazing!!!!: > >trunc((c(0.5,0.7)/c(0.1,0.1)))> trunc((c(0.5,0.7)/c(0.1,0.1)))[1] 5 6 You are getting bitten (you know where) by floating point representation issues here:> print(0.3 / 0.1, digits = 20)[1] 2.9999999999999995559> print(0.7 / 0.1, digits = 20)[1] 6.9999999999999991118 So as.integer() and trunc() is working as intended, since in both cases, they truncate towards 0. Bottom line: NOTABUG Read R FAQ 7.31 and the reference cited there for more information. HTH, Marc Schwartz