Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 29 Aug, 2020
View Discussion
Improve Article
Save Article

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

Getting the floor value

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 library
import numpy as np
  
  
# Initialize numpy array
a = np.array([1.2])
  
# Get floor value
a = 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.]

Getting the ceil value

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 library
import numpy as np
  
  
# Initialize numpy array
a = np.array([1.2])
  
# Get ceil value
a = 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.]

Getting the Truncate value

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 library
import numpy as np
  
  
# Initialize numpy array
a = np.array([1.2])
  
# Get truncate value
a = 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.]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!