Nepedia
Back to Discover
Fluent Reader
🔢

Modulo

math Maturity 7-9 Next article →

Imagine you are splitting a pile of snacks into equal groups. Sometimes, you will have a few pieces left over that do not fit. In mathematics, this leftover amount is called the remainder. The modulo operation is a special way to find that remainder after division.

Divmod truncated
Divmod truncated

When you divide a number by another, the second number is called the modulus. For example, if you have five items and divide them by two, the remainder is one. We can write this as "5 mod 2 = 1". If you have nine items and divide them by three, the remainder is zero.

Divmod Euclidean
Divmod Euclidean

This simple idea helps us understand how numbers fit together.

There are different ways to calculate the modulo, especially with negative numbers. In math, people often use the Euclidean division method. This method usually chooses the smallest non-negative integer as the remainder.

Divmod floored
Divmod floored

However, computers and calculators sometimes use different rules. Some systems use a method called truncated division. In this version, the sign of the remainder might match the number being divided. This can lead to surprises for people writing computer code. For example, checking if a number is odd by looking for a remainder of one might fail if the number is negative. A safer way is to check if the remainder is simply not zero.

Different programming languages choose different rules for their modulo operations. For instance, the languages Standard Pascal and ALGOL 68 always give a positive remainder. Other languages, like C90, let the specific computer system decide how to handle negative numbers.

Divmod rounding
Divmod rounding

This variety means that a coder must know which rule their language follows. Some languages like Python use a "floored" definition. This means the result depends on how the division is rounded. Because of these different rules, the same math problem might give different answers on different machines. Understanding these rules is a key part of computer science.

Computers use special tricks to make modulo math happen very quickly. Doing a full division every time can be slow for a machine. For certain numbers, like powers of two, computers can use a faster method called a bitwise AND operation.

Divmod Euclidean
Divmod Euclidean

This is a way for the hardware to look at the tiny pieces of a number to find the remainder. Compilers, which are tools that help run code, can often spot these patterns. They can automatically change a slow modulo command into a fast bitwise command. This helps programs run smoothly without the person writing the code having to do extra work. It is a clever way to save time and energy.

Modulo is also a very important tool in the world of secret codes. In a field called cryptography, math properties of the modulo are used to keep data safe. For example, the Diffie-Hellman key exchange uses these properties to help people share secrets.

Divmod floored
Divmod floored

You can also use a "modulo with offset" to change the range of the results. Instead of the remainder being between zero and the modulus, it can sit between two other numbers. This is useful for many different types of math problems. Whether it is for keeping secrets or making software fast, the modulo is a powerful idea. It turns the simple idea of leftovers into a major part of our digital world.

544 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (5)
File:Divmod truncated.svg
Divmod truncated.svg
File:Divmod floored.svg
Divmod floored.svg
File:Divmod Euclidean.svg
Divmod Euclidean.svg
File:Divmod rounding.svg
Divmod rounding.svg
File:Divmod ceiling.svg
Divmod ceiling.svg
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.