1 |
|
2 |
|
3 | # Function mod
|
4 |
|
5 | Calculates the modulus, the remainder of an integer division.
|
6 |
|
7 | For matrices, the function is evaluated element wise.
|
8 |
|
9 | The modulus is defined as:
|
10 |
|
11 | x - y * floor(x / y)
|
12 |
|
13 | See https://en.wikipedia.org/wiki/Modulo_operation.
|
14 |
|
15 |
|
16 | ## Syntax
|
17 |
|
18 | ```js
|
19 | math.mod(x, y)
|
20 | ```
|
21 |
|
22 | ### Parameters
|
23 |
|
24 | Parameter | Type | Description
|
25 | --------- | ---- | -----------
|
26 | `x` | number | BigNumber | Fraction | Array | Matrix | Dividend
|
27 | `y` | number | BigNumber | Fraction | Array | Matrix | Divisor
|
28 |
|
29 | ### Returns
|
30 |
|
31 | Type | Description
|
32 | ---- | -----------
|
33 | number | BigNumber | Fraction | Array | Matrix | Returns the remainder of `x` divided by `y`.
|
34 |
|
35 |
|
36 | ## Examples
|
37 |
|
38 | ```js
|
39 | math.mod(8, 3) // returns 2
|
40 | math.mod(11, 2) // returns 1
|
41 |
|
42 | function isOdd(x) {
|
43 | return math.mod(x, 2) != 0
|
44 | }
|
45 |
|
46 | isOdd(2) // returns false
|
47 | isOdd(3) // returns true
|
48 | ```
|
49 |
|
50 |
|
51 | ## See also
|
52 |
|
53 | [divide](divide.md)
|