Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconPython Program for Floor Function

Python Program for Floor Function

Last updated:
1st Jun, 2023
Views
Read Time
4 Mins
share image icon
In this article
Chevron in toc
View All
Python Program for Floor Function

The math.floor function Python is one of the math functions contained in the math library of Python. The math.floor function in Python returns the smallest integer value less than or identical to the defined expression or value.

Syntax of the Floor Function

The floor Function in the Python math library has the following syntax:

math.FLOOR(Expression)

Floor Function Number Theory

math.floor(x)

The x’s floor which is the greatest integer but less than or same as x is returned.

Syntax:

import math

math.floor(x)

Parameter:

x-numeric expression.

Returns:

An integer that is largest but not more than x.

The Python’s floor function returns an integer value close to the given numeric value. However, the returned integer value should be  equal to or less than the given numeric value. Let me show you a straightforward example of a floor function that returns the closet value of three numbers given below. 

The floor() method in Python is implemented as follows

# prints the ceil using floor() method

print "math.floor(-23.11) : ", math.floor(-23.11)

print "math.floor(300.16) : ", math.floor(300.16)

print "math.floor(300.72) : ", math.floor(300.72)

Explore our Popular Data Science Courses

Output 

math.floor(-23.11) : -24.0

math.floor(300.16) : 300.0

math.floor(300.72) : 300.0

Difference between int and floor Function in Python

Python’s floor() function returns a float value. Int() returns an integer (and in some cases, an overflow error). There may be a few explanations why having floor() might be helpful.

  • floor() is the mathematical floor function and int() is equivalent to sgn(x)|x| in math
  • When negative numbers are used the distinction is clear.

Examples:

Let’s look at a few examples to understand the usage of the floor() function in Python:

Example 1:

import math

x = 3.7

result = math.floor(x)

print(result)  

# Output: 3

Example 2:

import math

y = -2.5

result = math.floor(y)

print(result)  

# Output: -3

Example 3:

bring in math

math.floor(-3.5)

-4 

int (-3.5)

-3

Negative numbers are rounded down to get closer to 0 while truncating them gets them closer to 0.

To look at it another way, the floor() would still be less than or identical to the original. Int() will return a value that is closer to zero or equal.

Check Out upGrad’s Data Science Courses 

What are Ceil() and Floor() in Python?

The floor and ceil in Python are mathematical functions used to round numbers in Python. Both functions are part of the math module and provide different rounding behaviors.

Ceil() in Python

The ceil() function, also known as the ceiling function, rounds a given number up to the nearest integer. It returns the smallest integer greater than or equal to the original value. This function is useful when you need to obtain a whole number that is greater than or equal to a given value.

Floor() in Python

The floor function in Python, as mentioned earlier, rounds a given number down to the nearest integer. It returns the largest integer less than or equal to the original value. The python math floor function is commonly used when you want to obtain a whole number that is less than or equal to a given value.

What is the Difference Between the Two?

The main difference between the ceil() function and floor function Python is in their rounding behaviors. The ceil() function always rounds up to the nearest integer, while the floor() function always rounds down to the nearest integer.

For example, if we have a number 3.7, the ceil() function will round it up to 4, whereas the floor() function will round it down to 3.

Pros and Cons of Using These Functions

Using the ceil() and floor() functions in Python has its advantages and disadvantages.

Pros:

  • They provide a way to obtain rounded integer values from floating-point numbers.
  • They are simple to use and readily available in the math module.
  • They ensure consistent rounding behavior, regardless of the decimal part’s sign.

Cons:

  • They may not be suitable for all rounding requirements, as they always round up or down, respectively.
  • They may not be efficient for large-scale calculations involving huge datasets.

Conclusion

The floor() function in Python is a valuable tool for rounding down numbers to the nearest integer. It is particularly useful in various mathematical calculations, especially when dealing with real numbers. By understanding the syntax and functionality of the floor() function, you can efficiently work with rounded integer values in your data science projects and other applications.

If you are curious to learn about tableau, data science, check out IIIT-B & upGrad’s Executive PG Programme in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Explore Free Courses

Suggested Blogs

Priority Queue in Data Structure: Characteristics, Types & Implementation
57467
Introduction The priority queue in the data structure is an extension of the “normal” queue. It is an abstract data type that contains a
Read More

by Rohit Sharma

15 Jul 2024

An Overview of Association Rule Mining & its Applications
142458
Association Rule Mining in data mining, as the name suggests, involves discovering relationships between seemingly independent relational databases or
Read More

by Abhinav Rai

13 Jul 2024

Data Mining Techniques & Tools: Types of Data, Methods, Applications [With Examples]
101684
Why data mining techniques are important like never before? Businesses these days are collecting data at a very striking rate. The sources of this eno
Read More

by Rohit Sharma

12 Jul 2024

17 Must Read Pandas Interview Questions & Answers [For Freshers & Experienced]
58114
Pandas is a BSD-licensed and open-source Python library offering high-performance, easy-to-use data structures, and data analysis tools. The full form
Read More

by Rohit Sharma

11 Jul 2024

Top 7 Data Types of Python | Python Data Types
99373
Data types are an essential concept in the python programming language. In Python, every value has its own python data type. The classification of dat
Read More

by Rohit Sharma

11 Jul 2024

What is Decision Tree in Data Mining? Types, Real World Examples & Applications
16859
Introduction to Data Mining In its raw form, data requires efficient processing to transform into valuable information. Predicting outcomes hinges on
Read More

by Rohit Sharma

04 Jul 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
82805
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

04 Jul 2024

Most Common Binary Tree Interview Questions & Answers [For Freshers & Experienced]
10471
Introduction Data structures are one of the most fundamental concepts in object-oriented programming. To explain it simply, a data structure is a par
Read More

by Rohit Sharma

03 Jul 2024

Data Science Vs Data Analytics: Difference Between Data Science and Data Analytics
70271
Summary: In this article, you will learn, Difference between Data Science and Data Analytics Job roles Skills Career perspectives Which one is right
Read More

by Rohit Sharma

02 Jul 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon