gmail at kubism.ku.dk
2008-Feb-16 18:05 UTC
[Rd] Arithmetic bug? (found when use POSIXct) (PR#10776)
Full_Name: Bo Zhou Version: 2.6.1 (2007-11-26) OS: Windows XP Submission from: (NULL) (207.237.54.242) Hi, I found an arithmetic problem when I'm doing something with POSIXct The code to reproduce it is as follows (This is the recommended way of finding out time zone difference on R News 2004-1 Page 32 URL http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf) a=Sys.time() b<-as.POSIXct(format(a,tz="GMT")) a-b unclass(a) unclass(b) unclass(a)-unclass(b) as.numeric(a) as.numeric(b) as.numeric(a)-as.numeric(b) The result on my machine> a=Sys.time() > b<-as.POSIXct(format(a,tz="GMT")) > a-bTime difference of -4.999969 hours> unclass(a)[1] 1203184447> unclass(b)[1] 1203202447 attr(,"tzone") [1] ""> unclass(a)-unclass(b)[1] -17999.89 attr(,"tzone") [1] ""> as.numeric(a)[1] 1203184447> as.numeric(b)[1] 1203202447> as.numeric(a)-as.numeric(b)[1] -17999.89>
Gabor Grothendieck
2008-Feb-16 18:42 UTC
[Rd] Arithmetic bug? (found when use POSIXct) (PR#10776)
See FAQ 7.31 or explain what you believe to be the problem. On Feb 16, 2008 1:05 PM, <gmail at kubism.ku.dk> wrote:> Full_Name: Bo Zhou > Version: 2.6.1 (2007-11-26) > OS: Windows XP > Submission from: (NULL) (207.237.54.242) > > > Hi, > > I found an arithmetic problem when I'm doing something with POSIXct > The code to reproduce it is as follows (This is the recommended way of finding > out time zone difference on R News 2004-1 Page 32 URL > http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf) > > a=Sys.time() > b<-as.POSIXct(format(a,tz="GMT")) > a-b > unclass(a) > unclass(b) > unclass(a)-unclass(b) > as.numeric(a) > as.numeric(b) > as.numeric(a)-as.numeric(b) > > The result on my machine > > > a=Sys.time() > > b<-as.POSIXct(format(a,tz="GMT")) > > a-b > Time difference of -4.999969 hours > > unclass(a) > [1] 1203184447 > > unclass(b) > [1] 1203202447 > attr(,"tzone") > [1] "" > > unclass(a)-unclass(b) > [1] -17999.89 > attr(,"tzone") > [1] "" > > as.numeric(a) > [1] 1203184447 > > as.numeric(b) > [1] 1203202447 > > as.numeric(a)-as.numeric(b) > [1] -17999.89
bozhou1981 at hotmail.com
2008-Feb-17 20:50 UTC
[Rd] Arithmetic bug? (found when use POSIXct) (PR#10776)
Hi Gabo, FAQ 7.31 does not apply to this. "numeric" in R is 64bit which is large enough to handle this. I figured out the cause is that Sys.time() gives millisecond value while b<-as.POSIXct(format(a,tz=3D"GMT")) cuts off that part.=20 Then of course a-b is not 5 hours! Do it again with "options(digits=3D22)" and you will see what actually happened there. The recommended code from R News is not aware the truth that POSIXlt can represent sub-second time. Cheers, B _________________________________________________________________ Helping your favorite cause is as easy as instant messaging.=A0You IM, we give. [[alternative HTML version deleted]]
Gabor Grothendieck
2008-Feb-17 21:15 UTC
[Rd] Arithmetic bug? (found when use POSIXct) (PR#10776)
OK. Good point. Note that that article was written 4 years ago when R was at version 1.9 and POSIXt did not fully support subseconds. At that time, POSIXct could represent subseconds internally but it was not used as POSIXlt did not yet support it and that and the associated digits.sec option did not come until R version 2.3, released two years after the article was written. Try setting the digits.secs option before you run the command like this:> options(digits.secs = 3) > dp <- Sys.time() > dp-as.POSIXct(format(dp,tz="GMT"))Time difference of -5 hours On Feb 17, 2008 3:50 PM, <bozhou1981 at hotmail.com> wrote:> > Hi Gabo, > > FAQ 7.31 does not apply to this. "numeric" in R is 64bit which is large eno> ugh to handle this. > > I > figured out the cause is that Sys.time() gives millisecond value while > b<-as.POSIXct(format(a,tz=3D"GMT")) cuts off that part.=20 > > Then of course a-b is not 5 hours! > > Do it again with "options(digits=3D22)" and you will see what actually happ> ened there. > > The recommended code from R News is not aware the truth that POSIXlt can re> present sub-second time. > > Cheers, > > B > _________________________________________________________________ > Helping your favorite cause is as easy as instant messaging.=A0You IM, we g> ive. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >