Skip to content

Instantly share code, notes, and snippets.

@Aadithya-V
Last active May 13, 2023 11:07
Show Gist options
  • Save Aadithya-V/633716256d37c0cc3342f3cdea3551f9 to your computer and use it in GitHub Desktop.
Save Aadithya-V/633716256d37c0cc3342f3cdea3551f9 to your computer and use it in GitHub Desktop.
FloorMod for Go
// For modulo not remainder:
// n mod m = n-⌊nm⌋.m
func FloorMod(n, m int) int {
res := n % m // remainder operator, strictly speaking: example-
//If -8 % 7 == 6, you're fine, but if it is -1, you'll need to adjust it by
//adding m to any negative results as below.
if res < 0 {
res += m
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment