Last active
January 23, 2021 17:56
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' `%./%` | |
#' 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