Floor Division in Python

(Click the below link for a Microsoft Word version of this blog-post)

floor_division_in_python

(Click the below link for a pdf version of this blog-post)

floor_division_in_python

floor_division_operator

Figure 1:  The Floor-Division operator.  In Python, the Floor-Division operator consists of two forward slashes.  The Floor-Division operator is an example of a binary operator, as it takes two operands: the dividend and the divisor.

With floor division, one number, the dividend, is divided by another number, the divisor, and the result, or quotient – whatever it may happen to be – will be a rounded-down integer value.

Let us consider the Python Equation:

>>>8/5

1.6

>>>

The number, 8, the dividend, is divided by the divisor, 5, and a floating-point number, 1.6, is then returned as a quotient.

eight_divided_by_five_shell_ordinary_division

Figure 2:  When we divide 8 by 5 using the division operator, / , then a floating-point number, 1.6, is returned as a quotient.

division_operator

Figure 3:  This is our Division operator.  When we employ this binary operator, a floating-point number will be returned.

Whenever we employ a Division operator in Python, then a floating point number will always be returned as a quotient, even if the quotient has no significant fractional component.

eight_divided_by_four_shell_ordinary_division

Figure 4:  Whenever we divide the dividend, 8, by the divisor, 4, then the quotient, 2.0, is still returned as a floating-point number despite its not having any significant fractional component.

Let us, again, consider the Python equation:

>>>8/5

1.6

>>>

, but let us do things a little differently:

>>>8//5

1

>>>

In the above example, we have now employed the floor-division operator.  The floor-division operator will always return an integer value, if the 2 operands that it takes be integers.

eight_divided_by_five_shell_floor_division

Figure 5:  When we divide the dividend, 8, by the divisor, 5, we get the quotient, 1, rendered as an integer.

Let us consider 8 divided by 2 in ordinary arithmetic for a moment:

8 ÷ 5 = 1.6

In the above example, we divide an integer by an integer and we obtain a real number as a result, or quotient.

If we wanted a less precise answer, then it would be customary to see:

8 ÷ 5 2

In normal arithmetic, it would be customary to round:

1.6

up to:

2

.

However, in floor division, floating point numbers such as:

>>>1.6

1.6

>>>

are always rounded down to the value of its integral component.

So, in Python, the floor value of:

>>>1.6

1.6

>>>

would be:

>>>2

2

>>>

The floor-division operator will always return an integer as a quotient, unless floating-point numbers be employed as operands.

seven_point_nine_divided_by_three_point_two_shell_ordinary_division

Figure 6:  When we divide 7.9 by 3.2 in conventional division, we obtain the floating-point quotient, 2.46875

seven_point_nine_divided_by_three_point_two_shell_floor_division

Figure 7:  When we divide 7.9 by 3.2 in floor division, we still obtain a floating-point quotient, 2.0, but it does not have a significant fractional component.

Programming a Floor-Division Calculator in Python:

In the following section, we shall program a simple floor-division calculator in Python:

floor_division_calculator

Figure 8:  A simple floor-division calculator programmed in Python.  This program requests that the user input two numbers.  The program then takes these inputs; divides the dividend by the divisor; and then returns a rounded-down quotient.

output_floor_division_calculator_final

Figure 9:  What the previous program, depicted in Figure 8, outputs when run.

 

 

Leave a comment