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.

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.

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.

The mod function is useful for
congruence relationships: x and y are congruent (mod m) if
and only if mod(x,m) == mod(y,m). For example,
23 and 13 are congruent (mod 5).