Created
September 28, 2017 11:31
-
-
Save PaulChana/277665931608e755201274f8cb4cf6b9 to your computer and use it in GitHub Desktop.
Rounding to nearest
This file contains hidden or 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
// round n down to nearest multiple of m | |
int roundDown (int n, int m) | |
{ | |
return n >= 0 ? (n / m) * m : ((n - m + 1) / m) * m; | |
} | |
// round n up to nearest multiple of m | |
int roundUp (int n, int m) | |
{ | |
return n >= 0 ? ((n + m - 1) / m) * m : (n / m) * m; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment