API Reference › Math › Math.posmod
method

Math.posmod

Math.posmod(a, m) -> int

Returns a modulo m, but always in the range 0..m-1 — unlike the % operator, which keeps the sign of a (so -1 % 5 is -1, while Math.posmod(-1, 5) is 4). It is what you want for wrapping indices and grid coordinates that can go negative.

Example

program Demo {
  handler Step phase Update {
    let col = Math.posmod(x, grid_width)   # never negative
  }
}
← All symbols