w3resource

NumPy: Get the largest integer smaller or equal to the division of the inputs


4. Floor Division (Largest Integer Less or Equal)

Write a NumPy program to get the largest integer smaller or equal to the division of the inputs.

Sample Solution:

Python Code:

import numpy as np
x = [1., 2., 3., 4.]
print("Original array:")
print(x)
print("Largest integer smaller or equal to the division of the inputs:")
print(np.floor_divide(x, 1.5))

Sample Output:

Original array:                                                        
[1.0, 2.0, 3.0, 4.0]                                                   
Largest integer smaller or equal to the division of the inputs:        
[ 0.  1.  2.  2.]

Explanation:

In the above exercise -

x = [1., 2., 3., 4.] – This line defines a list of floats.

np.floor_divide(x, 1.5) – This line performs floor division of each element in x by 1.5.

Finally print() will print [0., 1., 2., 2.].


For more Practice: Solve these Related Problems:

  • Implement a function that performs floor division on two arrays and returns the largest integer less than or equal to each quotient.
  • Test the floor division on arrays with fractional values to confirm that the output matches np.floor of the division.
  • Compare np.floor_divide with manual floor operation on element-wise division of two arrays.
  • Apply floor division on an array and a scalar and verify that broadcasting yields the correct integer array.

Python-Numpy Code Editor:

Previous: Write a NumPy program to get true division of the element-wise array inputs.
Next: Write a NumPy program to get the powers of an array values element-wise.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.