Skip to content

Commit 083fbcd

Browse files
Enchufa2alusiani
andauthored
Add option to keep decimals for parenthesis notation (#61)
* added option to get proper decimal point in uncertainty formatted with "parenthesis" notation * restored undue modifications, more uniform code styling * new arg, rename option, some refactoring * update NEWS, bump version --------- Co-authored-by: Alberto Lusiani <[email protected]>
1 parent fab2796 commit 083fbcd

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: errors
22
Type: Package
33
Title: Uncertainty Propagation for R Vectors
4-
Version: 0.4.2
4+
Version: 0.4.2.1
55
Authors@R: c(
66
person("Iñaki", "Ucar", email="[email protected]",
77
role=c("aut", "cph", "cre"), comment=c(ORCID="0000-0001-6403-5550")),

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# errors devel
2+
3+
- Add option `decimals` to `format()` method to add support for uncertainty with
4+
decimals in the `parenthesis` notation (@alusiani #60, #61 addressing #47).
5+
16
# errors 0.4.2
27

38
- Add support for PDG rounding rules (@davidchall #59 addressing #45).

R/print.R

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#' encoded in scientific format.
1212
#' @param notation error notation; \code{"parenthesis"} and \code{"plus-minus"}
1313
#' are supported through the \code{"errors.notation"} option.
14+
#' @param decimals logical specifying whether the uncertainty should be formatted
15+
#' with a decimal point even when the \code{"parenthesis"} notation is used.
16+
#' Otherwise (by default), the \code{"parenthesis"} notation scales the
17+
#' uncertainty to match the least significant digit of the value.
1418
#' @param ... ignored.
1519
#'
1620
#' @references
@@ -20,6 +24,7 @@
2024
#' x <- set_errors(1:3*100, 1:3*100 * 0.05)
2125
#' format(x)
2226
#' format(x, digits=2)
27+
#' format(x, digits=2, decimals=TRUE)
2328
#' format(x, scientific=TRUE)
2429
#' format(x, notation="plus-minus")
2530
#'
@@ -31,12 +36,12 @@ format.errors = function(x,
3136
digits = NULL,
3237
scientific = FALSE,
3338
notation = getOption("errors.notation", "parenthesis"),
39+
decimals = getOption("errors.decimals", FALSE),
3440
...)
3541
{
3642
stopifnot(notation %in% c("parenthesis", "plus-minus"))
3743

38-
if (is.null(digits))
39-
digits <- getOption("errors.digits", 1)
44+
if (is.null(digits)) digits <- getOption("errors.digits", 1)
4045
digits <- if (digits == "pdg") digits_pdg(.e(x)) else rep(digits, length(x))
4146

4247
scipen <- getOption("scipen", 0)
@@ -45,8 +50,9 @@ format.errors = function(x,
4550

4651
e <- signif(.e(x), digits)
4752
nulle <- e == 0 & !is.na(e)
48-
xexp <- ifelse(.v(x) == 0, get_exponent(e) + 1, get_exponent(x))
49-
value_digits <- ifelse(e, digits - get_exponent(e), digits)
53+
eexp <- get_exponent(e)
54+
xexp <- ifelse(.v(x) == 0, eexp + 1, get_exponent(x))
55+
value_digits <- ifelse(e, digits - eexp, digits)
5056
value <- ifelse(e, signif(.v(x), xexp + value_digits), .v(x))
5157
value <- ifelse(is.finite(value), value, .v(x))
5258

@@ -60,7 +66,9 @@ format.errors = function(x,
6066
if (notation == "parenthesis") {
6167
sep <- "("
6268
append[] <- ")"
63-
e[is.finite(e)] <- (e * 10^(pmax(0, value_digits-1)))[is.finite(e)]
69+
e_scale_flag <- if (!isTRUE(decimals)) is.finite(e) else
70+
(cond & eexp < xexp) | (!cond & is.finite(e) & eexp < 0)
71+
e[e_scale_flag] <- (e * 10^(pmax(0, value_digits-1)))[e_scale_flag]
6472
} else {
6573
sep <- paste0(" ", .pm, " ")
6674
prepend[cond] <- "("

man/format.errors.Rd

Lines changed: 8 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ test_that("error formatting works properly", {
2323
expect_equal(format(x, notation="parenthesis", scientific=TRUE),
2424
c("1(1000)e4", "1.1(1)e4", "1.111(1)e4", "1.1111(1)e4", "1.11112(1)e4",
2525
"1.111122(1)e4", "1.111122222(1)e4", "1.111122222000(1)e4"))
26+
expect_equal(format(x, notation="parenthesis", digits=3, decimals=TRUE),
27+
c("10000(12300000)", "11110(1230)", "11111.2(12.3)", "11111.22(1.23)",
28+
"11111.222(123)", "11111.2222(123)", "11111.2222200(123)", "11111.2222200000(123)"))
2629

2730
expect_equal(format(x, notation="plus-minus"), sapply(list(
2831
c("10000", "10000000"), c("11000", "1000"), c("11110", "10"), c("11111", "1"),

0 commit comments

Comments
 (0)