How to get the floor, ceiling and truncated values of the elements of a numpy array? - GeeksforGeeks

29 Aug, 2020
In this article, let’s discuss how to get the floor, ceiling, and truncated values of the elements of a Numpy array. First, we need to import the NumPy library to use all the functions available in it. This can be done with this import statement:
import numpy as np

The greatest integer that is less than or equal to x where x is the array element is known as floor value. It can found using the function numpy.floor()
Syntax:
numpy.floor(x[, out]) = ufunc ‘floor’) 

Example 1: 
Python
# Import the numpy libraryimport numpy as np    # Initialize numpy arraya = np.array([1.2])  # Get floor valuea = np.floor(a)print(a)
Output: 
[1.]

Example 2:
Python
import numpy as np    a = np.array([-1.8, -1.6, -0.5, 0.5,              1.6, 1.8, 3.0])  a = np.floor(a)print(a)
OutPut: 
[-2., -2., -1., 0., 1., 1., 3.]

The least integer that is greater than or equal to x where x is the array element is known as ceil value. It can be found using the numpy.ceil() method.
Syntax:
numpy.ceil(x[, out]) = ufunc ‘ceil’) 

Example 1:
Python
# Import the numpy libraryimport numpy as np    # Initialize numpy arraya = np.array([1.2])  # Get ceil valuea = np.ceil(a)print(a)
Output: 
[2.]

Example 2:
Python
import numpy as np    a = np.array([-1.8, -1.6, -0.5, 0.5,              1.6, 1.8, 3.0])  a = np.ceil(a)print(a)
Output:
[-1., -1., -0., 1., 2., 2., 3.]

The trunc of the scalar x is the nearest integer i which, closer to zero than x. This simply means that, the fractional part of the signed number x is discarded by this function. It can be found using the numpy.trunc() method.
Syntax:
numpy.trunc(x[, out]) = ufunc ‘trunc’)

Example 1:
Python
# Import the numpy libraryimport numpy as np    # Initialize numpy arraya = np.array([1.2])  # Get truncate valuea = np.trunc(a)print(a)
Output:
[1.]

Example 2:
Python
import numpy as np    a = np.array([-1.8, -1.6, -0.5, 0.5,              1.6, 1.8, 3.0])  a = np.trunc(a)print(a)
Output:
[-1., -1., -0., 0., 1., 1., 3.]

Example to get floor, ceil, trunc values of the elements of a numpy array
Python
import numpy as np    input_arr = np.array([-1.8, -1.6, -0.5, 0.5,                       1.6, 1.8, 3.0])print(input_arr)  floor_values = np.floor(input_arr)print("\nFloor values : \n", floor_values)  ceil_values = np.ceil(input_arr)print("\nCeil values : \n", ceil_values)  trunc_values = np.trunc(input_arr)print("\nTruncated values : \n", trunc_values)
Output:
[-1.8 -1.6 -0.5  0.5  1.6  1.8  3. ]

Floor values : 
 [-2. -2. -1.  0.  1.  1.  3.]

Ceil values : 
 [-1. -1. -0.  1.  2.  2.  3.]

Truncated values : 
 [-1. -1. -0.  0.  1.  1.  3.]

Python numpy-Mathematical Function
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()