. Advertisement .
. Advertisement .
Many developers want to know how to carry out ceiling division in Python because the official operators of this programming language don’t cover it. Here is the way to get it done. It may help you later with, for example, calculating the Euclidean distance.
How To Perform Ceiling Division In Python
Using The // Operator
There are several ways to carry out division operations in Python and any developer should have a deep understanding of each of them. They are essential operators that you could make use of beyond their original intentions.
For example, while Python doesn’t have a dedicated operator for ceiling division, it does come with one for floor division – the // operator. Its syntax is quite straightforward:
>>> 10 / 3 3.33333333333333 >>> 10 // 3 3
This operator returns the greatest integer that is smaller or equal to a division result. And this can be used in a creative way to perform ceiling division in Python: -(-dividend // divisor). This works because when we keep the same divisor but change the sign of the dividend, the unsigned values of their flooring division and ceiling division flip.
This snippet demonstrates this new use of the // operator:
dividend = [1, 10, -10, 30, 100.7] divisor = [2, 3, 3, 15, 1] for x in dividend: i = dividend.index(x) print(int(-(-x // divisor[i])))
1 4 -3 2 101
As you can manually check, the statement inside the print() function carries out the ceiling division with exact results. It works for both positive and negative floating-point numbers.
While the modified use of the // operator above is a nice trick, many may want a more explicit solution, which is easier to remember and less prone to typos.
The ceil() function from the math module is a perfect alternative. It doesn’t require you to understand some complicated math transformations.
The function works in a plain manner: it rounds up the argument to the nearest integer, and if you give a division, it practically becomes a ceiling division.
>>> import math >>> math.ceil(10 /3) 4 >>> math.ceil(-10/3) -3
You can rewrite the previous program using math.ceil() instead of the // operator.
import math dividend = [1, 10, -10, 30, 100.7] divisor = [2, 3, 3, 15, 1] for x in dividend: i = dividend.index(x) print(math.ceil(x / divisor[i]))
1 4 -3 2 101
Both the approaches produce the same results for every test. And the best part is that it is much easier to implement this solution.
Remember that we are discussing the math.ceil() function in Python 3, which returns an integer value. This is not the case with Python 2.
>>> import sys >>> sys.version '2.7.18 (default, Oct 10 2021, 22:29:32) \n[GCC 11.1.0]' >>> import math >>> math.ceil(10 / 3) 3.0 >>> type(math.ceil(10 / 3)) <type 'float'> >>> math.ceil(-10 / 3) -4.0 >>> type(math.ceil(-10 / 3)) <type 'float'>
The math.ceil() function in Python 2 produces a float value If you want to do ceiling division in Python 2 with it, you must convert the results to int.
import math dividend = [1, 10, -10, 30, 100.7] divisor = [2, 3, 3, 15, 1] for x in dividend: i = dividend.index(x) print(int(math.ceil(x / divisor[i])))
0 3 -4 2 101
Without the int() function, the script will produce these floating-point numbers:
0.0 3.0 -4.0 2.0 101.0
The math.ceil() function is the easiest way to perform ceiling division in Python, while the // operator is worth considering too. Keep in mind the difference between math.ceil() in Python 2 and 3, which changes their entire use.
Leave a comment