Math Floor Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

In Python, the math module has several functions that allow us to perform various mathematical calculations. One of those functions is the math floor Python function. The floor function returns the closest integer that is less than or equal to the specified number. In simple words, it rounds the number down to the nearest integer.

Read more about the math module here.

Python Math Floor Function

The math floor Python function returns the closest integer value that is less than or equal to the specified value. In simple words, it rounds the number down to the closest integer. To use the floor function, we have to import the floor function from the math module.

Tip: For rounding up the number to the nearest integer, we can use math.ceil() function.

Syntax of Python Math Floor Function

We have many ways to use the math floor Python. First, we can simply import the math module using import math, and then we can use the math floor Python function by writing math.floor(). Second, we can import the floor function by writing from math import floor and then use the floor function by writing floor().

In this code, we have imported the math module, and then just written a number inside the math.floor() function.

In this code, we have imported the floor function from the math module and then used it by passing a number inside it.

In the above syntax, n specifies the number that is to be rounded down.

Parameters of Python Math Floor Function

The math floor Python function only takes one number, which can be a positive/negative floor or integer number, and does not take any infinite value or any string.

Return Value of Python Math Floor Function

The math floor Python function returns an integer not greater than n when we pass any float value and returns the same integer when we pass any integer value, but it throws an error when we pass an infinite value or any string.

Exceptions of Python Math Floor Function

The math floor Python function has also some exceptions, that why we have to use it carefully.

  • The program will throw a TypeError if you try to pass any string instead of a number to the floor function.

    Code:

    Output:

  • If you try to convert an infinite value to a floor value, we will get an OverflowError.

    Code:

    Output:

Working on Math Floor Function in Python

The math floor Python function works very similarly to the round function in Python.

As you can see above: For 13.45678: It is closer to 13, so return 13. For 134.959595: It is closer to 135, so return 135. For -123.3245: It is closer to -123, so return -123. For -133.9245: It is closer to -134, so return -134.

As you can see above: For 13.45678: It is closer to 13, so return 13. For 134.959595: It is closer to 135, but 134 is the largest integer <= 134.959595, so return 134. For -123.3245: It is closer to -123, but -124 is the largest integer <= -123.3245, so return -124. For -133.9245: It is closer to -134, so return -134. For 55.55: It is equidistant from both 55 and 56, but 55 is the largest integer <= 55.55, so return 55.

Uses of Math Floor Function

The floor() function in Python calculates the nearest integer less than the argument passed. Or, in simple words, it rounds down the number.

Python Math Floor Function Examples

Example 1

Code:

Output

In the above output, as you can see, 31 is the largest integer <= 31.78.

Example 2

Code:

Output:

In the above code, for every number in a list, it will print the largest integer less than or equal to that number.

Example 3

Code:

Output:

In the above output, if we pass any integer or float value, then we will get the integer in the output. However, if we pass any string as input, it will throw an error.

Example 4

Code:

Output:

In the above code, the PI(π\pi) value is 3.14, so it will become 3. The exponential constant(e) value is 2.17, so it will become 2. The INF value is infinite, so it is throwing an error because an infinite value can't be converted to an integer.

The Python math module has various functions that are used to solve mathematical operations. Some of these functions are given below, so take a look at them:

Conclusion

Let's summarise our topic by mentioning some of the important points.

  • In Python, the math module has several functions that allow us to perform various mathematical calculations.
  • The math floor Python function returns the closest integer that is less than or equal to the specified value.
  • To use the math floor Python function, we have to import the floor function from the math module.
  • The floor function only takes one number, which can be a positive/negative floor or integer number.
  • The floor function returns a number not greater than n.