NumPy Tutorial NumPy Statistics NumPy References

NumPy - floor_divide() function



The NumPy floor_divide() function returns the largest integer smaller or equal to the division of the inputs, element-wise. The syntax for using this function is given below:

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

Syntax

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

Parameters

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 floor(x1/x2).

Example:

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

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

#divide each element of Arr1 by 5
print("floor_divide(Arr1, 5) returns:")
print(np.floor_divide(Arr1, 5))

#divideing elements of Arr1 by Arr2
#Arr1 and Arr2 are broadcastable
print("\nfloor_divide(Arr1, Arr2) returns:")
print(np.floor_divide(Arr1, Arr2))

#divideing elements of Arr1 by Arr3
#Arr1 and Arr3 are broadcastable
print("\nfloor_divide(Arr1, Arr3) returns:")
print(np.floor_divide(Arr1, Arr3))

#divideing elements of Arr1 by Arr4
print("\nfloor_divide(Arr1, Arr4) returns:")
print(np.floor_divide(Arr1, Arr4))

The output of the above code will be:

floor_divide(Arr1, 5) returns:
[[2 4]
 [6 8]]

floor_divide(Arr1, Arr2) returns:
[[ 5  6]
 [15 13]]

floor_divide(Arr1, Arr3) returns:
[[ 5 10]
 [10 13]]

floor_divide(Arr1, Arr4) returns:
[[5 6]
 [7 8]]

❮ NumPy - Functions