rem

Remainder after division, with the sign of the dividend.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

r = rem(a, b)

Returns a - b .* fix(a ./ b).

Description

The quotient is truncated toward zero, so the remainder follows the sign of a: rem(-7, 3) is -1, while mod(-7, 3) is 2. For nonnegative operands the two functions agree.

rem(a, 0) is NaN for floating-point input. Decimals work too: rem(5.5, 2) is 1.5.

Input arguments

a
Dividend: numeric array.
b
Divisor: numeric array, compatible in size with a.

Output arguments

r
Remainder with the same sign as a and magnitude less than abs(b).

Example

rem(-7, 3)
rem(7, -3)
rem(10, [3 4])
rem(5, 0)
Output
ans = -1
ans = 1
ans = 1×2

     1     2
ans = NaN

See also

mod fix idivide gcd