w3resource logo
Python Exercises

Python NumPy: Get the floor, ceiling and truncated values of the elements of an numpy array

Python NumPy Math, Statistics, Trigonometry and Linear algebra: Exercise-10 with Solution

Write a Python program to get the floor, ceiling and truncated values of the elements of an numpy array.

Sample Solution:-

Python Code:

import numpy as np
x = np.array([-1.6, -1.5, -0.3, 0.1, 1.4, 1.8, 2.0])
print("Original array:")
print(x)
print("Floor values of the above array elements:")
print(np.floor(x))
print("Ceil values of the above array elements:")
print(np.ceil(x))
print("Truncated values of the above array elements:")
print(np.trunc(x))

Sample Output:

Original array:                                                        
[-1.6 -1.5 -0.3  0.1  1.4  1.8  2. ]                                   
Floor values of the above array elements:                              
[-2. -2. -1.  0.  1.  1.  2.]                                          
Ceil values of the above array elements:                               
[-1. -1. -0.  1.  2.  2.  2.]                                          
Truncated values of the above array elements:                          
[-1. -1. -0.  0.  1.  1.  2.]

Python Code Editor:

Improve this sample solution and post your code through Disqus