Skip to content

Commit

Permalink
Add option to keep decimals for parenthesis notation (#61)
Browse files Browse the repository at this point in the history
* added option to get proper decimal point in uncertainty formatted with "parenthesis" notation

* restored undue modifications, more uniform code styling

* new arg, rename option, some refactoring

* update NEWS, bump version

---------

Co-authored-by: Alberto Lusiani <[email protected]>
  • Loading branch information
Enchufa2 and alusiani authored Aug 1, 2024
1 parent fab2796 commit 083fbcd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: errors
Type: Package
Title: Uncertainty Propagation for R Vectors
Version: 0.4.2
Version: 0.4.2.1
Authors@R: c(
person("Iñaki", "Ucar", email="[email protected]",
role=c("aut", "cph", "cre"), comment=c(ORCID="0000-0001-6403-5550")),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# errors devel

- Add option `decimals` to `format()` method to add support for uncertainty with
decimals in the `parenthesis` notation (@alusiani #60, #61 addressing #47).

# errors 0.4.2

- Add support for PDG rounding rules (@davidchall #59 addressing #45).
Expand Down
18 changes: 13 additions & 5 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#' encoded in scientific format.
#' @param notation error notation; \code{"parenthesis"} and \code{"plus-minus"}
#' are supported through the \code{"errors.notation"} option.
#' @param decimals logical specifying whether the uncertainty should be formatted
#' with a decimal point even when the \code{"parenthesis"} notation is used.
#' Otherwise (by default), the \code{"parenthesis"} notation scales the
#' uncertainty to match the least significant digit of the value.
#' @param ... ignored.
#'
#' @references
Expand All @@ -20,6 +24,7 @@
#' x <- set_errors(1:3*100, 1:3*100 * 0.05)
#' format(x)
#' format(x, digits=2)
#' format(x, digits=2, decimals=TRUE)
#' format(x, scientific=TRUE)
#' format(x, notation="plus-minus")
#'
Expand All @@ -31,12 +36,12 @@ format.errors = function(x,
digits = NULL,
scientific = FALSE,
notation = getOption("errors.notation", "parenthesis"),
decimals = getOption("errors.decimals", FALSE),
...)
{
stopifnot(notation %in% c("parenthesis", "plus-minus"))

if (is.null(digits))
digits <- getOption("errors.digits", 1)
if (is.null(digits)) digits <- getOption("errors.digits", 1)
digits <- if (digits == "pdg") digits_pdg(.e(x)) else rep(digits, length(x))

scipen <- getOption("scipen", 0)
Expand All @@ -45,8 +50,9 @@ format.errors = function(x,

e <- signif(.e(x), digits)
nulle <- e == 0 & !is.na(e)
xexp <- ifelse(.v(x) == 0, get_exponent(e) + 1, get_exponent(x))
value_digits <- ifelse(e, digits - get_exponent(e), digits)
eexp <- get_exponent(e)
xexp <- ifelse(.v(x) == 0, eexp + 1, get_exponent(x))
value_digits <- ifelse(e, digits - eexp, digits)
value <- ifelse(e, signif(.v(x), xexp + value_digits), .v(x))
value <- ifelse(is.finite(value), value, .v(x))

Expand All @@ -60,7 +66,9 @@ format.errors = function(x,
if (notation == "parenthesis") {
sep <- "("
append[] <- ")"
e[is.finite(e)] <- (e * 10^(pmax(0, value_digits-1)))[is.finite(e)]
e_scale_flag <- if (!isTRUE(decimals)) is.finite(e) else
(cond & eexp < xexp) | (!cond & is.finite(e) & eexp < 0)
e[e_scale_flag] <- (e * 10^(pmax(0, value_digits-1)))[e_scale_flag]
} else {
sep <- paste0(" ", .pm, " ")
prepend[cond] <- "("
Expand Down
9 changes: 8 additions & 1 deletion man/format.errors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/testthat/test-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ test_that("error formatting works properly", {
expect_equal(format(x, notation="parenthesis", scientific=TRUE),
c("1(1000)e4", "1.1(1)e4", "1.111(1)e4", "1.1111(1)e4", "1.11112(1)e4",
"1.111122(1)e4", "1.111122222(1)e4", "1.111122222000(1)e4"))
expect_equal(format(x, notation="parenthesis", digits=3, decimals=TRUE),
c("10000(12300000)", "11110(1230)", "11111.2(12.3)", "11111.22(1.23)",
"11111.222(123)", "11111.2222(123)", "11111.2222200(123)", "11111.2222200000(123)"))

expect_equal(format(x, notation="plus-minus"), sapply(list(
c("10000", "10000000"), c("11000", "1000"), c("11110", "10"), c("11111", "1"),
Expand Down

0 comments on commit 083fbcd

Please sign in to comment.