Skip to content

Ceiling division Python

  • by

You need to import math, and use math.ceil to Ceiling division in Python. There is no operator that divides with ceil.

Here’s how you can do it:

  1. Import the math module.
  2. Use the math.ceil function to round up the result of the division.
math.ceil(x)

Ceiling division Python

A simple example code that rounds a number UP to the nearest integer, if necessary, and returns the result.

import math

# Round a number upward to its nearest integer
print(math.ceil(2.4))
print(math.ceil(7.3))
print(math.ceil(-1.3))
print(math.ceil(10.0))

Output:

Ceiling division Python

Alternatively, if you prefer not to use the math module, you can achieve ceiling division using integer arithmetic:

def ceiling_division(numerator, denominator):
    return -(-numerator // denominator)

# Example usage
numerator = 7
denominator = 3
result = ceiling_division(numerator, denominator)
print(result)  # Output: 3

What does it mean to round up?

When you round up a number you round the number to the closest integer that is greater than the number. Because of this, when you round 7.3 up, it returns 8.

Do comment if you have any doubts or suggestions on this Python deviation topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading