NumPy floor_divide()
The numpy.floor_divide()
function performs element-wise floor division, returning the largest integer less than or equal to the division result. It is equivalent to the Python //
operator.
Syntax
</>
Copy
numpy.floor_divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1 | array_like | Numerator array. |
x2 | array_like | Denominator array. Must be broadcastable to the shape of x1 . |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where results are stored. If None , a new array is created. |
where | array_like, optional | Boolean mask specifying where to apply the operation. |
casting | str, optional | Defines the casting behavior when computing floor division. |
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 where each element is the floor division result of the corresponding elements in x1
and x2
. If both inputs are scalars, a scalar is returned.
Examples
1. Floor Division of Two Scalars
In this example, we compute the floor division of two scalar values.
</>
Copy
import numpy as np
# Define two scalar values
numerator = 7
denominator = 3
# Compute floor division
result = np.floor_divide(numerator, denominator)
# Print the result
print("Floor division of 7 by 3:", result)
Output:
Floor division of 7 by 3: 2

2. Floor Division of Arrays
Here, we apply floor_divide
element-wise on two arrays.
</>
Copy
import numpy as np
# Define numerator and denominator arrays
x1 = np.array([7, 10, 15, 20])
x2 = np.array([3, 4, 6, 7])
# Compute floor division
result = np.floor_divide(x1, x2)
# Print the results
print("Numerator array:", x1)
print("Denominator array:", x2)
print("Floor division result:", result)
Output:
Numerator array: [ 7 10 15 20]
Denominator array: [3 4 6 7]
Floor division result: [2 2 2 2]

3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
</>
Copy
import numpy as np
# Define numerator and denominator arrays
x1 = np.array([8, 16, 22, 35])
x2 = np.array([4, 5, 7, 6])
# Create an output array
output_array = np.empty_like(x1)
# Compute floor division and store in output_array
np.floor_divide(x1, x2, out=output_array)
# Print the result
print("Floor division result stored in output array:", output_array)
Output:
Floor division result stored in output array: [2 3 3 5]

4. Using the where
Parameter
Using a condition to apply floor division only to selected elements.
</>
Copy
import numpy as np
# Define numerator and denominator arrays
x1 = np.array([10, 25, 30, 40])
x2 = np.array([2, 5, 7, 8])
# Define a mask (apply floor division only where mask is True)
mask = np.array([True, False, True, False])
# Compute floor division where mask is True
result = np.floor_divide(x1, x2, where=mask)
# Print the result
print("Floor division result with condition:", result)
Output:
Floor division result with condition: [ 5 0 4 0]

The floor division is computed only for elements where mask=True
. The other values remain unchanged.