Just use numpy.ceil:

out = np.ceil(a/2)

Output: array([1., 1., 2., 2.])

Or eventually:

out = np.ceil(a/2).astype(a.dtype)

Output: array([1, 1, 2, 2])

Answer from mozway on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ceil.html
numpy.ceil — NumPy v2.4 Manual
>>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.ceil(a) array([-1., -1., -0., 1., 2., 2., 2.])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.floor_divide.html
numpy.floor_divide — NumPy v2.4 Manual
divide · Standard division. floor · Round a number to the nearest integer toward minus infinity. ceil · Round a number to the nearest integer toward infinity. Examples · Try it in your browser! >>> import numpy as np >>> np.floor_divide(7,3) 2 >>> np.floor_divide([1., 2., 3., 4.], 2.5) ...
🌐
Programiz
programiz.com › python-programming › numpy › methods › ceil
NumPy ceil() (With Examples)
The ceil() function returns a new array with the rounded-up values. import numpy as np # create a 2D array array1 = np.array([[1.2, 2.7, 3.5], [4.8, 5.1, 6.3], [7.2, 8.5, 9.9]])
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ceil.html
numpy.ceil — NumPy v2.2 Manual
>>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.ceil(a) array([-1., -1., -0., 1., 2., 2., 2.])
Top answer
1 of 9
490

No, but you can use upside-down floor division:¹

def ceildiv(a, b):
    return -(a // -b)

This works because Python's division operator does floor division (unlike in C, where integer division truncates the fractional part).

Here's a demonstration:

>>> from __future__ import division     # for Python 2.x compatibility
>>> import math
>>> def ceildiv(a, b):
...     return -(a // -b)
...
>>> b = 3
>>> for a in range(-7, 8):
...     q1 = math.ceil(a / b)   # a/b is float division
...     q2 = ceildiv(a, b)
...     print("%2d/%d %2d %2d" % (a, b, q1, q2))
...
-7/3 -2 -2
-6/3 -2 -2
-5/3 -1 -1
-4/3 -1 -1
-3/3 -1 -1
-2/3  0  0
-1/3  0  0
 0/3  0  0
 1/3  1  1
 2/3  1  1
 3/3  1  1
 4/3  2  2
 5/3  2  2
 6/3  2  2
 7/3  3  3

Why this instead of math.ceil?

math.ceil(a / b) can quietly produce incorrect results, because it introduces floating-point error. For example:

>>> from __future__ import division     # Python 2.x compat
>>> import math
>>> def ceildiv(a, b):
...     return -(a // -b)
...
>>> x = 2**64
>>> y = 2**48
>>> ceildiv(x, y)
65536
>>> ceildiv(x + 1, y)
65537                       # Correct
>>> math.ceil(x / y)
65536
>>> math.ceil((x + 1) / y)
65536                       # Incorrect!

In general, it's considered good practice to avoid floating-point arithmetic altogether unless you specifically need it. Floating-point math has several tricky edge cases, which tends to introduce bugs if you're not paying close attention. It can also be computationally expensive on small/low-power devices that do not have a hardware FPU.


¹In a previous version of this answer, ceildiv was implemented as return -(-a // b) but it was changed to return -(a // -b) after commenters reported that the latter performs slightly better in benchmarks. That makes sense, because the dividend (a) is typically larger than the divisor (b). Since Python uses arbitrary-precision arithmetic to perform these calculations, computing the unary negation -a would almost always involve equal-or-more work than computing -b.

2 of 9
83

Solution 1: Convert floor to ceiling with negation

def ceiling_division(n, d):
    return -(n // -d)

Reminiscent of the Penn & Teller levitation trick, this "turns the world upside down (with negation), uses plain floor division (where the ceiling and floor have been swapped), and then turns the world right-side up (with negation again)"

Solution 2: Let divmod() do the work

def ceiling_division(n, d):
    q, r = divmod(n, d)
    return q + bool(r)

The divmod() function gives (a // b, a % b) for integers (this may be less reliable with floats due to round-off error). The step with bool(r) adds one to the quotient whenever there is a non-zero remainder.

Solution 3: Adjust the numerator before the division

def ceiling_division(n, d):
    return (n + d - 1) // d

Translate the numerator upwards so that floor division rounds down to the intended ceiling. Note, this only works for integers.

Solution 4: Convert to floats to use math.ceil()

def ceiling_division(n, d):
    return math.ceil(n / d)

The math.ceil() code is easy to understand, but it converts from ints to floats and back. This isn't very fast and it may have rounding issues. Also, it relies on Python 3 semantics where "true division" produces a float and where the ceil() function returns an integer.

🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ceil.html
numpy.ceil — NumPy v2.1 Manual
>>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.ceil(a) array([-1., -1., -0., 1., 2., 2., 2.])
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ceil.html
numpy.ceil — NumPy v2.5.dev0 Manual
>>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.ceil(a) array([-1., -1., -0., 1., 2., 2., 2.])
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.floor_divide.html
numpy.floor_divide — NumPy v2.2 Manual
divide · Standard division. floor · Round a number to the nearest integer toward minus infinity. ceil · Round a number to the nearest integer toward infinity.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy-ceil-python
numpy.ceil() in Python - GeeksforGeeks
March 8, 2024 - If number is already integer, same number is returned. Syntax: math.ceil(x) Parameter: x: This is ... numpy.who() function print the NumPy arrays in the given dictionary. Syntax : numpy.who(vardict = None) Parameters : vardict : [dict, optional] A dictionary possibly containing ndarrays.
🌐
Enterprise DNA
blog.enterprisedna.co › python-ceiling-division
Python Ceiling Division: Quick User Guide – Master Data Skills + AI
This Python code creates a NumPy array a with three float numbers, applies the ceil() function from NumPy to each element of the array which rounds each element up to the nearest integer, stores this in array b, and then prints b.
🌐
NumPy
numpy.org › doc › 1.15 › reference › generated › numpy.ceil.html
numpy.ceil — NumPy v1.15 Manual
>>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.ceil(a) array([-1., -1., -0., 1., 2., 2., 2.])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.divide.html
numpy.divide — NumPy v2.4 Manual
numpy.divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'divide'>#
🌐
Codecademy
codecademy.com › docs › python:numpy › math methods › .ceil()
Python:NumPy | Math Methods | .ceil() | Codecademy
June 13, 2025 - In NumPy, the .ceil() function rounds each element in an array to the nearest integer that is greater than or equal to the element.
🌐
Delft Stack
delftstack.com › home › howto › python › ceiling division python
Ceiling Division in Python | Delft Stack
March 11, 2025 - In this example, we import the NumPy library and define the ceiling_division function. The function divides a by b, applies np.ceil() to round up the result, and then converts it to an integer using .astype(int).
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.ceil.html
numpy.ceil — NumPy v2.3 Manual
>>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.ceil(a) array([-1., -1., -0., 1., 2., 2., 2.])
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.0 › reference › generated › numpy.ceil.html
numpy.ceil — NumPy v1.10 Manual
>>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.ceil(a) array([-1., -1., -0., 1., 2., 2., 2.])
🌐
AskPython
askpython.com › home › is there a ceiling equivalent of // operator in python?
Is There a Ceiling Equivalent of // Operator in Python? - AskPython
May 25, 2023 - 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 ...
🌐
Medium
medium.com › @heyamit10 › understanding-numpy-round-up-ceil-vs-round-vs-floor-f155922395b8
Understanding NumPy Round Up (ceil vs round vs floor) | by Hey Amit | Medium
April 12, 2025 - A: Nothing changes. Since integers are already whole numbers, np.ceil() has no effect. ... It only affects floating-point numbers. Q: How do I round up to a specific decimal place in NumPy?
🌐
SciPy
docs.scipy.org › doc › › numpy-1.13.0 › reference › generated › numpy.floor_divide.html
numpy.floor_divide — NumPy v1.13 Manual
June 10, 2017 - ceil · Round a number to the nearest integer toward infinity. Examples · >>> np.floor_divide(7,3) 2 >>> np.floor_divide([1., 2., 3., 4.], 2.5) array([ 0., 0., 1., 1.]) numpy.true_divide · numpy.float_power · © Copyright 2008-2009, The Scipy community. Last updated on Jun 10, 2017.