Division plays a significant role in calculating various operations. We are all familiar about the normal division using the / operator. The following article will talk about the Floor Division, its operator, and its functions using Python.
What is the // operator in Python?
In Python, the double-backslash (//) is a mathematical operator called the floor division operator. Floor division implies splitting and rounding down a number to its nearest whole integer-point value.
It's a regular division operation but returns the nearest possible integer. The result may be equal to the actual division output or less than it.
The physical understanding of the floor division is about sharing portions evenly. For example, if there are nine apples and two children, how many full apples does each child get? Well the answer is 9 // 2 = 4.
Regular Division (/)
In Python, the single backslash (/) is the division operator that performs division between two numbers and returns the result mostly in float.
a=3 b=2 c=a/b print(c) a=4 b=2 c=a/b print(c) a=100 b=56 c=a/b print(c) a=4.787 b=2.565 c=a//b print(c)
Floor Division (//)
In Python, the double backslash operator is a unique operator that performs the floor division. It divides two numbers and rounds the result down to its nearest integer.
Let us take an example,
[ 5/2 ]
In mathematical terms, the floor division gets signified by the symbol [ ]
Performing the division 5 by 2, we get:
5 ÷ 2 = 2.5
Now, if we pass the result (2.5) to the math.floor() function we'll get the result 2.
math.floor(2.5) = 2
To get the round down value of 5÷2, we can also use the floor division operator (//)
5//2 = 2
Employing the floor division operator in a Python code:
a = 3 b = 2 c=a//b print(c) a=4 b=2 c=a//b print(c) a=100 b=56 c=a//b print(c) a=4.787 b=2.565 c=a//b print(c)
The math.floor() function in Python
There is a built-in module in Python called the math module, which has several mathematical utilities for calculations and other mathematical tasks. The math module contains a set of methods that aids in performing mathematical tasks.
The math.floor() function is one of those methods. It takes the input of a numeric value and returns the floor value of that input by rounding it down to its nearest integer-point value.
Below are some examples of the math.floor() function:
import math #Import the math library # Round the numbers down to their nearest integer-point value print(math.floor (0.9)) print(math.floor (3.6)) print(math.floor (3.4)) print(math.floor (-5.8)) print(math.floor (29.7)) print(math.floor (12.0))
Difference between Regular Division and Floor Division
The difference between Regular Division and Floor Division is that the regular division returns the exact result in floating-point value. While, the floor division returns the nearest whole integer point value.
To sum up:
The double backslash operator in Python performs floor division between two operands. It rounds down the float value to its nearest whole integer-point value. The single backslash(/) operator or regular division operator performs normal division and returns the result mainly in a floating point.
The double backslash(//) operator or floor division operator also performs normal division but returns the nearest integer value to the result. Python has a built-in function in the math module called the math.floor() that returns the floor value of any floating number.