qq <- as.bigq((-21:31), 10)
noquote(cbind(as.character(qq), asNumeric(qq)))
round0(qq) # Big Integer ("bigz")
## corresponds to R's own "round to even" :
stopifnot(round0(qq) == round(asNumeric(qq)))
round(qq) # == round(qq, 0): the same as round0(qq) *but* Big Rational ("bigq")
halfs <- as.bigq(1,2) + -5:12
q <- c(halfs, qq)
stopifnot(round0(q) == round(q)) ; rm(q)
if(FALSE)
## round0() is simply
round0 <- function (x) {
nU <- as.bigz.bigq(xU <- x + as.bigq(1, 2)) # traditional round: .5 rounded up
if(any(I <- is.whole.bigq(xU))) { # I <==> x == .5 : "hard case"
I[I] <- .mod.bigz(nU[I], 2L) == 1L # rounded up is odd ==> round *down*
nU[I] <- nU[I] - 1L
}
nU
}
## 's' for simple: rounding as you learned in school:
round0s <- function(x) as.bigz.bigq(x + as.bigq(1, 2))
cbind(halfs, round0s(halfs), round0(halfs))
if(FALSE)
## roundQ() is simply
roundQ <- function(x, digits = 0, r0 = round0) {
## round(x * 10^d) / 10^d -- vectorizing in both (x, digits)
p10 <- as.bigz(10) ^ digits # class: if(all(digits >= 0)) "bigz" else "bigq"
r0(x * p10) / p10
}
Run the code above in your browser using DataLab