Skip to content

Instantly share code, notes, and snippets.

@PaulChana
Created September 28, 2017 11:31
Show Gist options
  • Save PaulChana/277665931608e755201274f8cb4cf6b9 to your computer and use it in GitHub Desktop.
Save PaulChana/277665931608e755201274f8cb4cf6b9 to your computer and use it in GitHub Desktop.
Rounding to nearest
// 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