Reference · Math
math.ceil
math.ceil(num) -> intWhat it does
The smallest int that is not below the number, as Python's math.ceil. A float gives
an int: math.ceil(3.2) is 4 and math.ceil(-3.2) is -3, since rounding goes
toward plus infinity. An int is itself. For a decimal, or to keep a float a float,
use ceil.
Examples
print(math.ceil(3.2), math.ceil(-3.2), math.ceil(2.0))print(math.ceil(-5), type_of(math.ceil(3.2)))print(math.ceil(7 / 2))4 -3 2-5 int4Edge cases
- A negative number rounds toward zero:
math.ceil(-0.5)is0, the same as math.trunc gives there. - Any positive fraction, however small, rounds up:
math.ceil(0.3)is1. - A float past what an int holds (2^63, about 9.22 × 10¹⁸) is a runtime error,
integer overflow; up to it, an int. - Pages of ten for 21 items:
math.ceil(21 / 10)is3. Of two ints written in the call, the division is done in ints, so it is exact past 2^53 too; a quotient kept in a variable first is a float.
print(math.ceil(-0.5), math.ceil(0.3), math.ceil(21 / 10))print(math.ceil(9007199254740995 / 2))0 1 34503599627370498print(math.ceil(1e19))line 1: integer overflowErrors
Infinity and NaN cannot be an int and are a runtime error, not an error value; so is
an argument that is not a number, text included. A decimal is refused with a hint to
convert it with float, or use ceil.
print(math.ceil("3"))line 1: `math.ceil` expects a number as argument 1, got strprint(math.ceil(float("nan")))line 1: `math.ceil`: cannot convert NaN to an intChaining
In a pipeline the number comes first:
print(21 / 10 | math.ceil)print([0.1, 1.5, 2.0] | map(math.ceil))3[1, 2, 2]See also
math.floor, math.trunc, ceil, round, int
Thread 0 posts
On math.ceil(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo