Rounding Big Rationals ("bigq") to Decimals
Rounding big rationals (of class "bigq"
, see as.bigq()
)
to decimal digits
is strictly based on a (optionally choosable)
definition of rounding to integer, i.e., digits = 0
, the default
method of which we provide as round0()
.
The users typically just call round(x, digits)
as elsewhere, and
the round()
method will call round(x, digits, round0=round0)
.
round0(x) roundQ(x, digits = 0, r0 = round0) ## S3 method for class 'bigq' round(x, digits = 0)
round0()
returns a vector of big integers, i.e., "bigz"
classed.
roundQ(x, digits, round0)
returns a vector of big rationals,
"bigq"
, as x
.
round.bigq
is very simply defined as
function(x, digits) roundQ(x, digits)
.
Martin Maechler, ETH Zurich
The vignette “Exact Decimal Rounding via Rationals” from CRAN package round,
Wikipedia, Rounding, notably "Round half to even": https://en.wikipedia.org/wiki/Rounding#Round_half_to_even
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 ## 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 == <n>.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)) ## 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 }
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.