A lot of the time you need to know how to do an operation and the opposite of that operation. e.g. if an attack subtracts 5 from health, then to 'undo' that attack, you

*add*5 to health.

In these cases, then mod will come up whenever you're trying to do the opposite of integer division. Keep in mind though that this is only for integers -- with floats there's no remainder in division because they can store fractions as well as whole numbers.

One of the most common examples is calculating an index for a 2D array (when emulating a 2D array using a 1D array).

`int* array2d = new int[width*height];`

To calculate a 1D index from 2D x/y values, we can use:

`int index = x + y*width;`

If you want to then turn this index back into an x and y value, we need the opposite of this function.

Mathematically, we can rearrange the above formula to solve for x/y:

`x = index - y*width`

`y = index/width - x/width`

or working with real numbers, we can find y first then use it to find x:

`y = RoundDown( index / width )`*(this works because we know x < width, so x/width < 1)*

`x = index - y*width`

or still working with real numbers, we can find x first then use it to find y:

`x = Remainder( index / width )`*(again, this works because we know x will be in the fractional part and y in the whole part)*

`y = index/width - x/width`

or working with integers, we can take the two bold bits above that map easily over to integer code:

`int y = index / width;`*(integer division rounds down)*

`int x = index % width;`