R: Rounding Big Rationals ("bigq") to Decimals

roundQ {gmp} | R Documentation

## Rounding Big Rationals ("bigq") to Decimals

### Description

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)`.

### Usage

    round0(x)

    roundQ(x, digits = 0, r0 = round0)

    ## S3 method for class 'bigq'
    round(x, digits = 0)

### Arguments

`x` | vector of big rationals, i.e., of `class` `"bigq"`.
`digits` | integer number of decimal digits to round to.
`r0` | a `function` of one argument which implements a version of `round(x, digits=0)`. The default for `roundQ()` is to use our `round0()` which implements “round to even”, as base R's `round`.

### Value

`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)` .

### Author(s)

Martin Maechler, ETH Zurich

### References

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

### See Also

`round` for (double precision) numbers in base R; `roundX` from CRAN package round.

### Examples

    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
    }

[Package gmp version 0.6-2 Index]