R 具有POSIXct/POSIXlt和亚秒精度的奇数行为

R 具有POSIXct/POSIXlt和亚秒精度的奇数行为,r,xts,R,Xts,在POSIXct中使用sub-seconds时,我很难让序列按顺序发生 options(digits.secs=6) x <- xts(1:10, as.POSIXct("2011-01-21") + c(1:10)/1e3) 我希望下面的代码产生相同的输出 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 @GSee是对的,这是一个浮点算术问题。这是正确的,因为它是对象打印的方式 R

在POSIXct中使用sub-seconds时,我很难让序列按顺序发生

options(digits.secs=6)
x <- xts(1:10, as.POSIXct("2011-01-21") + c(1:10)/1e3)
我希望下面的代码产生相同的输出

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

@GSee是对的,这是一个浮点算术问题。这是正确的,因为它是对象打印的方式

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
所有的精度都在那里,但是
format.POSIXlt
中的这些行导致
选项(digits.secs=6)
不被遵守

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
     }
  }
}
是的。这些数字无法准确存储。尝试
选项(数字=20);c(1:10)/1e3
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
     }
  }
}
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"