diff --git a/DESCRIPTION b/DESCRIPTION index 398ee30..e017f38 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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="iucar@fedoraproject.org", role=c("aut", "cph", "cre"), comment=c(ORCID="0000-0001-6403-5550")), diff --git a/NEWS.md b/NEWS.md index 7398ee6..28380e0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/print.R b/R/print.R index e376c1b..ee30fe6 100644 --- a/R/print.R +++ b/R/print.R @@ -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 @@ -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") #' @@ -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) @@ -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)) @@ -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] <- "(" diff --git a/man/format.errors.Rd b/man/format.errors.Rd index 692f758..9502eca 100644 --- a/man/format.errors.Rd +++ b/man/format.errors.Rd @@ -5,7 +5,8 @@ \title{Encode \code{errors}} \usage{ \method{format}{errors}(x, digits = NULL, scientific = FALSE, - notation = getOption("errors.notation", "parenthesis"), ...) + notation = getOption("errors.notation", "parenthesis"), + decimals = getOption("errors.decimals", FALSE), ...) } \arguments{ \item{x}{an \code{errors} object.} @@ -21,6 +22,11 @@ encoded in scientific format.} \item{notation}{error notation; \code{"parenthesis"} and \code{"plus-minus"} are supported through the \code{"errors.notation"} option.} +\item{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.} + \item{...}{ignored.} } \description{ @@ -30,6 +36,7 @@ Format an \code{errors} object for pretty printing. 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") diff --git a/tests/testthat/test-print.R b/tests/testthat/test-print.R index 5c4668a..a966e00 100644 --- a/tests/testthat/test-print.R +++ b/tests/testthat/test-print.R @@ -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"),