# numpy.floor_divide() in Python: A Detailed Guide

The numpy.floor_divide() function in Python is used to perform element-wise floor division of two arrays. It returns the largest integer that is less than or equal to each element in the resulting array. This function is particularly useful when we want to obtain the integer quotient after division, discarding any remainder.

In this article, we will understand Python numpy.floor_divide() function, its syntax, and we will also see the variety of ways using which division can be done on arrays with the help of this function. Let’s get started.

Also Read: numpy.float_power() in Python – Get Element-wise Power of Array

## Syntax of numpy.floor_divide() Function

Let us get started by understanding the fundamental constituents of the numpy.floor_divide() function with the help of its syntax given below.

Syntax:

```numpy.floor_divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None)
```

Parameters:

• x1: This is the dividend array or the scalar value.
• x2: This is the divisor array or the scalar value.
• out (optional): This is an optional parameter that specifies the location where the result should be stored. If not provided, a new array will be created.
• where (optional): This parameter is used for conditional execution. If provided, only the elements where the condition is True will be included in the result.
• casting (optional): This parameter controls what kind of data casting may occur. The default is ‘same_kind’, which means that only casting between similar kinds (e.g., float to float) is allowed.
• order (optional): Specifies the memory layout of the result. The default is ‘K’ which means the order is determined by the input array’s memory layout.
• dtype (optional): This parameter allows us to set the data type of the output array. If not specified, it is determined by the data types of the input arrays.

Return:

The numpy.floor_divide() function returns an array with the same shape as the input arrays, where each element is the largest integer less than or equal to the corresponding elements in x1 divided by x2.

## Examples of numpy.floor_divide() Function

Let us now look at some examples to demonstrate the use of numpy.floor_divide() function in Python.

### Example 1: 1D Arrays with numpy.floor_divide()

Let’s first see the example in which we pass two one-dimensional arrays into the numpy.floor_divide() which will perform the element-wise floor division between them.

```import numpy as np
ar1 = np.array([11, 24, 21])
ar2 = np.array([4, 4, 19])
result = np.floor_divide(ar1, ar2)
print(result)
```

In the above code, we first imported the NumPy library as np and with the help of it, we created two 1D arrays. Then we passed these two arrays in the numpy.floor_divide() function that will perform element-wise division and calculate floor value which results in a 1D array.

Output:

Here we can see that we got the first element in our resulting array as 2 because the first element of the first array ar1 got divided by the first element of the second array ar2 which gives 2.75 but after calculating its floor value it becomes 2.

In the same manner, 24/4 = 6, and the floor value of 6 is 6 so the second element in our resulting array is 6. Now if we see the third element of the resulting array it’s 1 because 21/19=1.1 and the floor value of 1.1 is 1. So our resulting array is [2,6,1]

### Example 2: When Input Arrays Contain Negative Elements

Let’s see how to compute floor division when we pass the arrays containing the negative elements into the numpy.floor_divide() function.

```import numpy as np
ar1 = np.array([-10, 20, -30])
ar2 = np.array([3, -2, 6])
result = np.floor_divide(ar1, ar2)
print(result)
```

Output:

We can see that we got our output array in the same manner as we got in the above example.

The first element in the output array is -4 as if we do -10/3 = -3.3 and the floor value of -3.3 is -4. In the same way, we got the second element as -10 and the third element as -5 in the output array.

### Example 3: When One Input Is Scalar and the Other Is an Array

Now let’s do the floor division by taking one of the inputs as a scalar and the other will be a one-dimensional array.

```import numpy as np
ar = np.array([14, 8, 19])
sc = 3
result = np.floor_divide(ar, sc)
print(result)
```

In this example, we have taken a scaler value of 3 which we passed with our array ar in the numpy.floor_divide() function means this function computes the resultant array by dividing scalar value sc with each element from our array and calculating its floor value.

Output:

Firstly, when the first element of the input array which was 14 got divided by the scalar value which was 3 then we got 4.6 and after calculating the floor value of 4.6 we got 4 so that’s why we got 4 as the first element in our resultant array.

Similarly, 8/3= 2.6, and the floor value of 2.6 is 2 so the second element in the result array is 2 and for the third element 19/3 = 6.3, and the floor value of 6.3 is 6 so the third element in the resultant array is 6.

### Example 4: Passing ‘dtype’ Parameter in numpy.floor_divide() Function

As we know numpy.floor_divide() function by default returns us the integer array but we can change the datatype of our resulting array by passing some different datatype to the parameter ‘dtype’ in the numpy.floor_divide() function.

```ar1= np.array([13,15,38])
ar2 = np.array([6,4,2])
output_datatype = np.float64
result = np.floor_divide(ar1, ar2, dtype=output_datatype)
print(result)
```

In the above code, we have passed one extra parameter ‘dtype’ as float into the numpy.floor_divde() function which changed the datatype of our resulting array to float.

Output:

We can see that we got the elements of datatype float in our output array.

## Difference Between numpy.floor_divide() and numpy.true_divide() Function

The main difference between numpy.floor_divide() and numpy.true_divide() exists in the type of division both functions perform and the type of result they return.

Let’s first see the type of division both functions perform. The numpy.floor_divide() function performs the floor division and the result that we get is the largest integer that is less than or equal to the division result while in the numpy.true_divide() function performs floating-point division.

When we talk about the result that they return then numpy.floor_divide() function discards the fractional part of the division result means it returns us the integer array whereas the numpy.true_divide() always returns the floating-point result of the division which also includes the decimal part means it returns us the floating-point array.

In simple words, if we want the exact floating-point result of the division including the decimal part then we can use numpy.true_divide() function and if we want the integer result that excludes the decimal part then we can use numpy.floor_divide() function.

## Conclusion

Now that we have reached the end of this article, hope it has elaborated on the different ways to find the element-wise floor division of the arrays by using the numpy.floor_divide() function from the NumPy library. For individuals who want to advance in Python, CodeforGeek has a ton of other entertaining and equally educational articles that could be quite beneficial.

## Reference

https://numpy.org/doc/stable/reference/generated/numpy.floor_divide.html

Articles: 44