Python math.ceil syntax
math.ceil(x)
In Python, math.ceil rounds toward positive infinity. The ceiling of 2.3 is 3, the ceiling of −2.3 is −2, and 2 stays 2.
math.ceil
import math
print(math.ceil(2.3))
3
math.ceil(x)
| Input | A finite real number in these examples. Import math first. |
|---|---|
| Result | An int for built-in integers and finite floats. Infinity raises OverflowError and NaN raises ValueError. |
A test bench increases a reading by one tenth of a unit at a time. Watch where the rounded display changes as the sample passes from one integer toward the next.
import math
class MathExample:
def __init__(self):
pass
def getCeilValue(self):
num1 = math.ceil(1.0);
num2 = math.ceil(1.1);
num3 = math.ceil(1.2);
num4 = math.ceil(1.3);
num5 = math.ceil(1.4);
num6 = math.ceil(1.5);
num7 = math.ceil(1.6);
num8 = math.ceil(1.7);
num9 = math.ceil(1.8);
num10 = math.ceil(1.9);
num11 = math.ceil(2.0);
print("The ceiling value of 1.0 is " + str(num1));
print("The ceiling value of 1.1 is " + str(num2));
print("The ceiling value of 1.2 is " + str(num3));
print("The ceiling value of 1.3 is " + str(num4));
print("The ceiling value of 1.4 is " + str(num5));
print("The ceiling value of 1.5 is " + str(num6));
print("The ceiling value of 1.6 is " + str(num7));
print("The ceiling value of 1.7 is " + str(num8));
print("The ceiling value of 1.8 is " + str(num9));
print("The ceiling value of 1.9 is " + str(num10));
print("The ceiling value of 2.0 is " + str(num11));
obj = MathExample()
obj.getCeilValue()
The ceiling value of 1.0 is 1 The ceiling value of 1.1 is 2 The ceiling value of 1.2 is 2 The ceiling value of 1.3 is 2 The ceiling value of 1.4 is 2 The ceiling value of 1.5 is 2 The ceiling value of 1.6 is 2 The ceiling value of 1.7 is 2 The ceiling value of 1.8 is 2 The ceiling value of 1.9 is 2 The ceiling value of 2.0 is 2
A scheduling display groups positive measurements into whole-unit intervals. Compare readings just on either side of an integer to see which interval the display would select.
import math
class MathExample:
def __init__(self):
pass
def getCeilValue(self):
num1 = math.ceil(1.5698);
num2 = math.ceil(1.3093);
num3 = math.ceil(1.0087);
num4 = math.ceil(1.1038);
num5 = math.ceil(1.9573);
num6 = math.ceil(1.7462);
num7 = math.ceil(1.6427);
num8 = math.ceil(1.4128);
num9 = math.ceil(1.8099);
num10 = math.ceil(1.2743);
num11 = math.ceil(2.0001);
print("The ceiling value of 1.5698 is " + str(num1));
print("The ceiling value of 1.3093 is " + str(num2));
print("The ceiling value of 1.0087 is " + str(num3));
print("The ceiling value of 1.1038 is " + str(num4));
print("The ceiling value of 1.9573 is " + str(num5));
print("The ceiling value of 1.7462 is " + str(num6));
print("The ceiling value of 1.6427 is " + str(num7));
print("The ceiling value of 1.4128 is " + str(num8));
print("The ceiling value of 1.8099 is " + str(num9));
print("The ceiling value of 1.2743 is " + str(num10));
print("The ceiling value of 2.0001 is " + str(num11));
obj = MathExample()
obj.getCeilValue()
The ceiling value of 1.5698 is 2 The ceiling value of 1.3093 is 2 The ceiling value of 1.0087 is 2 The ceiling value of 1.1038 is 2 The ceiling value of 1.9573 is 2 The ceiling value of 1.7462 is 2 The ceiling value of 1.6427 is 2 The ceiling value of 1.4128 is 2 The ceiling value of 1.8099 is 2 The ceiling value of 1.2743 is 2 The ceiling value of 2.0001 is 3
A temperature logger records values just below zero or another integer boundary. Follow the rounding direction on the negative side before using the result as the label of a chart interval.
import math
class MathExample:
def __init__(self):
pass
def getCeilValue(self):
num1 = math.ceil(-1.5698);
num2 = math.ceil(-1.3093);
num3 = math.ceil(-1.0087);
num4 = math.ceil(-1.1038);
num5 = math.ceil(-1.9573);
num6 = math.ceil(-1.7462);
num7 = math.ceil(-1.6427);
num8 = math.ceil(-1.4128);
num9 = math.ceil(-1.8099);
num10 = math.ceil(-1.2743);
num11 = math.ceil(-2.0001);
print("The ceiling value of -1.5698 is " + str(num1));
print("The ceiling value of -1.3093 is " + str(num2));
print("The ceiling value of -1.0087 is " + str(num3));
print("The ceiling value of -1.1038 is " + str(num4));
print("The ceiling value of -1.9573 is " + str(num5));
print("The ceiling value of -1.7462 is " + str(num6));
print("The ceiling value of -1.6427 is " + str(num7));
print("The ceiling value of -1.4128 is " + str(num8));
print("The ceiling value of -1.8099 is " + str(num9));
print("The ceiling value of -1.2743 is " + str(num10));
print("The ceiling value of -2.0001 is " + str(num11));
obj = MathExample()
obj.getCeilValue()
The ceiling value of -1.5698 is -1 The ceiling value of -1.3093 is -1 The ceiling value of -1.0087 is -1 The ceiling value of -1.1038 is -1 The ceiling value of -1.9573 is -1 The ceiling value of -1.7462 is -1 The ceiling value of -1.6427 is -1 The ceiling value of -1.4128 is -1 The ceiling value of -1.8099 is -1 The ceiling value of -1.2743 is -1 The ceiling value of -2.0001 is -2
A sensor can use infinity to mark a reading outside its measuring range. Pass both signs through this operation so the display can handle the result or the reported error deliberately.
import math
class MathExample:
def __init__(self):
pass
def getCeilValue(self):
# Positive ∞ infinite value
n = math.ceil(float("inf"))
print("The ceiling value of inf is " + str(n))
# Negative ∞ infinite value
x = math.ceil(-float("inf"))
print("The ceiling value of -inf is " + str(x))
obj = MathExample()
obj.getCeilValue()
OverflowError: cannot convert float infinity to integer
A teacher has 73 passengers on the museum trip, including the adults. Each minibus seats 24. She needs enough vehicles for the last passenger as well as the first 72.
import math
passengers, seats = 73, 24
buses = math.ceil(passengers / seats)
print(f"{buses} minibuses")
4 minibuses
Three minibuses seat 72 people. Dividing before rounding up reserves the fourth vehicle for the remaining passenger.
Reference: Python numeric operations