This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| functions:mod [2017/03/15 14:52] – created korshun | functions:mod [2017/07/15 12:13] (current) – korshun | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | {{tag>todo}} | + | ====== mod ====== |
| + | {{tag>math}} | ||
| + | '' | ||
| + | |||
| + | [[: | ||
| + | |||
| + | ===== Description ===== | ||
| + | This function implements modulo division '' | ||
| + | |||
| + | This makes its result safe to use an array index, as the result is not negative for negative values of '' | ||
| + | |||
| + | Only positive values of '' | ||
| + | |||
| + | ===== Examples ===== | ||
| + | < | ||
| + | mod(-5, 3) -> 1 | ||
| + | mod(-4, 3) -> 2 | ||
| + | mod(-3, 3) -> 0 | ||
| + | mod(-2, 3) -> 1 | ||
| + | mod(-1, 3) -> 2 | ||
| + | mod(0, 3) -> 0 | ||
| + | mod(1, 3) -> 1 | ||
| + | mod(2, 3) -> 2 | ||
| + | mod(3, 3) -> 0 | ||
| + | mod(4, 3) -> 1 | ||
| + | mod(5, 3) -> 2 | ||
| + | </ | ||
| + | |||
| + | Using mod to implement menu cycling easily: | ||
| + | |||
| + | < | ||
| + | if (KeyPressed(BT_DOWN)) | ||
| + | menuindex++; | ||
| + | if (KeyPressed(BT_UP)) | ||
| + | menuindex--; | ||
| + | |||
| + | menuindex = mod(menuindex, | ||
| + | </ | ||