Odd behavor with POSIXct/POSIXlt and subsecond accuracy

可紊 提交于 2019-12-07 23:31:49

问题


I am having difficulty having a sequence occur in order when using sub seconds with POSIXct.

options(digits.secs=6)
x <- xts(1:10, as.POSIXct("2011-01-21") + c(1:10)/1e3)

Produces the following output, why aren't the times in order?

                        [,1]
2011-01-21 00:00:00.000    1
2011-01-21 00:00:00.002    2
2011-01-21 00:00:00.003    3
2011-01-21 00:00:00.003    4
2011-01-21 00:00:00.005    5
2011-01-21 00:00:00.006    6
2011-01-21 00:00:00.006    7
2011-01-21 00:00:00.007    8
2011-01-21 00:00:00.009    9
2011-01-21 00:00:00.009   10

I would expect the same output the code below produces

c(1:10)/1e3
[1] 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.010

回答1:


@GSee is right, this is a floating point arithmetic problem. And Gavin Simpson's answer is correct in that it's how the object is printed.

R> options(digits=17)
R> .index(x)
 [1] 1295589600.0009999 1295589600.0020001 1295589600.0030000 1295589600.0039999
 [5] 1295589600.0050001 1295589600.0060000 1295589600.0070000 1295589600.0079999
 [9] 1295589600.0090001 1295589600.0100000

All the precision is there, but these lines in format.POSIXlt cause options(digits.secs=6) to not be honored.

np <- getOption("digits.secs")
if (is.null(np)) 
  np <- 0L
else
  np <- min(6L, np)
if (np >= 1L) {
  for (i in seq_len(np) - 1L) {
     if (all(abs(secs - round(secs, i)) < 1e-06)) {
       np <- i
       break
     }
  }
}

Due to precision issues, in your example np is reset to 3 in the above for loop. And the format "%Y-%m-%d %H:%M:%OS3" yields the times you posted. You can see the times are accurate if you use the "%Y-%m-%d %H:%M:%OS6" format.

R> format(as.POSIXlt(index(x)[1:2]), "%Y-%m-%d %H:%M:%OS3")
[1] "2011-01-21 00:00:00.000" "2011-01-21 00:00:00.002"
R> format(as.POSIXlt(index(x)[1:2]), "%Y-%m-%d %H:%M:%OS6")
[1] "2011-01-21 00:00:00.000999" "2011-01-21 00:00:00.002000"


来源:https://stackoverflow.com/questions/11571105/odd-behavor-with-posixct-posixlt-and-subsecond-accuracy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!