Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
README.md
^quantities$
^revdep$
^\.positai$
^\.claude$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ configure.scan
# OSX-specific
.DS_Store
inst/doc
.positai
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: units
Version: 1.0-1.3
Version: 1.0-1.4
Title: Measurement Units for R Vectors
Authors@R: c(person("Edzer", "Pebesma", role = c("aut", "cre"), email = "edzer.pebesma@uni-muenster.de", comment = c(ORCID = "0000-0001-8049-7069")),
person("Thomas", "Mailund", role = "aut", email = "mailund@birc.au.dk"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

* Several fixes in documentation files; #433 @tszberkowitz

* Simplify unitless powers automatically; #435 addressing #179, #434

# version 1.0-1

* Add internal workaround for udunits2 bug with parsing of units that contain
Expand Down
14 changes: 8 additions & 6 deletions R/arith.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@ Ops.units <- function(e1, e2) {
return(.simplify_units(NextMethod(), .symbolic_units(numerator, denominator)))

} else if (pw) {
if (e2_inherits_units) {
if (e1_inherits_units && identical(units(e1), units(as_units(1)))) {
if (e2_inherits_units && identical(units(e2), unitless)) {
e2 <- drop_units(e2)
if (!e1_inherits_units)
return(NextMethod())

} else if (e2_inherits_units) {
if (e1_inherits_units && identical(units(e1), unitless))
e1 <- drop_units(e1)
} else if (e1_inherits_units) {
stop("power operation only allowed with numeric power")
Comment thread
edzer marked this conversation as resolved.
}

# code to manage things like exp(log(...)) and 10^log10(...) follows
# this is not supported in udunits2, so we are on our own
Expand Down Expand Up @@ -179,7 +181,7 @@ Ops.units <- function(e1, e2) {
# when the power is negative and we have a special case when it is zero where
# units should be removed.
if (e2 == 0) {
u <- units(as_units(1))
u <- unitless
} else {
tbl_den <- tabulate(factor(units(e1)$denominator))
tbl_num <- tabulate(factor(units(e1)$numerator))
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test_arith.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ test_that("we can take powers of units", {

expect_equal(as.numeric(ux ** 0), x ** 0)
expect_equal(as.numeric(ux ^ 0), x ^ 0)
expect_identical(units(ux ** 0), units(as_units(1)))
expect_identical(units(ux ^ 0), units(as_units(1)))
expect_identical(units(ux ** 0), unitless)
expect_identical(units(ux ^ 0), unitless)
})

test_that("we support unary +/-", {
Expand Down Expand Up @@ -181,7 +181,7 @@ test_that("we can undo logatithms", {
expect_equal(expm1(3^log(log1p(y), base=3)), set_units(y, m^2))
expect_error(exp(log10(x)), "wrong base in power operation")
expect_error(exp(x), "only allowed with logarithmic unit")
expect_error(exp(set_units(1, 1)), "only allowed with logarithmic unit")
expect_equal(exp(x/y), exp(drop_units(x/y)))
})

test_that("%/% and %% work", {
Expand Down
Loading