Reference · Math

math.floor

signature
math.floor(num) -> int

What it does

The largest int that is not above the number, as Python's math.floor. A float gives an int, not a float: math.floor(3.7) is 3 and math.floor(-3.7) is -4, since rounding goes toward minus infinity. An int is itself. For a decimal, or to keep a float a float, use floor.

Examples

print(math.floor(3.7), math.floor(-3.7), math.floor(2.0))print(math.floor(5), type_of(math.floor(3.7)))print(math.floor(7 / 2))
output
3 -4 25 int3

Edge cases

print(math.floor(-0.5), math.floor(-0.0), math.floor(0.1 + 0.2))print(math.floor(9.1e18), math.floor(9.22e18))print(math.floor(9007199254740995 / 2), math.floor(-7 / 2))
output
-1 0 09100000000000000000 92200000000000000004503599627370497 -4
this one fails
print(math.floor(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. A decimal is refused too, with a hint: convert it with float first, or use floor, which takes a decimal.

this one fails
print(math.floor(float("inf")))
output
line 1: `math.floor`: cannot convert inf to an int
this one fails
print(math.floor(dec("3.7")))
output
line 1: `math.floor` expects a number as argument 1, got dec (this works on floats: convert a dec with float(x))

Chaining

In a pipeline the number comes first, so math.floor takes it as its only argument. The result is an int, ready for a list index or a count:

print(17 / 4 | math.floor)print([2.5, 3.5] | math.fsum | math.floor)print([1.9, -1.9, 2.1] | map(math.floor))
output
46[1, -2, 2]

See also

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

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

Thread 0 posts

On math.floor(): 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