ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


functions:mod

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
functions:mod [2017/03/15 16:52] – created korshunfunctions:mod [2017/07/15 15:13] (current) korshun
Line 1: Line 1:
-{{tag>todo}}+====== mod ====== 
 +{{tag>math}} 
 +''num mod(num a, num b)'' 
 + 
 +[[:types|num - any numeric type (int or fixed)]] 
 + 
 +===== Description ===== 
 +This function implements modulo division ''a % b'', but, unlike the ACS operator, it handles negative values of ''a'' like mathematical modulo, making ''a'' cycle around on the range ''[0, b)''
 + 
 +This makes its result safe to use an array index, as the result is not negative for negative values of ''a''.  
 + 
 +Only positive values of ''b'' are supported. 
 + 
 +===== Examples ===== 
 +<code> 
 +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 
 +</code> 
 + 
 +Using mod to implement menu cycling easily: 
 + 
 +<code> 
 +if (KeyPressed(BT_DOWN)) 
 +    menuindex++; 
 +if (KeyPressed(BT_UP)) 
 +    menuindex--; 
 +     
 +menuindex = mod(menuindex, NUM_MENU_ITEMS); 
 +</code>
functions/mod.1489589543.txt.gz · Last modified: 2017/03/15 16:52 by korshun