Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

mod, _mod

Modulo operator

For the mod function in MATLAB®, see mod.

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Syntax

x mod m
_mod(x, m)

Description

If m <> 0, then mod(x, m) returns the value x - n*m where n = floor(x/m). If x and m have different signs, then mod(x, m) has the same sign as m. If m = 0, then mod(x, m) returns x. See Example 1.

_mod(x, m) is the functional equivalent of the operator notation x mod m. See Example 2.

By default, x mod m and _mod(x, m) are both equivalent to modp(x, m). You can redefine the modulo operator mod and its functional form _mod by using modp and mods. For example, after the assignment _mod:=mods, both the operator mod and the equivalent function _mod return remainders of least absolute value. See Example 3.

All functions return an error when one of the arguments is a floating-point number, a complex number, or not an arithmetical expression.

If one of the arguments is not a number, then a symbolic function call is returned. See Example 4.

_mod and modp are kernel functions.

Examples

Example 1

Find the modulus after division of these integers.

27 mod 4, 27 mod -4, -27 mod 4, -27 mod -4

Find the modulus after division by zero.

9 mod 0, -9 mod 0, 0 mod 0

Find the modulus after division of these rational numbers.

22/3 mod 5, 22 mod 5/3, 22/3 mod 5/4

Example 2

Find the modulus after division of 23 by 5 using the modulo operator and its functional form. _mod and the operator mod are equivalent.

hold(_mod(23, 5))

23 mod 5 = _mod(23, 5)

Example 3

By default the binary operator mod and the equivalent function _mod are both equivalent to modp. You can redefine _mod.

modp(11, 7), mods(11,7);
11 mod 7

_mod := mods:
11 mod 7

For further computations, define _mod as modp.

_mod := modp:

Example 4

If one of the arguments is not a number, then the modulo operator returns a symbolic function call.

delete x, m:
x mod m, x mod 2, 2 mod m

When called with nonnumeric arguments, the function currently associated with _mod is printed in the operator notation.

_mod := mods:
modp(x, m), mods(x, m)

_mod := modp:
modp(x, m), mods(x, m)

Related Examples

Parameters

x

An integer, a rational number, or an arithmetical expression

m

An integer or an arithmetical expression

Overloaded By

m, x

See Also

MuPAD Domains

MuPAD Functions

Was this topic helpful?