GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Processing
  • Download
  • Reference
  • Environment
  • Libraries
  • Tools
  • Wiki
  • Tutorials
  • Examples
  • Books
  • Forum
  • Overview
  • People
  • Privacy

This view

  • Original page
  • New address

GeezerVison is showing processing.org — % (modulo) / Reference

Reference+
Documentation→Reference→Math→Operators

Name

% (modulo)

Description

Calculates the remainder when one number is divided by another. For example, when 52.1 is divided by 10, the divisor (10) goes into the dividend (52.1) five times (5 * 10 == 50), and there is a remainder of 2.1 (52.1 - 50 == 2.1). Thus, 52.1 % 10 produces 2.1.

Note that when the divisor is greater than the dividend, the remainder constitutes the value of the entire dividend. That is, a number cannot be divided by any number larger than itself. For example, when 9 is divided by 10, the result is zero with a remainder of 9. Thus, 9 % 10 produces 9.

Modulo is extremely useful for ensuring values stay within a boundary, such as when keeping a shape on the screen. (See the second example above.)

Examples

  • int a = 5 % 4;            // Sets 'a' to 1
    int b = 125 % 100;        // Sets 'b' to 25
    float c = 285.5 % 140.0;  // Sets 'c' to 5.5 
    float d = 30.0 % 33.0;    // Sets 'd' to 30.0
    
  • int a = 0;
    void draw() {
      background(200);
      a = (a + 1) % width;  // 'a' increases between 0 and width 
      line(a, 0, a, height);
    }
    

Syntax

  • value1 % value2

Parameters

  • value1int or float
  • value2int or float

Return

  • int or float

Related

  • divide
Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.