Skip to content

Instantly share code, notes, and snippets.

@SigurdJanson
Last active January 23, 2021 17:56
Show Gist options
  • Save SigurdJanson/f9afdd487b6758d15fd65b8a49684c04 to your computer and use it in GitHub Desktop.
Save SigurdJanson/f9afdd487b6758d15fd65b8a49684c04 to your computer and use it in GitHub Desktop.
Specialized integer division `x1 / x2` that correctly rounds to the nearest value (in R)
#' `%./%`
#' Specialized integer division `x1 / x2` that correctly rounds to the
#' nearest value.
#' @param x1,x2 numerator and denominator (integer)
#' @return integer
#' @source https://stackoverflow.com/questions/36377244/make-int-round-off-to-nearest-value/36377365
`%./%` <- function(x1, x2) {
return( x1 %/% x2 + (x1 %% x2) %/% (x2 %/% 2L + x2 %% 2L) )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment