R/utils.r

Defines functions ts_differ identicalts exponent

Documented in exponent

#' returns the exponent of a floating point numeber (eng representation)
#'
#' @name exponent
#' @param x number
#' @return the exponent of the eng. representation of the number

exponent <- function(x) {
   if (x == 0) 0
   else floor(log10(abs(x)))
}


identicalts <- function(x, y, toll = 0.000001) {
  all(abs(x - y) <= toll)
}


ts_differ <- function(x, y, toll = 0.000001) {
  is_empty <- function(obj) {
    length(obj) == 0 || (length(obj) == 1 && is.na(obj[1]))
  }

  if (is_empty(x) && is_empty(y)) {
    warning("objects in input are empty")
    return(FALSE)
  } else {
    if (is_empty(x)) {
      warning("1st argument in input is empty")
      return(TRUE)
    }
    if (is_empty(y)) {
      warning("2nd argument in input is empty")
      return(TRUE)
    }
  }

  x[is.na(x)] <- 0
  y[is.na(y)] <- 0

  if (toll < 0) warning("toll is negative")

  any(abs(x - y) >= toll) ||
    length(x) != length(y) ||
    # may be the index doesn't need a toll
    any(abs(zoo::index(x) - zoo::index(y)) >= 0.001)
}
giupo/GrafoDB documentation built on Oct. 12, 2022, 9:43 a.m.