• 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
  • VBA
  • 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.  Sort by Two Keys in Python
  • 2.  Python isprime – Function for Determining if Number is Prime
  • 3.  pandas head – Return First n Rows from DataFrame
  • 4.  Symmetric Difference of Two Sets in Python
  • 5.  Get Last Character in String in Python
  • 6.  Check if String Contains Only Certain Characters in Python
  • 7.  Using Python to Remove Commas from String
  • 8.  How to Check if Number is Power of 2 in Python
  • 9.  Python Replace Space With Dash Using String replace() Function
  • 10.  Change Python Turtle Shape Fill Color with fillcolor() Function

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy