Last active
May 3, 2026 21:29
-
-
Save TracyGJG/1f1cd852efc960398082864215c9656e to your computer and use it in GitHub Desktop.
Modulo that handles negative values properly
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
| 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