Reference · Math

math.ceil

signature
math.ceil(num) -> int

What 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))
output
4 -3 2-5 int4

Edge cases

print(math.ceil(-0.5), math.ceil(0.3), math.ceil(21 / 10))print(math.ceil(9007199254740995 / 2))
output
0 1 34503599627370498
this one fails
print(math.ceil(1e19))
output
line 1: integer overflow

Errors

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.

this one fails
print(math.ceil("3"))
output
line 1: `math.ceil` expects a number as argument 1, got str
this one fails
print(math.ceil(float("nan")))
output
line 1: `math.ceil`: cannot convert NaN to an int

Chaining

In a pipeline the number comes first:

print(21 / 10 | math.ceil)print([0.1, 1.5, 2.0] | map(math.ceil))
output
3[1, 2, 2]

See also

math.floor, math.trunc, ceil, round, int

Thread
7Useful? Like it, or add to its thread below.

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