Time for action – computing the modulo
Let's call the previously mentioned functions:
- The
remainder()
function returns the remainder of the two arrays, element-wise.0
is returned if the second number is0
:a = np.arange(-4, 4) print("Remainder", np.remainder(a, 2))
The result of the
remainder()
function is shown as follows:Remainder [0 1 0 1 0 1 0 1]
- The
mod()
function does exactly the same as theremainder()
function:print("Mod", np.mod(a, 2))
The result of the
mod()
function is shown as follows:Mod [0 1 0 1 0 1 0 1]
- The
%
operator is just shorthand for theremainder()
function:print("% operator", a % 2)
The result of the
%
operator is shown as follows:% operator [0 1 0 1 0 1 0 1]
- The
fmod()
function handles negative numbers differently than ...
Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.