Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle sub-second representation error
According to ?`%/%`: '%%' and 'x %/% y' can be used for non-integer 'y', e.g. '1 %/% 0.2', but the results are subject to representation error and so may be platform-dependent. The example below shows one consequence of this. The 'x' object has an observation every 100ms, so we expect endpoints(x, "ms", 200) to produce a sequence from 0 to 10 in steps of 2. But the 4th observation is 7 instead of 6, the cause of which you can see from the output of sprintf(): R> x <- .xts(1:10, .POSIXct(1.5e9 + 0:9 / 10)) R> endpoints(x, "ms", 200) [1] 0 2 4 7 8 10 R> sprintf("%0.0f", .index(x) %/% 1e-3) [1] "1500000000000" "1500000000099" "1500000000200" "1500000000300" [5] "1500000000400" "1500000000500" "1500000000599" "1500000000700" [9] "1500000000800" "1500000000900" The output is more reasonable if we convert to milliseconds and microseconds by multiplying instead of integer division by a fraction. R> sprintf("%0.0f", .index(x) * 1e3) [1] "1500000000000" "1500000000100" "1500000000200" "1500000000300" [5] "1500000000400" "1500000000500" "1500000000600" "1500000000700" [9] "1500000000800" "1500000000900" We don't need to worry about values to the right of the decimal place, because the cast to 64-bit integer in the C code will truncate the double values toward zero (according to C99). See #202.
- Loading branch information