Is There a Ceiling Equivalent of // Operator in Python?

How To Modify A Text File In Python

If you’re a beginner in programming and have just started using Python, I’m pretty sure you must have encountered the ‘//’ operator or floor division operator. The // operator is used to divide an integer and then round off its value to its floor value.

Have you thought that there’s no equivalent of // operator for ceiling division?

In this article, we’re going to study different ways to perform ceiling division and also create a custom function for ceiling division. The article is beginner-friendly so no need to worry and let’s jump right into it.

Operators in Python

Operators are symbols that denote a mathematical operation. There are 4 basic operations in math. Addition, Subtraction, Division, and Multiplication. But in programming, you generally deal with 6 types of operations. The Division operations have 3 different operations – Modulus, integer division, and floating point division.

The integer division when performed sometimes may not return integer values. In those cases, we need to round it off to the most suitable integer value. Generally, in math, we round it off the closest integer value but in programming, we generally round it off to its floor value. We use the ‘//’ operator for this purpose.

Related: Learn more about operators in Python.

// operator

// operator is the floor division operator. It’s very useful cause most of the time as programmers, we’re working on integers instead of decimal values. And instead of rounding off to the closest value it rounds off to the floor value. It is similar to / operator in C or C++.

Ceiling Division Operation in Mathematics

The ceiling division is dividing two numbers and then rounding it off to the ceiling value of the result. There’s no operator for ceiling division in Python. The ceiling operation has applications in computer resource allocation, financial and pricing calculations, user interface and graphic design, etc. Though some various libraries and modules provide functions for ceiling division. Also, we can create our own ceiling function.

The default method for ceiling division in Python

Usually, we perform ceiling division using the floor division operator and double negation. The code below is an example of how we can achieve ceiling division using the floor division operator and double negation.

print(-(-10//3)) #ceiling operation on 10 divided by 3

Output: 4

Basically, instead of the ceiling dividing 10 by 3, we floor divided -10 by 3 and then multiplied it by a minus. Floor division of -10 by 3 will be -4 and -(-4) will be 4. This is our desired output for the ceiling division. This method is simple, has a short syntax, and is fast.

Python’s math module

Python’s math module is part of the standard Python library which provides a variety of mathematical functions. It is really useful for mathematical projects which require a lot of math functions. It has both floor and ceiling functions to round off a number to its floor value or its ceiling value.

Related: Learn more about the math module.

Using floor() and ceil() functions

To use floor() or ceil() methods, we simply divide two numbers using the ‘/’ operator and then round it off to its floor or ceiling value using the floor() or ceil() method.

import math
print(math.floor(10/3)) #floor operation on 10 divided by 3
print(math.ceil(10/3)) #ceiling operation on 10 divided by 3
Math Floor And Ceil
Math Floor And Ceil

Python’s Alternative Methods for Ceiling Operations

Introduction to the NumPy Library

The numpy library is one of the most popular libraries in Python. It provides a lot of computational abilities. Numpy has functions to manipulate multi-dimensional lists or arrays. It also has functions for statistical measures such as mean, standard deviation, etc. which are really useful in data science and machine learning. Though our concern now is just the ceil() function provided by numpy.

Related: Learn more about the NumPy library.

How to use numpy.ceil() function?

The ceil() function of numpy can also be used to perform ceiling division. Below is an example of the same.

import numpy as np
print(np.ceil(10.5))
print(np.ceil(10/3))
Numpy Ceil
Numpy Ceil

Python’s Alternative Packages for Ceiling Operations

Introduction to Python’s sympy package

SymPy is a lightweight library for advanced mathematical topics such as calculus. It has applications in quantum field theory, chemistry, LaTex, and many more. Though instead of going crazy over such complex topics we will be checking out just the sympy.ceiling() function of it.

Related: Learn more about the SymPy package.

‘How to use sympy.ceiling() function?

import sympy as sp
x = sp.Symbol('x')
expr = sp.ceiling(x)
print(expr.subs(x, 10/3))
Sympy Ceil Operation
Sympy Ceil Operation

In the above code, first, we imported the symPy library. Then we created a symbolic variable using the sp.Symbol() function. Then we created an expression using this symbolic variable. This expression returns the ceiling value of the value passed in x.

Comparison of Different Ceiling Operations Methods

Now that you know all these methods for doing a simple task like ceiling division, why do we need all these? As programmers, we always need to have backup options as we never know when an error will occur. In case of an error can simply choose different options if you have a bunch. Though all these methods have their own advantages and use cases.

Advantages and use cases of math.ceil()

  • It’s a part of the standard Python library so no installations are required.
  • It is simple and has fewer parameters so not confusing.
  • Works well for simple mathematical use cases.
  • Returns an integer value.

Advantages and use cases of numpy.ceil()

  • It can work on iterables like lists.
  • Has more flexibility.
  • Really efficient when you’re working on large vectors of data.
  • Returns a float value instead of an integer.

Advantages and use cases of sympy.ceil()

  • It works well on expressions.
  • Can be used to work on advanced mathematics such as calculus, eigenvalues, etc.
  • Returns an integer value.

Conclusion

These were some methods by which you can achieve ceiling division. It’s not necessary to learn every method but you must have a basic idea of different ways as you never know which one you might need. The operators are just the basics and you have a big journey ahead as the programmers. You must be determined as you will bump into problems here and there. In such cases, don’t panic, just ask for help.

References

Stack Overflow for the same question.

Official Python Documentation.