Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
17 changes: 14 additions & 3 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand All @@ -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] <- "("
Expand Down
5 changes: 4 additions & 1 deletion man/format.errors.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/test-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down