NumPy Tutorial NumPy Statistics NumPy References

NumPy - fmod() function

The NumPy fmod() function returns element-wise remainder of division. The syntax for using this function is given below:

Note: It is equivalent to x1 % x2 in terms of array broadcasting.


numpy.fmod(x1, x2, out=None)


x1, x2 Required. Specify arrays to be divided: x1 as dividend and x2 as divisor. If x1.shape != x2.shape, they must be broadcastable to a common shape.
out Optional. Specify a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.

Return Value

Returns remainder of division of x1 and x2, element-wise.


The example below shows the usage of fmod() function.

import numpy as np
Arr1 = np.array([[10,20],[30,40]])
Arr2 = np.array([[6,7]])
Arr3 = np.array([[6],[7]])
Arr4 = np.array([[4,7],[5,9]])

#remainder when each element of Arr1
#is divided by 5
print("fmod(Arr1, 5) returns:")
print(np.fmod(Arr1, 5))

#remainder when Arr1 is divided by Arr2
#Arr1 and Arr2 are broadcastable
print("\nfmod(Arr1, Arr2) returns:")
print(np.fmod(Arr1, Arr2))

#remainder when Arr1 is divided by Arr3
#Arr1 and Arr3 are broadcastable
print("\nfmod(Arr1, Arr3) returns:")
print(np.fmod(Arr1, Arr3))

#remainder when Arr1 is divided by Arr4
print("\nfmod(Arr1, Arr4) returns:")
print(np.fmod(Arr1, Arr4))

The output of the above code will be:

fmod(Arr1, 5) returns:
[[0 0]
 [0 0]]

fmod(Arr1, Arr2) returns:
[[4 6]
 [0 5]]

fmod(Arr1, Arr3) returns:
[[4 2]
 [2 5]]

fmod(Arr1, Arr4) returns:
[[2 6]
 [0 4]]

❮ NumPy - Functions