Python

Python Math Floor

In Maths, the floor method takes input a real number a and shows the resulting output that is greater or less than or equal to “a”. It is denoted by [a].

For example : [1.5] = 1 , [-3.6] = -3

Basically, the floor function returns the number in an integer value and changes the floating value into an integer.

Python language contains many functions that are helpful for programmers to solve their tasks by using these functions. In the floor() function, we must import the math library.

Daily Life Example:

Real-life examples explain the concept of floor() function easily. Let’s assume we are at a cafe. We like to make a converter that rounds down the quantity of coffee to the nearest integer. This helps the user to estimate how much coffee we still have.

Example 01:

In Example 1, we import the “math” library first because we can’t use the floor function. Floor() function returns integer value after execution of code. So in this example, we simply use variables and pass math.floor() and print the variable to check the output.

import math    
number = 50.67    
print ("Floor  value is : ", math.floor(number))

We can see the result of math.floor() function is:

Example 02:

A list is a collection of several ordered or unordered items. It can be used for multiple purposes, sorting, deleting, and adding items to a list.

In Example 2, we use the math floor() method on List. We use the floor function to iterate the list item given in the example.

import math
lst = [13.9, -33.7, 26.89, 15.56, -65.90]
for val in lst:
print(math.floor(val))

Floor function with List output is :

Example 03:

Map() is a built-in function used to process and change all the items in a list without using for loop. This method of transforming value is called mapping.

Python use map function with floor function to overcome the use of for loop. So, In Example 3, we check how both functions work.

import math
value = [13.9, -33.7, 26.89, 15.56, -65.90]
print(value)
print()
result = map(lambda number: math.floor(number), value)
print('Result = ', list(result))

Floor() function with map() and lambda functions output is:

Example 04:

In Example 4, we take input from the user and check if the entered number result is in float. Python’s built-in exceptional handling displays a message if the user enters an invalid value. For Exceptional handling, we use a try statement.

import math
number = input('Enter number to check is integer or float): ')
try:
    value = float(number)
except Exception:
    print('Must enter a valid number.')
    number = 0
print('Result is ', math.floor(value))

The output of the given an example is:

Example 05:

In Example 5, we use the class having the name Floor. Class contains method name getDesiredValue(). In this function, we initialized five variables that are equal to the math.floor() function and then printed this variable inside the string. Outside the class initialize the variable name obj which is equal to Floor() then pass the getDesiredValue() to obj . Because without doing the last two steps, we can’t access the class and get results.

import math
class Floor:   
    def getDesiredValue(self):
        n1 = math.floor(1.0);
        n2 = math.floor(2.8);
        n3 = math.floor(3.3);
        n4 = math.floor(3.4);
        n5 = math.floor(7.7);
        print("Floor value Of Variable n1 " + str(n1));
        print("Floor value Of Variable n2 " + str(n2));
        print("Floor value Of Variable n3 " + str(n3));
        print("Floor value Of Variable n4 " + str(n4));
        print("Floor value Of Variable n5 " + str(n5));    
obj = Floor()
obj.getDesiredValue()

We get the output of the given an example as:

Example 06:

A range of different constants is shown in the “math” library of Python. Math.pi is used to find the value of pi (3.14). “math.e” is used to find the value of e (2.71). “math.inf” means the infinite value or limitless, so the result of this function is OverflowError: cannot convert float infinity to an integer.

import math
valuePi = math.pi
valueE = math.e
valueinfinite = math.inf
print(math.floor(valuePi))
print(math.floor(valueE))
print(math.floor(valueinfinite))

The Output of the given an example is :

Example 07:

In Example 7, first, we import the “NumPy” library to get our output. We initialize the 1D array, the values of the array are in float data type. After that, check the resulting print of the Array. The program’s goal is to discover the sum of array values, and the resultant total is of the floating data type value, as can be seen. Then, we have displayed the derived sum’s floor number.

import numpy as np
Array = np.array([14.7,45.7,34.6,1.78,32.65])
# Print the Array
print("The array is: ", Array)
val = np.sum(Array)
print("Sum of array elements is: ", val)
# Print floor value of  sum
print("Floor value of the sum is : ", np.floor(val))

As we can see, the example shows the output is:

Conclusion:

The floor() method rounds a value to the nearest whole number in Python. This function only accepts integer and float values as arguments; otherwise, an error will be returned. We learned how to use the floor() function of a math module by importing it. We also used math.floor() method of lists, tuples, and the map() function in Python.

At the same time, we spoke about how to use the floor() function in Python on integers, decimals, and negative numbers. In addition, we discussed how the floor() method differs from the int() method in Python.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content