Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# mod

Modulus after division

## Description

example

M = mod(X,Y) returns the modulus after division of X by Y. In general, if Y does not equal 0, M = mod(X,Y) returns X - n.*Y, where n = floor(X./Y). If Y is not an integer and the quotient X./Y is within roundoff error of an integer, then n is that integer. Inputs X and Y must have the same dimensions unless one is a scalar double. If one input has an integer data type, then the other input must be of the same integer data type or be a scalar double.

The following are true by convention:

• mod(X,0) is X.

• mod(X,X) is 0.

• mod(X,Y) for X~=Y and Y~=0 has the same sign as Y.

## Examples

expand all

### Modulus of Two Scalars

Compute 23 modulo 5.

```X = 23;
Y = 5;
M = mod(X,Y)```
```M =

3```

### Modulus of a Vector

Create a vector, then use mod to find the modulus after dividing a scalar into each element of the vector.

```X = 1:5;
Y = 3;
M = mod(X,Y)```
```M =

1     2     0     1     2```

When you specify one or more of the inputs as an array, the mod function acts on each array element independently.

### Modulus of Two Arrays

Create two 3-by-3 matrices, then use mod to find the modulus after dividing Y into X.

```X = [1 2 3;4 5 6;7 8 9];
Y = [9 8 7;6 5 4;3 2 1];
M = mod(X,Y)```
```M =

1     2     3
4     0     2
1     0     0```

Inputs X and Y must have the same dimensions unless one is a scalar double.

### Forced Rounding in mod

If Y is not an integer and X./Y is within roundoff error of an integer, then mod rounds to that integer for its calculation. The size of the roundoff error is very small.

```X = 2;
Y = 2 - eps(2)
```
```Y =

2.0000```

It looks like Y is trivially equal to 2, but in fact there is an infinitesimal difference.

```2 - Y
```
```ans =

4.4409e-16```

This difference is forced to zero by mod if it is small enough.

```M = mod(X,Y)
```
```M =

0```

Make the difference a little larger and the forced rounding disappears.

```Y = 2 - eps(4);
M = mod(X,Y)
```
```M =

8.8818e-16```

### Difference Between mod and rem

Define X and Y with different signs.

```X = 5;
Y = -2;
```

Compute the modulus after division with mod, then compute the remainder after division with rem.

`M = mod(X,Y)`
```M =

-1```
`R = rem(X,Y)`
```R =

1```

mod(X,Y) and rem(X,Y) are equal if X and Y have the same sign, but differ by Y if X and Y have different signs. Notice that mod retains the sign of Y, while rem retains the sign of X.

## Input Arguments

expand all

### X — Dividendscalar | vector | matrix | multidimensional array

Dividend, specified as a scalar, vector, matrix, or multidimensional array. Must be a real-valued number of any numerical type.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char

### Y — Divisorscalar | vector | matrix | multidimensional array

Divisor, specified as a scalar, vector, matrix, or multidimensional array. Must be a real-valued number of any numerical type.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char