Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
How to get the floor, ceiling and truncated values of the elements of a numpy array?
Next article icon

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

Last Updated : 29 Aug, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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.]  

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

S

sandhiya728
Improve
Article Tags :
  • Python
  • Python-numpy
  • Python numpy-Mathematical Function
Practice Tags :
  • python

Similar Reads

    How to get element-wise true division of an array using Numpy?
    True Division in Python3 returns a floating result containing the remainder of the division. To get the true division of an array, NumPy library has a function numpy.true_divide(x1, x2). This function gives us the value of true division done on the arrays passed in the function. To get the element-w
    2 min read
    How to get the n-largest values of an array using NumPy?
    Let's see the program for how to get the n-largest values of an array using NumPy library. For getting n-largest values from a NumPy array we have to first sort the NumPy array using numpy.argsort() function of NumPy then applying slicing concept with negative indexing. Syntax: numpy.argsort(arr, ax
    2 min read
    How to calculate the element-wise absolute value of NumPy array?
    Let's see the program for finding the element-wise absolute value of NumPy array. For doing this task we are using numpy.absolute() function of NumPy library. This mathematical function helps to calculate the absolute value of each element in the array. Syntax: numpy.absolute(arr, out = None, ufunc
    2 min read
    How to round elements of the NumPy array to the nearest integer?
    Prerequisites: Python NumPy In this article, let's discuss how to round elements of the NumPy array to the nearest integer. numpy.rint() function of Python that can convert the elements of an array to the nearest integer. Syntax: numpy.rint(x, /, out=None, *, where=True, casting='same_kind', order='
    1 min read
    How to get values of an NumPy array at certain index positions?
    Sometimes we need to remove values from the source Numpy array and add them at specific indices in the target array. In NumPy, we have this flexibility, we can remove values from one array and add them to another array. We can perform this operation using numpy.put() function and it can be applied t
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences