Documentation Center

  • Trials
  • Product Updates

rem

Remainder after division

Syntax

Description

example

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.

Examples

expand all

Remainder of Two Scalars

Compute the remainder after dividing 5 into 23.

X = 23;
Y = 5;
R = rem(X,Y)
R =

     3

Remainder of a Vector

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

X = 1:5;
Y = 3;
R = rem(X,Y)
R =

     1     2     0     1     2

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

Remainder of Two Arrays

Create two 3-by-3 matrices, then use rem to find the remainder 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];
R = rem(X,Y)
R =

     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 rem

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.

Y = 2 - eps(4);
R = rem(X,Y)
R =

   8.8818e-16

Difference Between rem and mod

Define X and Y with different signs.

X = 5;
Y = -2;

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.

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

See Also

Was this topic helpful?