• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Ceiling Division in Python

Ceiling Division in Python

May 6, 2022 Leave a Comment

To perform ceiling division in Python, you can define your own function and utilize the floor division operator //.

def ceiling_division(x,y):
    return -1 * (-x // y)

print(ceiling_division(11,3))
print(ceiling_division(40,9))
print(ceiling_division(1,4))

#Output:
4
5
1

You can also utilize the math module ceil() function to perform ceiling division.

import math 

print(math.ceil(11/3))
print(math.ceil(40/9))
print(math.ceil(1/4))

#Output:
4
5
1

When working with number in Python, the ability to easily perform different calculations is very useful.

One such calculation is ceiling division, or the ceiling of the number you get after dividing two numbers.

In the Python language, we have the // operator for floor division, but there is not a built in function which performs ceiling division.

However, we can create our own function to do ceiling division utilizing the mathematical fact that negative one times the floor of a negative number is equal to the ceiling of a positive number.

Therefore, if we do floor division with two numbers, multiplying the first number by -1 and then taking the resulting number times -1 again, we can get result we want.

Below is a function which will do ceiling division for us in Python.

def ceiling_division(x,y):
    return -1 * (-x // y)

print(ceiling_division(11,3))
print(ceiling_division(40,9))
print(ceiling_division(1,4))

#Output:
4
5
1

Using math.ceil() to Perform Ceiling Division in Python

Another way that you can do ceiling division in Python is to perform regular division and take the ceiling of the number with the Python math.ceil() function.

The math module ceil() function returns the ceiling of a number.

Ceiling division is simply the ceiling of the result after dividing one number by another.

Therefore, you can do ceiling division by dividing and passing the result to ceil().

Below shows that using the math module ceil() function gives us the same result as our custom function from above.

import math 

print(math.ceil(11/3))
print(math.ceil(40/9))
print(math.ceil(1/4))

#Output:
4
5
1

Hopefully this article has been useful for you to learn how to do ceiling division in your Python code.

Other Articles You'll Also Like:

  • 1.  Get Day of Year from Date in pandas DataFrame
  • 2.  How to Combine Dictionaries in Python
  • 3.  Python Negative Infinity – How to Use Negative Infinity in Python
  • 4.  Using Lambda Expression with min() in Python
  • 5.  Using Python to Find Maximum Value in List
  • 6.  Reverse Words in a String Python
  • 7.  Get Year from Date in pandas DataFrame
  • 8.  Sign Function in Python – Get Sign of Number
  • 9.  How to Check If File is Empty with Python
  • 10.  Print Approximately Equal Symbol in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy