w3resource

NumPy: Calculate round, floor, ceiling, truncated and round of the input, element-wise of a given array

NumPy Mathematics: Exercise-26 with Solution

Write a NumPy program to calculate round, floor, ceiling, truncated and round (to the given number of decimals) of the input, element-wise of a given array.

Sample Solution:-

Python Code:

# Importing the NumPy library
import numpy as np

# Creating an array with various float values
x = np.array([3.1, 3.5, 4.5, 2.9, -3.1, -3.5, -5.9])

# Displaying the original array
print("Original array: ")
print(x)

# Using NumPy's around function to round the values to the nearest integer
r1 = np.around(x)

# Using NumPy's floor function to round down to the nearest integer
r2 = np.floor(x)

# Using NumPy's ceil function to round up to the nearest integer
r3 = np.ceil(x)

# Using NumPy's trunc function to truncate the decimal part of each value
r4 = np.trunc(x)

# Using Python's built-in round function for each element in the array
r5 = [round(elem) for elem in x]

# Printing the results of each operation
print("\naround:   ", r1)
print("floor:    ", r2)
print("ceil:     ", r3)
print("trunc:    ", r4)
print("round:    ", r5) 

Sample Output:

Original array: 
[ 3.1  3.5  4.5  2.9 -3.1 -3.5 -5.9]

around:    [ 3.  4.  4.  3. -3. -4. -6.]
floor:     [ 3.  3.  4.  2. -4. -4. -6.]
ceil:      [ 4.  4.  5.  3. -3. -3. -5.]
trunc:     [ 3.  3.  4.  2. -3. -3. -5.]
round:     [3.0, 4.0, 4.0, 3.0, -3.0, -4.0, -6.0]

Explanation:

In the above code –

x = np.array([3.1, 3.5, 4.5, 2.9, -3.1, -3.5, -5.9]) – This line generates a one-dimensional array named x containing 7 float values.

r1 = np.around(x) returns the evenly rounded integer values to the nearest integer of the elements of array x. If the decimal value is exactly halfway between two integers, np.around() rounds it to the nearest even integer. So, r1 contains the output of np.around(x).

r2 = np.floor(x) returns the largest integer less than or equal to the elements of array x. So, r2 contains the output of np.floor(x).

r3 = np.ceil(x) returns the smallest integer greater than or equal to the elements of array x. So, r3 contains the output of np.ceil(x).

r4 = np.trunc(x) returns the truncated value of the elements of array x. It returns the integer value towards zero. So, r4 contains the output of np.trunc(x).

r5 = [round(elem) for elem in x]: This code computes the rounding off of elements in the array x using the built-in round() function in Python. It iterates over the elements of x and rounds each element to the nearest integer. The output of this operation is stored in r5.

Python-Numpy Code Editor:

Previous: Write a NumPy program to calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent for all elements in a given array.

Next: Write a NumPy program to calculate cumulative sum of the elements along a given axis, sum over rows for each of the 3 columns and sum over columns for each of the 2 rows of a given 3x3 array.

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.