Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# mod, _mod

Modulo operator

### Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

## Syntax

```x mod m
_mod(x, m)
```

## Description

By default, x mod m and _mod(x, m) are both equivalent to modp(x, m).

modp(x, m) computes the unique nonnegative remainder on division of the integer x by the integer m.

If m is a non-zero integer and x is an integer, then modp returns an integer r such that x = qm + r holds for some integer q. In addition, we have 0 ≤ r < |m| for modp. See Example 2. These conditions uniquely define r. In the modp case, we have q = x div m.

If m is a non-zero integer and x is a rational number, say for two non-zero coprime integers u and v, then modp computes an integral solution r of the congruence . To this end, it first computes an inverse w of v modulo m, such that vw - 1 is divisible by m. This only works if v is coprime to m, i.e., if their greatest common divisor is 1. Then modp(u*w, m), as described above, is returned. Otherwise, if v and m are not coprime, then an error message is returned. See Example 2.

The number x - modp(x, m) is not an integral multiple of m in this case.

If the second argument m is 0 or a rational number, then an error message is returned.

_mod(x, m) is the functional equivalent of the operator notation x mod m. See Example 1.

By default, _mod is equivalent to modp.

The function modp can be used to redefine the modulo operator. E.g., after the assignment _mod:=mods, both the operator mod and the equivalent function _mod return remainders of least absolute value. See Example 3.

All functions return an error when one of the arguments is a floating-point number, a complex number, or not an arithmetical expression.

If one of the arguments is not a number, then a symbolic function call is returned. See Example 4.

_mod and modp are kernel functions.

## Environment Interactions

By default the operator mod and the function _mod are equivalent to modp. This can be changed by assigning a new value to _mod. See Example 3.

## Examples

### Example 1

The example demonstrates the correspondence between the function _mod and the operator mod:

`hold(_mod(23,5))`

`23 mod 5 = _mod(23,5)`

### Example 2

Here are some examples where the modulus is an integer. We see that mod and modp are equivalent by default:

`27 mod 3, 27 mod 4, modp(27, 4), mods(27, 4)`

`27 = (27 div 4)*4 + modp(27, 4)`

Let us now compute modulo 5. The greatest common divisor of 3 and 5 is 1, and 2 is an inverse of 3 modulo 5. Thus modulo 5 equals 22 *2 modulo 5:

```modp(22/3, 5) = modp(22*2, 5),
mods(22/3, 5) = mods(22*2, 5)```

The greatest common divisor of 15 and 27 is 3, so that 15 has no inverse modulo 27 and the following command fails:

`modp(-22/15, 27)`
```Error: The modular inverse does not exist. [modp]
```

However, we can compute modulo 26, since 15 and 26 are coprime:

`-22/15 mod 26`

### Example 3

By default the binary operator mod and the equivalent function _mod are both equivalent to modp. This can be changed by redefining _mod:

`11 mod 7, modp(11, 7), mods(11,7)`

```_mod := mods: 11 mod 7;
_mod := modp:```

### Example 4

If one of the arguments is not a number, then a symbolic function call is returned:

```delete x, m:
x mod m, x mod 2, 2 mod m```

When called with non-numeric arguments, the function coinciding with _mod is printed in the operator notation:

`_mod := mods: modp(x, m), mods(x, m)`

`_mod := modp: modp(x, m), mods(x, m)`

## Parameters

 x An integer, a rational number, or an arithmetical expression m An integer or an arithmetical expression