mod

Remainder after division, with the sign of the divisor (modulo).

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

r = mod(a, m)

Returns a - m .* floor(a ./ m).

Description

Because the quotient is rounded down, the result takes the sign of m: mod(-7, 3) is 2, which fits clock arithmetic (mod(h, 24) always lands in 0..23). Compare rem, which takes the sign of the dividend and gives -1 here.

mod(a, 0) returns a. Floating-point operands are allowed, so mod(5.5, 2) is 1.5. For integer types the result keeps the class.

Input arguments

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

Output arguments

r
Result with 0 <= r < m for positive m, or m < r <= 0 for negative m.

Example

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

     1     2    -2
ans = 1.5000
ans = 5

See also

rem idivide floor gcd