Skip to content

Instantly share code, notes, and snippets.

@TracyGJG
Last active May 3, 2026 21:29
Show Gist options
  • Select an option

  • Save TracyGJG/1f1cd852efc960398082864215c9656e to your computer and use it in GitHub Desktop.

Select an option

Save TracyGJG/1f1cd852efc960398082864215c9656e to your computer and use it in GitHub Desktop.
Modulo that handles negative values properly
const mod = (v, m) => (v + m) % m;
function modulo(m, v) {
return v == null ? (_v) => mod(_v, m) : mod(v, m);
}
console.log('\nJS mod: -1 % 1000 \t=', -1 % 1000);
console.log('\nmod(-1, 1000) \t\t=', mod(-1, 1000));
console.log('\nmodulo(1000, -1) \t=', modulo(1000, -1));
console.log('modulo(1000)(-1) \t=', modulo(1000)(-1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment