Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option to get decimal point in uncertainty formatted with "parenthesis" notation #60

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 16 additions & 6 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 All @@ -34,21 +37,22 @@ format.errors = function(x,
...)
{
stopifnot(notation %in% c("parenthesis", "plus-minus"))

alusiani marked this conversation as resolved.
Show resolved Hide resolved
if (is.null(digits))
digits <- getOption("errors.digits", 1)
digits <- if (digits == "pdg") digits_pdg(.e(x)) else rep(digits, length(x))

alusiani marked this conversation as resolved.
Show resolved Hide resolved
scipen <- getOption("scipen", 0)
prepend <- rep("", length(x))
append <- rep("", length(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)
e_expon = get_exponent(e)
Enchufa2 marked this conversation as resolved.
Show resolved Hide resolved
xexp <- ifelse(.v(x)==0, e_expon+1, get_exponent(x))
Enchufa2 marked this conversation as resolved.
Show resolved Hide resolved
value_digits <- ifelse(e, digits - e_expon, digits)
value <- ifelse(e, signif(.v(x), xexp + value_digits), .v(x))
##+++ str(value)
Enchufa2 marked this conversation as resolved.
Show resolved Hide resolved
value <- ifelse(is.finite(value), value, .v(x))
##+++ str(value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove


cond <- (scientific | (xexp > 4+scipen | xexp < -3-scipen)) & is.finite(e)
e[cond] <- e[cond] * 10^(-xexp[cond])
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 & e_expon < xexp) | (!cond & is.finite(e) & e_expon<0)
e[e_scale_flag] <- (e * 10^(pmax(0, value_digits-1)))[e_scale_flag]
}
} else {
sep <- paste0(" ", .pm, " ")
prepend[cond] <- "("
Expand All @@ -81,7 +92,6 @@ format.errors = function(x,
decimal.mark=getOption("OutDec"))
})
e <- sub("\\.$", "", e)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore

paste(prepend, value, sep, e, append, sep="")
}

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.

27 changes: 9 additions & 18 deletions tests/testthat/test-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ test_that("error formatting works properly", {
expect_equal(format(x, notation="parenthesis", digits=3),
c("10000(12300000)", "11110(1230)", "11111.2(123)", "11111.22(123)",
"11111.222(123)", "11111.2222(123)", "11111.2222200(123)", "11111.2222200000(123)"))
expect_equal(format(x, notation="parenthesis", digits="pdg"),
c("10000(12000000)", "11100(1200)", "11111(12)", "11111.2(12)",
"11111.22(12)", "11111.222(12)", "11111.222220(12)", "11111.222220000(12)"))
Enchufa2 marked this conversation as resolved.
Show resolved Hide resolved
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"))
Expand All @@ -32,33 +29,27 @@ test_that("error formatting works properly", {
c("10000", "12300000"), c("11110", "1230"), c("11111.2", "12.3"), c("11111.22", "1.23"),
c("11111.222", "0.123"), c("11111.2222", "0.0123"), c("11111.2222200", "0.0000123"), c("11111.2222200000", "0.0000000123")),
paste, collapse=paste("", .pm, "")))
expect_equal(format(x, notation="plus-minus", digits="pdg"), sapply(list(
c("10000", "12000000"), c("11100", "1200"), c("11111", "12"), c("11111.2", "1.2"),
c("11111.22", "0.12"), c("11111.222", "0.012"), c("11111.222220", "0.000012"), c("11111.222220000", "0.000000012")),
paste, collapse=paste("", .pm, "")))
Enchufa2 marked this conversation as resolved.
Show resolved Hide resolved
expect_equal(format(x, notation="plus-minus", scientific=TRUE), sapply(list(
c("(1", "1000)e4"), c("(1.1", "0.1)e4"), c("(1.111", "0.001)e4"), c("(1.1111", "0.0001)e4"),
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, "")))

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(
c("0.83", "0.12"), c("0.8", "0.4"), c("1", "1")),
paste, collapse=paste("", .pm, "")))
Enchufa2 marked this conversation as resolved.
Show resolved Hide resolved
#
# 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(10, 1)
expect_equal(format(x - set_errors(10)), "0(1)")
expect_equal(format(x - x), "0(0)")

x <- set_errors(c(0.4, NA, NaN, Inf, -Inf))
Enchufa2 marked this conversation as resolved.
Show resolved Hide resolved
x <- set_errors(c(0.4, NA, NaN, Inf))
expect_equal(format(x[1]), "0.4(0)")
expect_equal(format(x[2]), "NA(NA)")
expect_equal(format(x[3]), "NaN(NaN)")
expect_equal(format(x[4]), "Inf(Inf)")
expect_equal(format(x[5]), "-Inf(Inf)")

x <- set_errors(c(0e10, 1e12), 0.1e12)
expect_equal(format(x), c("0.0(1)e12", "1.0(1)e12"))
expect_equal(format(x, digits=2), c("0.00(10)e12", "1.00(10)e12"))
Enchufa2 marked this conversation as resolved.
Show resolved Hide resolved
})
Loading