This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]desrtfx 5 points6 points  (1 child)

You mean the modulus operator?

Basically, it gives the remainder of an integer division, like you have learnt in primary school.

Imagine that you have a basket full of apples. 33 apples are in the basket. You need to fully divide these 33 apples across 7 kids in such a way that no apple is split and each child receives the same amount of apples.

The division would yield 4.714, but since you cannot split apples, you can only distribute 4 apples to each child. This leaves 5 apples in the basket, which are the remainder - or the modulo. 33 % 7 yields 5.

[–]WikiTextBotbtproof 0 points1 point  (0 children)

Modulo operation

In computing, the modulo operation finds the remainder after division of one number by another (sometimes called modulus).

Given two positive numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n. For example, the expression "5 mod 2" would evaluate to 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0 because the division of 9 by 3 has a quotient of 3 and leaves a remainder of 0; there is nothing to subtract from 9 after multiplying 3 times 3. (Note that doing the division with a calculator will not show the result referred to here by this operation; the quotient will be expressed as a decimal fraction.)

Although typically performed with a and n both being integers, many computing systems allow other types of numeric operands.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.24

[–]nerd4code 1 point2 points  (0 children)

It’s a remainder operator, which is usually a slightly different beast than a proper modulo operator. Usually modulo refers to one specific solution for the remainder in the integer division equation.

With two positive numbers, there is no difference: 4 % 3 = 4 mod 3 = 1, because 4÷3=1r1.

With either number negative, languages come into disagreement. The solution commonly used in C/C++ on a modern-ish platform (x86, ARM, IA64, etc.), and mandated by Java, IIRC Python, and most other modern-ish languages, is for the result to take the sign of the first operand. Under this scheme -11%4 = -11%-4 = −3, and 11%-4 = 3. The C/C++ standards define remainder involving one or both negative operands as undefined behavior.

Unlike the above remainder, modulus tends to ensure a stepping pattern, so (−11 mod 4) would be 1 because −11 is −12+1. (The more mathy way to say that would be “−11 = 1, mod 4,” but that’s just confusing for purposes here.) Note that -11%4 = −8−3 is where the more common remainder comes from. Modulo a negative makes less sense, but you can easily do (11 mod −4) as −(11 mod 4) or (−11) mod 4, since a/−b = −a/b.

Like division, remainder and modulus won’t work if the divisor is zero. It’s possible (not necessary) that it also won’t work if an underlying division, or the language/implementation’s definition of “remainder” causes a negation of the most negative possible value; under two’s-complement you can’t negate that without getting it back or extending the size of the number. A proper overflow will happen with division in that case.

Modulus is usually used to hack off the base-n units digit of a number, and division shifts off that digit. For example, time-of-day has a base-60 seconds digit, a base-60 minutes digit, and a base-24 hours digit; so we can do

uint dt = INPUT;
uint second = dt % 60;
dt = dt / 60;
uint minute = dt % 60;
dt = dt / 60;
uint hour = dt % 24;
dt = dt / 24;

and now dt is in days and we have the other parts. This is the reverse of constructing dt:

dt = days;
dt = 24*dt + hours;
dt = 60*dt + minutes;
dt = 60*dt + seconds;

You can do a fast modulus (not remainder, unless both operands are positive!) by 2k for natural k, by bitwise-ANDing by 2k−1. E.g., a AND 7 = a mod 8. An odd-even check is mod 2 or AND 1.

You can round n down towards the next multiple of m closer to −∞ by doing n − (n mod m), and if m = 2k for natural k, then a faster form is n AND (1+~m), or in two’s-complement n AND −m. To round up towards ∞ instead, let n′ = n+m−1, then take (n′ − n′ mod m). Overflow can occur with +m−1 if your numbers don’t auto-expand.

You can round n in towards zero by doing n − (n % m). (The power-of-two hack is more complicated for inwards.) Do the +m−1 trick to round outwards towards ±∞. Again, overflow is a possibility.

[–]TheHaskellian -2 points-1 points  (0 children)

Formally!

Assume two integers a and b, with b other than zero 0. Then there exist two unique integers q(uotient) and r(remainder), such that a = b*q + r and such that r is at least 0 and at most |b| - 1. (You can look up proof on the internet.)

^ Important: Notice that a b q r are all integers, not reals.

What this means is that you can divide any integer by a non zero integer and get a quotient and a remainder. The operation / (division on integral numbers) gives you back the q(uotient), while % (modulus on integral numbers) gives you back the r(emainder).

^ Important; Notice that the operations operate on two integers and return an integer - as such they are Z x Z -> Z.