I have been trying to make a contribution to the data.table
package by adding the round
function to the ITime
class, when i came across a rather odd discrepancy produced by the round
function. Behind the scenes, an object of class ITime
is just an integer vector with pretty formatting, and thus unclass(object)
provides an integer vector.
Rounding this integer vector to the nearest minute can thus be done like this:
x <- as.ITime(seq(as.POSIXct("2020-01-01 07:00:00"), as.POSIXct("2020-01-01 07:10:00"), "30 sec"))
round(unclass(x) / 60L) * 60L
# or
round(as.integer(x) / 60L) * 60L
Here is where the problem comes...
When I do this operation, I would expect any instance of unclass(x) / 60
that ends with .5 to be rounded up. However, that is not the case!
I have tried the example on both Windows and Mac on two different computers with the same result. Does anyone have an idea as to why this would happen?
** FYI I know that this particular problem can be solved differently: unclass(x) %/% 60L
. But my interest is in why the round
function does not work as expected.