Mod Function

A mod function divides two numbers and returns the remainder. It requires the same input values as a division operation but outputs the "leftover" value instead of the division result. For example:

  • 7 mod 3 = 1
  • 8 mod 3 = 2

If there is no remainder, the result is zero.

  • 9 mod 3 = 0

In mathematical formulas, the mod or "modulo" operator is often represented with a percentage sign. For example, 7 mod 3 can be written as the following expression:

  • 7 % 3 = 1

In computer science, a mod function may be written as mod(x,y), or something similar, depending on the programming language. For example:

  • mod(7,3) = 1

The function above divides the first value (dividend) by the second value (divisor) and returns the remainder. Because 7 ÷ 3 = 2r1, the result of mod(7,3) is 1.

Mod Function Applications

The mod function has several uses in computer programming. Since the result must be less than the divisor, it can limit results to a specific range. For example mod(x,4) can only return 0, 1, 2, or 3, assuming x is an integer. It is useful for checking every nth value or categorizing results into a limited number of "buckets."

In most programming languages, 0 evaluates as false, and other numbers evaluate as true. Therefore, a mod function can provide a boolean result — false if two numbers are evenly divisible and true if they are not. Below is an example of a mod function within an if statement:

if (mod(x,5)) then { ... }

If x is not evenly divisible by 5, the code in the then clause will execute. If x is divisible by five — 5, 10, 15, 20, etc — the code will not.

NOTE: Mod functions often have two integers as arguments. However, floating point numbers are also acceptable. Two integer values always produce an integer, but one or more fractional inputs may output a fractional result. For instance, 8 % 2.5 = 0.5 since 8 ÷ 2.5 = 3r0.5.

Updated May 24, 2022 by Per C.

quizTest Your Knowledge

Which of these devices does not modulate and demodulate data over an analog signal?

A
USB flash drive
0%
B
Cable modem
0%
C
Wi-Fi network card
0%
D
Bluetooth adapter
0%
Correct! Incorrect!     View the Modulation definition.
More Quizzes →

The Tech Terms Computer Dictionary

The definition of Mod Function on this page is an original definition written by the TechTerms.com team. If you would like to reference this page or cite this definition, please use the green citation links above.

The goal of TechTerms.com is to explain computer terminology in a way that is easy to understand. We strive for simplicity and accuracy with every definition we publish. If you have feedback about this definition or would like to suggest a new technical term, please contact us.

Sign up for the free TechTerms Newsletter

How often would you like to receive an email?

You can unsubscribe or change your frequency setting at any time using the links available in each email.

Questions? Please contact us.