NumPy remainder()
The numpy.remainder()
function computes the element-wise remainder of division, similar to the modulus (%
) operator in Python. It complements numpy.floor_divide()
and ensures that the remainder has the same sign as the divisor.
Syntax
</>
Copy
numpy.remainder(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1 | array_like | Dividend array (numerator values). |
x2 | array_like | Divisor array (denominator values). Must be broadcastable with x1 . |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array to store results. If not provided, a new array is created. |
where | array_like, optional | Boolean condition array determining which elements are computed. |
casting | str, optional | Specifies casting behavior for the output array. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array containing the element-wise remainder from floor_divide(x1, x2)
. If both x1
and x2
are scalars, a scalar is returned.
Examples
1. Computing Remainder of Two Scalars
We compute the remainder of two scalar values using numpy.remainder()
.
</>
Copy
import numpy as np
# Define two scalar values
x1 = 10
x2 = 3
# Compute the remainder
result = np.remainder(x1, x2)
# Print the result
print("Remainder of 10 divided by 3:", result)
Output:
Remainder of 10 divided by 3: 1

2. Computing Remainder for Arrays
We apply numpy.remainder()
element-wise to an array.
</>
Copy
import numpy as np
# Define dividend and divisor arrays
x1 = np.array([10, 20, 30, 40])
x2 = np.array([3, 4, 5, 6])
# Compute the remainder for each element
remainder_values = np.remainder(x1, x2)
# Print the results
print("Dividends:", x1)
print("Divisors:", x2)
print("Remainder values:", remainder_values)
Output:
Dividends: [10 20 30 40]
Divisors: [3 4 5 6]
Remainder values: [1 0 0 4]

3. Using the out
Parameter
We use an output array to store the remainder values instead of creating a new array.
</>
Copy
import numpy as np
# Define input arrays
x1 = np.array([15, 25, 35, 45])
x2 = np.array([4, 5, 6, 7])
# Create an empty output array
output_array = np.empty_like(x1)
# Compute remainder and store it in output_array
np.remainder(x1, x2, out=output_array)
# Print the results
print("Computed remainder values:", output_array)
Output:
Computed remainder values: [3 0 5 3]

4. Using the where
Parameter
The where
parameter allows computing the remainder only for selected elements.
</>
Copy
import numpy as np
# Define input arrays
x1 = np.array([12, 22, 32, 42])
x2 = np.array([5, 4, 3, 2])
# Define a mask (compute remainder only where mask is True)
mask = np.array([True, False, True, False])
# Compute remainder values where mask is True
result = np.remainder(x1, x2, where=mask)
# Print the results
print("Computed remainder values with mask:", result)
Output:
Computed remainder values with mask: [2 0 2 0]

Only elements where mask=True
are computed.