diff --git a/NEWS.md b/NEWS.md index 7398ee6..9bc2617 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# errors 0.4.3 + +- add option to print uncertainty with decimal point when appropriate + in the `parenthesis` notation + # 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..a91fe0e 100644 --- a/R/print.R +++ b/R/print.R @@ -11,6 +11,9 @@ #' encoded in scientific format. #' @param notation error notation; \code{"parenthesis"} and \code{"plus-minus"} #' are supported through the \code{"errors.notation"} option. +#' When using the "parenthesis" error notation, by default the uncertainty +#' is formatted without any decimal point, unless the option +#' \code{"errors.parenthesis.unc.dec.point"} is set to TRUE. #' @param ... ignored. #' #' @references @@ -45,8 +48,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 +64,14 @@ format.errors = function(x, if (notation == "parenthesis") { sep <- "(" append[] <- ")" - e[is.finite(e)] <- (e * 10^(pmax(0, value_digits-1)))[is.finite(e)] + if (!getOption("errors.parenthesis.unc.dec.point", FALSE)) { + ## remove decimal point from uncertainty by scaling it appropriately + e[is.finite(e)] <- (e * 10^(pmax(0, value_digits-1)))[is.finite(e)] + } else { + ## convert uncertainty for printing, keeping decimal point in line with value + e_scale_flag = (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..85140b3 100644 --- a/man/format.errors.Rd +++ b/man/format.errors.Rd @@ -19,7 +19,10 @@ value according to the Particle Data Group rounding rule (see references).} encoded in scientific format.} \item{notation}{error notation; \code{"parenthesis"} and \code{"plus-minus"} -are supported through the \code{"errors.notation"} option.} +are supported through the \code{"errors.notation"} option. +When using the "parenthesis" error notation, by default the uncertainty +is formatted without any decimal point, unless the option +\code{"errors.parenthesis.unc.dec.point"} is set to TRUE.} \item{...}{ignored.} } diff --git a/tests/testthat/test-print.R b/tests/testthat/test-print.R index 5c4668a..7a2a947 100644 --- a/tests/testthat/test-print.R +++ b/tests/testthat/test-print.R @@ -41,6 +41,14 @@ test_that("error formatting works properly", { c("(1.11112", "0.00001)e4"), c("(1.111122", "0.000001)e4"), c("(1.111122222", "0.000000001)e4"), c("(1.111122222000", "0.000000000001)e4")), paste, collapse=paste("", .pm, ""))) + # + # test using option to keep decimal point in uncertainty in parenthesis notation + # + saved_options = options(errors.parenthesis.unc.dec.point = TRUE) + expect_equal(format(x, notation="parenthesis", digits=3), + 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)")) + options(saved_options) x <- set_errors(rep(0.827, 3), c(0.119, 0.367, 0.962)) expect_equal(format(x, notation="plus-minus", digits="pdg"), sapply(list(