Skip to content

Commit fdcc671

Browse files
authored
Support digits="pdg" (#59)
Apply the Particle Data Group rounding rule
1 parent 5581714 commit fdcc671

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

R/print.R

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@
55
#' @param x an \code{errors} object.
66
#' @param digits how many significant digits are to be used for uncertainties.
77
#' The default, \code{NULL}, uses \code{getOption("errors.digits", 1)}.
8+
#' Use `digits="pdg"` to choose an appropriate number of digits for each value
9+
#' according to the Particle Data Group rounding rule.
810
#' @param scientific logical specifying whether the elements should be
911
#' encoded in scientific format.
1012
#' @param notation error notation; \code{"parenthesis"} and \code{"plus-minus"}
1113
#' are supported through the \code{"errors.notation"} option.
1214
#' @param ... ignored.
1315
#'
16+
#' @references
17+
#' K. Nakamura et al. (Particle Data Group), J. Phys. G 37, 075021 (2010)
18+
#'
1419
#' @examples
1520
#' x <- set_errors(1:3*100, 1:3*100 * 0.05)
1621
#' format(x)
1722
#' format(x, digits=2)
1823
#' format(x, scientific=TRUE)
1924
#' format(x, notation="plus-minus")
2025
#'
26+
#' x <- set_errors(c(0.827, 0.827), c(0.119, 0.367))
27+
#' format(x, notation="plus-minus", digits="pdg")
28+
#'
2129
#' @export
2230
format.errors = function(x,
2331
digits = NULL,
@@ -32,9 +40,12 @@ format.errors = function(x,
3240
prepend <- rep("", length(x))
3341
append <- rep("", length(x))
3442

43+
if (digits == "pdg")
44+
digits <- digits_pdg(.e(x))
45+
3546
e <- signif(.e(x), digits)
3647
exponent <- get_exponent(x)
37-
value_digits <- ifelse(e, digits - get_exponent(e), getOption("digits"))
48+
value_digits <- ifelse(e, digits - get_exponent(e), digits)
3849
value <- ifelse(e, signif(.v(x), exponent + value_digits), .v(x))
3950

4051
cond <- (scientific | (exponent > 4+scipen | exponent < -3-scipen)) & is.finite(e)
@@ -61,7 +72,13 @@ format.errors = function(x,
6172
formatC(value[[i]], format="f", digits=max(0, value_digits[[i]]-1), decimal.mark=getOption("OutDec"))
6273
else format(value[[i]])
6374
})
64-
e <- formatC(e, format="fg", flag="#", digits=digits, width=digits, decimal.mark=getOption("OutDec"))
75+
e <- if (length(unique(digits)) > 1) {
76+
sapply(seq_along(digits), function(i) {
77+
formatC(e[[i]], format="fg", flag="#", digits=digits[[i]], width=max(1, digits[[i]]), decimal.mark=getOption("OutDec"))
78+
})
79+
} else {
80+
formatC(e, format="fg", flag="#", digits=digits[[1]], width=max(1, digits[[1]]), decimal.mark=getOption("OutDec"))
81+
}
6582
e <- sub("\\.$", "", e)
6683
paste(prepend, value, sep, e, append, sep="")
6784
}

R/utils.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ warn_once_coercion <- function(fun) warn_once(
2626

2727
get_exponent <- function(x) ifelse(.v(x), floor(log10(abs(.v(x)))), 0)
2828

29+
digits_pdg <- function(x) {
30+
# extract 3 highest order digits
31+
x <- ifelse(is.finite(x), x, 0)
32+
x_sci <- formatC(abs(x), digits=2, format="e", decimal.mark=".")
33+
x_hod <- as.integer(gsub("(\\.|e.*)", "", x_sci))
34+
35+
ifelse(x_hod < 355, 2, ifelse(x_hod < 950, 1, 0))
36+
}
37+
2938
propagate <- function(xx, x, y, dx, dy, method=getOption("errors.propagation", "taylor-first-order")) {
3039
# if y not defined, use a vector of NAs
3140
if (length(y) == 1 && is.na(y))

man/format.errors.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-print.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ test_that("error formatting works properly", {
1717
expect_equal(format(x, notation="parenthesis", digits=3),
1818
c("10000(12300000)", "11110(1230)", "11111.2(123)", "11111.22(123)",
1919
"11111.222(123)", "11111.2222(123)", "11111.2222200(123)", "11111.2222200000(123)"))
20+
expect_equal(format(x, notation="parenthesis", digits="pdg"),
21+
c("10000(12000000)", "11100(1200)", "11111(12)", "11111.2(12)",
22+
"11111.22(12)", "11111.222(12)", "11111.222220(12)", "11111.222220000(12)"))
2023
expect_equal(format(x, notation="parenthesis", scientific=TRUE),
2124
c("1(1000)e4", "1.1(1)e4", "1.111(1)e4", "1.1111(1)e4", "1.11112(1)e4",
2225
"1.111122(1)e4", "1.111122222(1)e4", "1.111122222000(1)e4"))
@@ -29,12 +32,21 @@ test_that("error formatting works properly", {
2932
c("10000", "12300000"), c("11110", "1230"), c("11111.2", "12.3"), c("11111.22", "1.23"),
3033
c("11111.222", "0.123"), c("11111.2222", "0.0123"), c("11111.2222200", "0.0000123"), c("11111.2222200000", "0.0000000123")),
3134
paste, collapse=paste("", .pm, "")))
35+
expect_equal(format(x, notation="plus-minus", digits="pdg"), sapply(list(
36+
c("10000", "12000000"), c("11100", "1200"), c("11111", "12"), c("11111.2", "1.2"),
37+
c("11111.22", "0.12"), c("11111.222", "0.012"), c("11111.222220", "0.000012"), c("11111.222220000", "0.000000012")),
38+
paste, collapse=paste("", .pm, "")))
3239
expect_equal(format(x, notation="plus-minus", scientific=TRUE), sapply(list(
3340
c("(1", "1000)e4"), c("(1.1", "0.1)e4"), c("(1.111", "0.001)e4"), c("(1.1111", "0.0001)e4"),
3441
c("(1.11112", "0.00001)e4"), c("(1.111122", "0.000001)e4"), c("(1.111122222", "0.000000001)e4"),
3542
c("(1.111122222000", "0.000000000001)e4")),
3643
paste, collapse=paste("", .pm, "")))
3744

45+
x <- set_errors(rep(0.827, 3), c(0.119, 0.367, 0.962))
46+
expect_equal(format(x, notation="plus-minus", digits="pdg"), sapply(list(
47+
c("0.83", "0.12"), c("0.8", "0.4"), c("1", "1")),
48+
paste, collapse=paste("", .pm, "")))
49+
3850
x <- set_errors(10, 1)
3951
expect_equal(format(x - set_errors(10)), "0(1)")
4052
expect_equal(format(x - x), "0(0)")

0 commit comments

Comments
 (0)