Posted by: pixelero | July 28, 2008

Various functions with modulo

Some functions using the modulo-operation (mod, %, remainder) :

basic case: y=x%100, value waves between 0 and 99:


addition of two different modulos: y=(x%100+x%79)/2
addition of three: y=(x%100+x%31+x%79)/3. I divide by n to get a result easier to compare, with f'(x)=1 when defined …
addition of four – and we’re approaching a fractal wave: y=(x%100 + x%79 + x%31 + x%17)/4


Subtracting two modulos: y=x%50 -x%30 – tops of the levels are horizontal e.g. f'(x)=0 when defined
and four y=x%50 -x%30 +x%20 -x%12, a ‘city skyline’-function


A zigzag function: y=|x%(2*m)-m| : m= wave’s height, 2*m = wavelength:
In the javascript-demo I defined a special function for that:

function zigzag(x,m) {
return Math.abs(x%(2*m)-m);

adding two zigzags, y=zigzag(x,25) +zigzag(x,15):
… and three, y=zigzag(x,25) +zigzag(x,21)+zigzag(x,15), for a ‘random’ mountain silhuette:

small javascript-demo for testing (try also (x*x)%100 or the ‘barcode’-function: (x*x*x)%150-(x*x*x)%75 )

Pixel Bendel -code as an example of this in 2D, creating a ‘random’ size tiling:


  1. Awesome, well presented, many thanks.

  2. I’d love to see the pixel bender code for the random tile effect — can you link to it?

  3. it’s right here …

  4. Cool man, the modulu-waves is real similiar to what we call in (musical) Analogue-oscillators “Hard-Sync”,
    The 2d example is real cool. attach an oscillator man!

  5. Are you doing modulos division using ActionScript or from within PixelBinder? I’m having trouble doing simple modulo division in PixelBender getting ERROR: (line 35): ‘%’ : syntax error parse error

  6. Figured out the modulo question. This worked
    float xr = float(5);
    float yr = float(7);
    float qer = mod(xr,yr);

    Anyone know of a full list of built in PixelBuilder methods…I haven’t found any such thing from Adobe

  7. @oyd11 : I might try that some day, I’ve just recently done some first studies with generating Sound with Flash 10.

    @JP DeVries :
    yes, it’s sometimes %, sometimes mod, see

    I think you’ll info in Menu:Help/Pixel Bender Specification in the toolkit, on the last pages of that pdf.

    btw and generally, one thing sometimes causing extra trouble is that different languages have different practicies when it comes to the sign of a modulo operation with negative values: see , second table on the right

  8. thanks pixelero!
    I just did my first PixelBender filter, can be seen here

  9. […] is an article by PIXELERO, he’s to be credited for that and the article is available here: various functions with modulo. I’ve added a greyscale representation underneath to view it as a series of variations on 1 […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: