R = rem(X,Y) returns
the remainder after division of X by Y.
In general, if Y does not equal 0, R
= rem(X,Y) returns X - n.*Y, where n
= fix(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 of them is a scalar double. If
one of the inputs 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:

rem(X,0) is NaN.

rem(X,X) for X~=0 is 0.

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

If Y is not an integer and X./Y is
within roundoff error of an integer, then rem 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 rem if
it is small enough.

R = rem(X,Y)

R =
0

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

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

R = rem(X,Y)

R =
1

M = mod(X,Y)

M =
-1

rem(X,Y) and mod(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 rem retains the sign
of X, while mod retains the
sign of Y.