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.
Install the package from CRAN:
install.packages("roundwork")Alternatively, install the development version from GitHub:
remotes::install_github("lhdjung/roundwork")Round in some specific way, e.g., up from 5:
round_up(4.1679, digits = 2)
#> [1] 4.17Use 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.17Ceil or floor numbers if certain strong assumptions are permitted:
reround(4.1679, digits = 2, rounding = "floor")
#> [1] 4.16The 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“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] -3unround() 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