Skip to content

Commit

Permalink
Check that x is below 0
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmbaazam committed Nov 6, 2023
1 parent ffcdfa4 commit f7d27a5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#' @author Sebastian Funk
#' @keywords internal
complementary_logprob <- function(x) {
checkmate::assert_numeric(
x, lower = -Inf, upper = 0
)
tryCatch(log1p(-sum(exp(x))), error = function(e) -Inf)
}

Expand Down
12 changes: 9 additions & 3 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ test_that("Errors are thrown", {
)
})

test_that("Warnings are thrown", {
expect_warning(complementary_logprob(0.1), "NaNs produced")
expect_warning(complementary_logprob(Inf), "NaNs produced")
test_that("Errors are thrown", {
expect_error(
complementary_logprob(0.1),
"is not <= 0"
)
expect_error(
complementary_logprob(Inf),
"is not <= 0"
)
})

0 comments on commit f7d27a5

Please sign in to comment.