Skip to content

Instantly share code, notes, and snippets.

@queviva
Last active January 31, 2022 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save queviva/b3016da66955682c76a19930e4431d5e to your computer and use it in GitHub Desktop.
Save queviva/b3016da66955682c76a19930e4431d5e to your computer and use it in GitHub Desktop.
returns a value of x, cyclically-modulated by M
const xModByM = (x, M) => ((x % M) + M) % M;
@queviva
Copy link
Author

queviva commented Jan 31, 2022

returns the ACTUAL modulus value of a number; that is to say, instead of negative numbers returning reverse-mod-negative values - as
happens with %= or any other modulus use - it converts the ENTIRE number-line into a cyclical universe of values from 0 - M

if you are at, say, the x-coordinate 3px, and you want to move_negative_ twenty pixels through a cyclical universe that is a hundred pixels
wide, compare these two results:

console.log(
    (3 - 20) % 100,     // produces -17
    xModByM(3-20, 100)  // produces  83
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment