Reference · Math
floor
floor(num | dec) -> num | decWhat it does
The largest whole number not above the argument: 2.7 gives 2, -2.7 gives -3.
An int or a float gives an int; a decimal gives a decimal with no decimals.
Examples
print(floor(2.7), floor(-2.7), floor(3), floor(2.0))print(floor(2.7m), floor(-2.7m))print(floor(7 / 2))2 -3 3 22 -33Edge cases
-
Negative numbers go down, away from zero:
floor(-0.5)is-1.intcuts toward zero instead. -
The result of a float is an int; of a decimal a decimal:
floor(2.50m)is2m. -
A float gives an int up to 2^63 (about 9.22 × 10¹⁸):
type_of(floor(1e16))isint. Past that, andNaNand infinities, it stays a float. -
An int is itself, past 2^53 too, as it never goes through a float:
floor(9007199254740993)is9007199254740993, on the server and in the browser. -
floor(a / b)of two ints is integer division, done in ints, so it is exact past 2^53 (Python'sa // b), however the call is written:floor(a / b),a / b | floor,(a / b).floor(),math.floor(a / b), orfloorkept in a variable. A quotient kept in a variable first is a float, which has only 53 bits of digits. A division by 0 is a runtime error.
print(floor(-0.5), int(-0.5))print(type_of(floor(2.7)), type_of(floor(2.7m)), floor(2.50m))print(floor(1e16), type_of(floor(1e16)), floor(float("inf")))let a = 9007199254740995print(floor(a / 2), a / 2 | floor, floor(-7 / 2))let q = a / 2print(floor(q), type_of(q))-1 0int dec 210000000000000000 int inf4503599627370497 4503599627370497 -44503599627370498 floatErrors
A string, null or a boolean is a runtime error, not an error value.
print(floor("x"))line 1: `floor` expects a number as argument 1, got strChaining
print(3.7 | floor, [1.9, 2.1] | map(floor))print(17 / 5 | floor, 17 % 5)3 [1, 2]3 2See also
Thread 0 posts
On 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