R/safe_log.R

Defines functions safe_log

Documented in safe_log

#' Take logarithm avoiding underflow
#'
#' Returns the normal log if there is no underflow. If there is underflow, then
#' returns the minimum for which log can return (-744.4401)
#'
#' @export
#' @param x vector
#' @return vector log in base e of input or minimum possible log value of -744.4401
#'
#' @examples
#' log(c(1e-323, 1e-324))      # gives -Inf for the second value
#' safe_log(c(1e-323, 1e-324)) # gives the minimum value of -744.4401

safe_log <- function(x) {
  ret <- log(x)
#   if(sum(ret == -Inf) > 0) stop('Cutting log')
  ret[ret == -Inf] <- -744.4401
  return(ret)
}

Try the BaTFLED3D package in your browser

Any scripts or data that you put into this service are public.

BaTFLED3D documentation built on May 2, 2019, 2:38 p.m.