(Ad, please don’t block.)

`%`

is a remainder operator, not a modulo operator- 5.1 Remainder operator
`rem`

vs. modulo operator`mod`

- 5.2 An intuitive understanding of the remainder operation
- 5.3 An intuitive understanding of the modulo operation
- 5.4 Similarities and differences between
`rem`

and`mod`

- 5.5 The equations behind remainder and modulo
- 5.5.1
`rem`

and`mod`

perform integer division differently - 5.5.2 Implementing
`rem`

- 5.5.3 Implementing
`mod`

- 5.5.1
- 5.6 Where are
`rem`

and`mod`

used in programming languages? - 5.7 Further reading and sources of this chapter

*Remainder* and *modulo* are two similar operations. This chapter explores how they work and reveals that JavaScript’s `%`

operator computes the remainder, not the modulus.

`rem`

vs. modulo operator `mod`

In this chapter, we pretend that JavaScript has the following two operators:

- The
*remainder operator*`rem`

- The
*modulo operator*`mod`

That will help us examine how the underlying operations work.

The operands of the remainder operator are called *dividend* and *divisor*:

`remainder = dividend rem divisor`

How is the result computed? Consider the following expression:

We remove 3 from the dividend until we are left with a value that is smaller than 3:

What do we do if the dividend is negative?

This time, we add 3 to the dividend until we have a value that is smaller than -3:

It is insightful to map out the results for a fixed divisor:

```
x: -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
x rem 3: -1 0 -2 -1 0 -2 -1 0 1 2 0 1 2 0 1
```

Among the results, we can see a symmetry: `x`

and `-x`

produce the same results, but with opposite signs.

Once again, the operands are called *dividend* and *divisor* (hinting at how similar `rem`

and `mod`

are):

`modulus = dividend mod divisor`

Consider the following example:

This operation maps `x`

into the range:

`[0,3) = {0,1,2}`

That is, zero is included (opening square bracket), 3 is excluded (closing parenthesis).

How does `mod`

perform this mapping? It is easy if `x`

is already inside the range:

If `x`

is greater than or equal to the upper boundary of the range, then the upper boundary is subtracted from `x`

until it fits into the range:

That means we are getting the following mapping for non-negative integers:

```
x: 0 1 2 3 4 5 6 7
x mod 3: 0 1 2 0 1 2 0 1
```

This mapping is extended as follows, so that it includes negative integers:

```
x: -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
x mod 3: 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1
```

Note how the range [0,3) is repeated over and over again.

`rem`

and `mod`

`rem`

and `mod`

are quite similar – they only differ if dividend and divisor have different signs:

With

`rem`

, the result has the same sign as the dividend (first operand):With

`mod`

, the result has the same sign as the divisor (second operand):

The following two equations define both remainder and modulo:

```
dividend = (divisor * quotient) + remainder
|remainder| < |divisor|
```

Given a dividend and a divisor, how do we compute remainder and modulus?

```
remainder = dividend - (divisor * quotient)
quotient = dividend div divisor
```

The `div`

operator performs integer division.

`rem`

and `mod`

perform integer division differentlyHow can these equations contain the solution for *both* operations? If variable `remainder`

isn’t zero, the equations always allow two values for it: one positive, the other one negative. To see why, we turn to the question we are asking when determining the quotient:

How often do we need to multiply the divisor to get as close to the dividend as possible?

For example, that gives us two different results for dividend −2 and divisor 3:

`-2 rem 3 = -2`

3 × 0 gets us close to −2. The difference between 0 and the dividend −2 is −2.`-2 mod 3 = 1`

3 × −1 also gets us close to −2. The difference between −3 and the dividend −2 is 1.

It also gives us two different results for dividend 2 and divisor −3:

`2 rem -3 = 2`

-3 × 0 gets us close to 2.`2 mod -3 = -1`

-3 × -1 gets us close to 2.

The results differ depending on how the integer division `div`

is implemented.

`rem`

The following JavaScript function implements the `rem`

operator. It performs integer division by performing floating point division and rounding the result to an integer via `Math.trunc()`

:

```
function rem(dividend, divisor) {
const quotient = Math.trunc(dividend / divisor);
return dividend - (divisor * quotient);
}
```

`mod`

The following function implements the `mod`

operator. This time, integer division is based on `Math.floor()`

:

```
function mod(dividend, divisor) {
const quotient = Math.floor(dividend / divisor);
return dividend - (divisor * quotient);
}
```

Note that other ways of doing integer division are possible (e.g. based on `Math.ceil()`

or `Math.round()`

). That means that there are more operations that are similar to `rem`

and `mod`

.

`rem`

and `mod`

used in programming languages?`%`

operator computes the remainderFor example (Node.js REPL):

`%`

operator computes the modulusFor example (Python REPL):

```
>>> 7 % 6
1
>>> -7 % 6
5
```

The ECMAScript specification uses modulo several times – for example:

To convert the operands of the

`>>>`

operator to unsigned 32-bit integers (`x mod 2**32`

):To convert arbitrary numbers so that they fit into Typed Arrays. For example,

`x mod 2**8`

is used to convert numbers to unsigned 8-bit integers (after first converting them to integers):

- The Wikipedia page on “modulo operation” has much information.
- The ECMAScript specification mentions that
`%`

is computed via “truncating division”. - Sect. “Rounding” in “JavaScript for impatient programmers” describes several ways of rounding in JavaScript.