Rounding Infrastructure

Flexible rounding functions for use in error detection, including the exact rounding bounds that consistency tests are built on. They were outsourced from the 'scrutiny' package, which depends on them since its version 1.0.0.


roundwork

R-CMD-check

The goal of roundwork is to enable flexible rounding beyond base::round(). Its main API is reround(), from which all implemented rounding procedures can be accessed. This is useful to reconstruct how others may have rounded their data.

Given a rounded number, call unround() to get the lower and upper bounds of the original number. This also takes rounding options into account, just like reround() does. bound_numerators() and sum_range() are building blocks using exact integer arithmetic to avoid all the bugs that can occur with floating-point operations.

roundwork contains the rounding layer that used to be part of scrutiny. Since version 1.0.0, scrutiny imports it from here, as can any other package that reconstructs rounding.

Installation

Install the package from CRAN:

install.packages("roundwork")

Alternatively, install the development version from GitHub:

remotes::install_github("lhdjung/roundwork")

Get started

Round in some specific way, e.g., up from 5:

round_up(4.1679, digits = 2)
#> [1] 4.17

Use reround() to round more flexibly, especially in your own functions. You can specify a rounding procedure:

reround(4.1679, digits = 2, rounding = "up")
#> [1] 4.17

Ceil or floor numbers if certain strong assumptions are permitted:

reround(4.1679, digits = 2, rounding = "floor")
#> [1] 4.16

The default is to return two numbers — one rounded up, one rounded down. This is best for conservative assumptions, so that your conclusions won’t hinge on edge cases where these rounding procedures differ.

reround(1.275, digits = 2)
#> [1] 1.28 1.27

Ties

“Up” and “down” mean up and down on the number line, so they only part company with “away from zero” and “toward zero” below zero. If you would rather not have to keep that straight, name the tie-breaking rule outright:

round_ties_up(-2.5)     # Stata
#> [1] -2
round_ties_away(-2.5)   # Excel, SPSS, SAS, Matlab
#> [1] -3

Bounds on the unrounded number

unround() computes the range of an unknown original number, given its rounded version. The rounding procedure determines whether the bounds are inclusive or not:

unround("3.60")
#> # A tibble: 1 × 7
#>   range                     rounding   lower incl_lower x     incl_upper upper
#>   <chr>                     <chr>      <dbl> <lgl>      <chr> <lgl>      <dbl>
#> 1 3.595 <= x(3.60) <= 3.605 up_or_down  3.60 TRUE       3.60  TRUE        3.60

unround("3.60", rounding = "up")
#> # A tibble: 1 × 7
#>   range                    rounding lower incl_lower x     incl_upper upper
#>   <chr>                    <chr>    <dbl> <lgl>      <chr> <lgl>      <dbl>
#> 1 3.595 <= x(3.60) < 3.605 up        3.60 TRUE       3.60  FALSE       3.60

unround("3.60", rounding = "ceiling")
#> # A tibble: 1 × 7
#>   range                 rounding lower incl_lower x     incl_upper upper
#>   <chr>                 <chr>    <dbl> <lgl>      <chr> <lgl>      <dbl>
#> 1 3.59 < x(3.60) <= 3.6 ceiling   3.59 FALSE      3.60  TRUE         3.6

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("roundwork")

0.1.0 by Lukas Jung, 5 days ago


https://lhdjung.github.io/roundwork/, https://github.com/lhdjung/roundwork/


Report a bug at https://github.com/lhdjung/roundwork/issues


Browse source code at https://github.com/cran/roundwork


Authors: Lukas Jung [aut, cre]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports cli, dplyr, magrittr, rlang, stringr, tibble

Suggests conflicted, ggplot2, knitr, rmarkdown, testthat


Imported by unsum.


See at CRAN