Home » Python Tutorial » Python Floor Division and Math.floor() Function with Examples

Python Floor Division and Math.floor() Function with Examples

The Floor of the real number is used in python to find out the greatest number which is less than or equivalent to the given number. In python floor division or floor functions are used to return the nearest integer value which must not be greater than the given number. Other than this, to find out the closest integer value which can be equal to or greater than the given number. We do use the ceil() method in python.

Example

Python Floor of a real number 

floor(5.6) = 5

floor(7.2) = 7

There are generally three methods with which we can perform division in Python

1. a/b – It will normally return the result as in quotient form.

Example

a=4
b=8
c=b/a
print(c)

Output

2.0

2. a%b – It is known as division using the modulo operator. It returns the remainder value after division.

Example

a=4
b=8
c=b%a
print(c)

Output

0

3. a//b(floor division) – It is known as Python floor division. It would return the closest value which has to be equivalent or less than  the value obtained from natural division. // is known as a python floor division operator. And it is also known as div.

Floor division

Floor division in python is used to perform division conventionally but it returns the closest or greatest integer value which can be equivalent or less than  the conventionally obtained division result. 

  • It roundoff the resultant value to the closest integer
  • The closest integer must be less than or equivalent to the normal division result.
  • // operator is used to perform floor division.
  • N = D * ( N // D) + (N % D)condition is always satisfied.

Example 1

a=14
b=8
c=b//a
print(c)

Output

0

Example 2

a=20
b=-6
c=a//b
print(c)

Output

-4

math.floor()

Apart from the floor operator, We can also use the python floor function to find out the floor value of a real number. It will just give the same results as that of python floor division. This floor function can be used by importing it from the Math library using the import keyword.

Floor function syntax

math.floor()

Example

import math
a=math.floor(15.9)
print(a)
 
 
a=17
b=8
c= math.floor(a//b)
print(c)

Output

15
2

We can also find the closest value of pi by using floor function.

d= math.floor(math.pi)
print(d)

Output

3

We can also perform some operations using floor on sequence containers or iterables such as lists, tuples etc.

e = [1.22,3.45,7.89,3.55]
g=[]
for i in  e:
    j= math.floor(i)
    g.append(j)
   
print(g)

Output

[1, 3 , 7 ,3]

Note – We can also use floor functions by using them within a function. We can also use lambda functions for the same purpose.

FAQ’s

Is there any difference between floor() and int()  function in Python?

There is no such major difference between the floor() and int() function. Both floor and int returns the same positive integer values for positive numbers. Int () function converts specified numbers into proper integers. It return 0 if no argument is given to the function. floor function just returns the greater value which is equivalent or less than the given number passed in the argument. But for the negative values, floor and int might return negative values unlike in the case of Positive values.

What is floor division in python?

Floor division in python is used to perform division in which the resultant value is closest or greatest but not equivalent or less than to the value obtained from normal division. It rounds off the value according to the situation or condition mentioned in the above line. It uses // python floor operator  perform division in python. Floor division in python is different from other normal and modulo the division.

What happens when floor division in python when the divisor is larger than dividend?

Terminal returns 0 in the situation when the divisor is larger than dividend in  python floor division. It does not return any roundoff value of any number other than 0. This thing also warns the user whenever he has used wrong digits in place of divisor and dividend.

Did you know?
1. Python Strftime
2. Encapsulation in Python
3. Operator Overloading in Python
4. iloc in Python
5. Python Pop
6. Python Enum
7. Python Pass
8. Python deque
9. Python Not Equal Operator
10. Python Floor

Pin It on Pinterest