In mathematics, the floor function is the function that takes as input a real number, and gives as output the greatest integer less than or equal to, denoted or . Similarly, the ceiling function maps to the smallest integer greater than or equal to, denoted or .[1]
For example, for floor:,, and for ceiling:, and .
The floor of is also called the integral part, integer part, greatest integer, or entier of, and was historically denoted (among other notations).[2] However, the same term, integer part, is also used for truncation towards zero, which differs from the floor function for negative numbers.
For an integer, .
Although and produce graphs that appear exactly alike, they are not the same when the value of x is an exact integer. For example, when =2.0001; . However, if =2, then, while .
2 | 2 | 2 | 0 | |
---|---|---|---|---|
2.0001 | 2 | 3 | 0.0001 | |
2.4 | 2 | 3 | 0.4 | |
2.9 | 2 | 3 | 0.9 | |
2.999 | 2 | 3 | 0.999 | |
-2.7 | -3 | -2 | 0.3 | |
-2 | -2 | -2 | 0 |
The integral part or integer part of a number (French: partie entière in the original) was first defined in 1798 by Adrien-Marie Legendre in his proof of the Legendre's formula.
Carl Friedrich Gauss introduced the square bracket notation in his third proof of quadratic reciprocity (1808).[3] This remained the standard[4] in mathematics until Kenneth E. Iverson introduced, in his 1962 book A Programming Language, the names "floor" and "ceiling" and the corresponding notations and .[5] [6] (Iverson used square brackets for a different purpose, the Iverson bracket notation.) Both notations are now used in mathematics, although Iverson's notation will be followed in this article.
In some sources, boldface or double brackets are used for floor, and reversed brackets or for ceiling.[7] [8]
The fractional part is the sawtooth function, denoted by for real and defined by the formula
For all x,
.
These characters are provided in Unicode:
In the LaTeX typesetting system, these symbols can be specified with the {{mono|\lceil, \rceil, \lfloor, }}
and {{mono|\rfloor}}
commands in math mode. LaTeX has supported UTF-8 since 2018, so the Unicode characters can now be used directly.[10] Larger versions are{{mono|\left\lceil, \right\rceil, \left\lfloor,}}
and {{mono|\right\rfloor}}
.
Given real numbers x and y, integers m and n and the set of integers
Z
\lfloorx\rfloor=max\{m\inZ\midm\lex\},
\lceilx\rceil=min\{n\inZ\midn\gex\}.
Since there is exactly one integer in a half-open interval of length one, for any real number x, there are unique integers m and n satisfying the equation
x-1<m\lex\len<x+1.
where
\lfloorx\rfloor=m
\lceilx\rceil=n
These formulas can be used to simplify expressions involving floors and ceilings.[11]
\begin{alignat}{3} \lfloorx\rfloor&=m &&ifandonlyif&m&\lex<m+1,\\ \lceilx\rceil&=n&&ifandonlyif& n-1&<x\len,\\ \lfloorx\rfloor&=m&&ifandonlyif&x-1&<m\lex,\\ \lceilx\rceil&=n&&ifandonlyif&x&\len<x+1. \end{alignat}
In the language of order theory, the floor function is a residuated mapping, that is, part of a Galois connection: it is the upper adjoint of the function that embeds the integers into the reals.
\begin{align} x<n& ifandonlyif&\lfloorx\rfloor&<n,\\ n<x& ifandonlyif&n&<\lceilx\rceil,\\ x\len& ifandonlyif&\lceilx\rceil&\len,\\ n\lex& ifandonlyif&n&\le\lfloorx\rfloor. \end{align}
These formulas show how adding an integer to the arguments affects the functions:
\begin{align} \lfloorx+n\rfloor&=\lfloorx\rfloor+n,\\ \lceilx+n\rceil&=\lceilx\rceil+n,\\ \{x+n\}&=\{x\}. \end{align}
The above are never true if is not an integer; however, for every and, the following inequalities hold:
\begin{align} \lfloorx\rfloor+\lfloory\rfloor&\leq\lfloorx+y\rfloor\leq\lfloorx\rfloor+\lfloory\rfloor+1,\\[3mu] \lceilx\rceil+\lceily\rceil-1&\leq\lceilx+y\rceil\leq\lceilx\rceil+\lceily\rceil. \end{align}
Both floor and ceiling functions are monotonically non-decreasing functions:
\begin{align} x1\lex2& ⇒ \lfloorx1\rfloor\le\lfloorx2\rfloor,\\ x1\lex2& ⇒ \lceilx1\rceil\le\lceilx2\rceil.\end{align}
It is clear from the definitions that
\lfloorx\rfloor\le\lceilx\rceil,
\lceilx\rceil-\lfloorx\rfloor=\begin{cases} 0&ifx\inZ\\ 1&ifx\not\inZ \end{cases}
In fact, for integers n, both floor and ceiling functions are the identity:
\lfloorn\rfloor=\lceiln\rceil=n.
Negating the argument switches floor and ceiling and changes the sign:
\begin{align} \lfloorx\rfloor+\lceil-x\rceil&=0\\ -\lfloorx\rfloor&=\lceil-x\rceil\\ -\lceilx\rceil&=\lfloor-x\rfloor \end{align}
and:
\lfloorx\rfloor+\lfloor-x\rfloor=\begin{cases} 0&ifx\inZ\\ -1&ifx\not\inZ, \end{cases}
\lceilx\rceil+\lceil-x\rceil=\begin{cases} 0&ifx\inZ\\ 1&ifx\not\inZ. \end{cases}
Negating the argument complements the fractional part:
\{x\}+\{-x\}=\begin{cases} 0&ifx\inZ\\ 1&ifx\not\inZ. \end{cases}
The floor, ceiling, and fractional part functions are idempotent:
\begin{align} \lfloor\lfloorx\rfloor\rfloor&=\lfloorx\rfloor,\\ \lceil\lceilx\rceil\rceil&=\lceilx\rceil,\\ \{\{x\}\}&=\{x\}. \end{align}
The result of nested floor or ceiling functions is the innermost function:
\begin{align} \lfloor\lceilx\rceil\rfloor&=\lceilx\rceil,\\ \lceil\lfloorx\rfloor\rceil&=\lfloorx\rfloor \end{align}
If m and n are integers and n ≠ 0,
0\le\left\{
m | |
n |
\right\}\le1-
1 | |
|n| |
.
If n is a positive integer[12]
\left\lfloor | x+m |
n |
\right\rfloor=\left\lfloor
\lfloorx\rfloor+m | |
n |
\right\rfloor,
\left\lceil | x+m |
n |
\right\rceil=\left\lceil
\lceilx\rceil+m | |
n |
\right\rceil.
If m is positive[13]
n=\left\lceil | n\vphantom1 |
m |
\right\rceil+\left\lceil
n-1 | |
m |
\right\rceil+...+\left\lceil
n-m+1 | |
m |
\right\rceil,
n=\left\lfloor | n\vphantom1 |
m |
\right\rfloor+\left\lfloor
n+1 | |
m |
\right\rfloor+...+\left\lfloor
n+m-1 | |
m |
\right\rfloor.
For m = 2 these imply
n=\left\lfloor
n\vphantom1 | |
2 |
\right\rfloor+\left\lceil
n\vphantom1 | |
2 |
\right\rceil.
More generally,[14] for positive m (See Hermite's identity)
\lceilmx\rceil=\left\lceilx\right\rceil+\left\lceilx-
1 | |
m |
\right\rceil+...+\left\lceilx-
m-1 | |
m |
\right\rceil,
\lfloormx\rfloor=\left\lfloorx\right\rfloor+\left\lfloorx+
1 | |
m |
\right\rfloor+...+\left\lfloorx+
m-1 | |
m |
\right\rfloor.
The following can be used to convert floors to ceilings and vice versa (m positive)[15]
\left\lceil
n\vphantom1 | |
m |
\right\rceil=\left\lfloor
n+m-1 | |
m |
\right\rfloor=\left\lfloor
n-1 | |
m |
\right\rfloor+1,
\left\lfloor
n\vphantom1 | |
m |
\right\rfloor=\left\lceil
n-m+1 | |
m |
\right\rceil=\left\lceil
n+1 | |
m |
\right\rceil-1,
For all m and n strictly positive integers:[16]
n-1 | |
\sum | |
k=1 |
\left\lfloor
km | |
n |
\right\rfloor=
(m-1)(n-1)+\gcd(m,n)-1 | |
2, |
which, for positive and coprime m and n, reduces to
n-1 | |
\sum | |
k=1 |
\left\lfloor
km | |
n |
\right\rfloor=\tfrac{1}{2}(m-1)(n-1),
and similarly for the ceiling and fractional part functions (still for positive and coprime m and n),
n-1 | |
\sum | |
k=1 |
\left\lceil
km | |
n |
\right\rceil=\tfrac{1}{2}(m+1)(n-1),
n-1 | |
\sum | |
k=1 |
\left\{
km | |
n |
\right\}=\tfrac{1}{2}(n-1).
Since the right-hand side of the general case is symmetrical in m and n, this implies that
\left\lfloor
m\vphantom1 | |
n |
\right\rfloor+\left\lfloor
2m | |
n |
\right\rfloor+...+\left\lfloor
(n-1)m | |
n |
\right\rfloor= \left\lfloor
n\vphantom1 | |
m |
\right\rfloor+\left\lfloor
2n | |
m |
\right\rfloor+...+\left\lfloor
(m-1)n | |
m |
\right\rfloor.
More generally, if m and n are positive,
\begin{align} &\left\lfloor
x\vphantom1 | |
n |
\right\rfloor+ \left\lfloor
m+x | |
n |
\right\rfloor+ \left\lfloor
2m+x | |
n |
\right\rfloor+ ...+ \left\lfloor
(n-1)m+x | |
n |
\right\rfloor\\[5mu] = &\left\lfloor
x\vphantom1 | |
m |
\right\rfloor+ \left\lfloor
n+x | |
m |
\right\rfloor+ \left\lfloor
2n+x | |
m |
\right\rfloor+ … + \left\lfloor
(m-1)n+x | |
m |
\right\rfloor. \end{align}
This is sometimes called a reciprocity law.[17]
Division by positive integers gives rise to an interesting and sometimes useful property. Assuming
m,n>0
m\leq\left\lfloor
x | |
n |
\right\rfloor\iffn\leq\left\lfloor
x | |
m |
\right\rfloor\iffn\leq
\lfloorx\rfloor | |
m |
.
Similarly,
m\geq\left\lceil
x | |
n |
\right\rceil\iffn\geq\left\lceil
x | |
m |
\right\rceil\iffn\geq
\lceilx\rceil | |
m |
.
Indeed,
m\leq\left\lfloor
x | |
n |
\right\rfloor\impliesm\leq
x | |
n |
\impliesn\leq
x | |
m |
\impliesn\leq\left\lfloor
x | |
m |
\right\rfloor\implies\ldots\impliesm\leq\left\lfloor
x | |
n |
\right\rfloor,
For positive integer n, and arbitrary real numbers m,x:[18]
\left\lfloor
\lfloorx/m\rfloor | |
n |
\right\rfloor=\left\lfloor
x | |
mn |
\right\rfloor
\left\lceil
\lceilx/m\rceil | |
n |
\right\rceil=\left\lceil
x | |
mn |
\right\rceil.
None of the functions discussed in this article are continuous, but all are piecewise linear: the functions
\lfloorx\rfloor
\lceilx\rceil
\{x\}
\lfloorx\rfloor
\lceilx\rceil
\{x\}
Since none of the functions discussed in this article are continuous, none of them have a power series expansion. Since floor and ceiling are not periodic, they do not have uniformly convergent Fourier series expansions. The fractional part function has Fourier series expansion[19] for not an integer.
At points of discontinuity, a Fourier series converges to a value that is the average of its limits on the left and the right, unlike the floor, ceiling and fractional part functions: for y fixed and x a multiple of y the Fourier series given converges to y/2, rather than to x mod y = 0. At points of continuity the series converges to the true value.
Using the formula
\lfloorx\rfloor=x-\{x\}
For an integer x and a positive integer y, the modulo operation, denoted by x mod y, gives the value of the remainder when x is divided by y. This definition can be extended to real x and y, y ≠ 0, by the formula
x\bmody=x-y\left\lfloor
x | |
y |
\right\rfloor.
Then it follows from the definition of floor function that this extended operation satisfies many natural properties. Notably, x mod y is always between 0 and y, i.e.,
if y is positive,
0\lex\bmody<y,
0\gex\bmody>y.
Gauss's third proof of quadratic reciprocity, as modified by Eisenstein, has two basic steps.[20] [21]
Let p and q be distinct positive odd prime numbers, and let
m=\tfrac12(p-1),
n=\tfrac12(q-1).
First, Gauss's lemma is used to show that the Legendre symbols are given by
\begin{align} \left( | q |
p |
\right) &=
| |||||||||||
(-1) |
,\\[5mu] \left(
p | |
q |
\right) &=
| |||||||||||
(-1) |
. \end{align}
The second step is to use a geometric argument to show that
\left\lfloor | q |
p |
\right\rfloor+\left\lfloor
2q | |
p |
\right\rfloor+...+\left\lfloor
mq | \right\rfloor +\left\lfloor | |
p |
p | |
q |
\right\rfloor+\left\lfloor
2p | |
q |
\right\rfloor+...+\left\lfloor
np | |
q |
\right\rfloor =mn.
Combining these formulas gives quadratic reciprocity in the form
\left( | p |
q |
\right)\left(
q | |
p |
\right)=(-1)mn
| ||||||||
=(-1) |
.
There are formulas that use floor to express the quadratic character of small numbers mod odd primes p:[22]
\begin{align} \left( | 2 |
p |
\right)&=
| |||||
(-1) |
,\\[5mu] \left(
3 | |
p |
\right)&=
| |||||
(-1) |
. \end{align}
For an arbitrary real number
x
x
rpi(x)=\left\lfloorx+\tfrac{1}{2}\right\rfloor=\left\lceil\tfrac12\lfloor2x\rfloor\right\rceil
rni(x)=\left\lceilx-\tfrac{1}{2}\right\rceil=\left\lfloor\tfrac12\lceil2x\rceil\right\rfloor
If tie-breaking is away from 0, then the rounding function is
ri(x)=sgn(x)\left\lfloor|x|+\tfrac{1}{2}\right\rfloor
\lfloorx\rceil=\left\lfloorx+\tfrac{1}{2}\right\rfloor+l\lceil\tfrac14(2x-1)r\rceil-l\lfloor\tfrac14(2x-1)r\rfloor-1
rpi(x)
\tfrac14(2x-1)
x
\Delta
Q(x)=\Delta ⋅ \left\lfloor
x | |
\Delta |
+
1 | |
2 |
\right\rfloor
The number of digits in base b of a positive integer k is
\lfloorlogb{k}\rfloor+1=\lceillogb{(k+1)}\rceil.
The number of possible strings of arbitrary length that doesn't use any character twice is given by[23]
(n)0+ … +(n)n=\lflooren!\rfloor
where:
(n)k=n(n-1) … (n-k+1)
For = 26, this comes out to 1096259850353149530222034277.
Let n be a positive integer and p a positive prime number. The exponent of the highest power of p that divides n! is given by a version of Legendre's formula[24]
\left\lfloor | n |
p |
\right\rfloor+\left\lfloor
n | |
p2 |
\right\rfloor+\left\lfloor
n | |
p3 |
\right\rfloor+...=
n-\sumkak | |
p-1 |
where is the way of writing n in base p. This is a finite sum, since the floors are zero when pk > n.
The Beatty sequence shows how every positive irrational number gives rise to a partition of the natural numbers into two sequences via the floor function.[25]
There are formulas for Euler's constant γ = 0.57721 56649 ... that involve the floor and ceiling, e.g.[26]
\gamma
infty\left({1\over\lfloor | |
=\int | |
1 |
x\rfloor}-{1\overx}\right)dx,
\gamma=\limn
1 | |
n |
n | |
\sum | |
k=1 |
\left(\left\lceil
n | |
k |
\right\rceil-
n | |
k |
\right),
and
\gamma=
infty | |
\sum | |
k=2 |
(-1)k
\left\lfloorlog2k\right\rfloor | |
k |
=\tfrac12-\tfrac13 +2\left(\tfrac14-\tfrac15+\tfrac16-\tfrac17\right) +3\left(\tfrac18- … -\tfrac1{15}\right)+ …
The fractional part function also shows up in integral representations of the Riemann zeta function. It is straightforward to prove (using integration by parts)[27] that if
\varphi(x)
\suma<n\le\varphi(n)
b\varphi(x) | |
= \int | |
a |
dx
b\left(\{x\}-\tfrac12\right)\varphi'(x) | |
+ \int | |
a |
dx+ \left(\{a\}-\tfrac12\right)\varphi(a)- \left(\{b\}-\tfrac12\right)\varphi(b).
Letting
\varphi(n)=n-s
\zeta(s)=
| |||||||
s\int | |||||||
1 |
This formula is valid for all s with real part greater than -1, (except s = 1, where there is a pole) and combined with the Fourier expansion for can be used to extend the zeta function to the entire complex plane and to prove its functional equation.[28]
For s = σ + it in the critical strip 0 < σ < 1,
infty | |
\zeta(s)=s\int | |
-infty |
e-\sigma\omega(\lfloore\omega\rfloor-e\omega)e-it\omegad\omega.
In 1947 van der Pol used this representation to construct an analogue computer for finding roots of the zeta function.[29]
The floor function appears in several formulas characterizing prime numbers. For example, since is equal to 1 if m divides n, and to 0 otherwise, it follows that a positive integer n is a prime if and only if[30]
infty | ||
\sum | \left(l\lfloor | |
m=1 |
n | r\rfloor-l\lfloor | |
m |
n-1 | |
m |
r\rfloor\right)=2.
One may also give formulas for producing the prime numbers. For example, let pn be the n-th prime, and for any integer r > 1, define the real number α by the sum
\alpha=
infty | |
\sum | |
m=1 |
pm
-m2 | |
r |
.
Then[31]
pn=\left\lfloor
n2 | |
r |
\alpha\right\rfloor-r2n-1\left\lfloor
(n-1)2 | |
r |
\alpha\right\rfloor.
A similar result is that there is a number θ = 1.3064... (Mills' constant) with the property that
\left\lfloor\theta3\right\rfloor,\left\lfloor\theta9\right\rfloor,\left\lfloor\theta27\right\rfloor,...
are all prime.[32]
There is also a number ω = 1.9287800... with the property that
\left\lfloor2\omega\right\rfloor,\left\lfloor
2\omega | |
2 |
\right\rfloor,\left\lfloor
| |||||
2 |
\right\rfloor,...
are all prime.[32]
Let (x) be the number of primes less than or equal to x. It is a straightforward deduction from Wilson's theorem that[33]
\pi(n)=
| ||||
\sum | ||||
j=2 |
-\left\lfloor
(j-1)! | |
j |
\right\rfloorr\rfloor.
Also, if n ≥ 2,[34]
\pi(n)=
n | |
\sum | |
j=2 |
l\lfloor1
| |||||
/ {\sum | \right\rfloor | ||||
k=2 |
k | |
j |
\right\rfloor}r\rfloor.
None of the formulas in this section are of any practical use.[35] [36]
Ramanujan submitted these problems to the Journal of the Indian Mathematical Society.[37]
If n is a positive integer, prove that
\left\lfloor\tfrac{n}{3}\right\rfloor+\left\lfloor\tfrac{n+2}{6}\right\rfloor+\left\lfloor\tfrac{n+4}{6}\right\rfloor=\left\lfloor\tfrac{n}{2}\right\rfloor+\left\lfloor\tfrac{n+3}{6}\right\rfloor,
\left\lfloor\tfrac12+\sqrt{n+\tfrac12}\right\rfloor=\left\lfloor\tfrac12+\sqrt{n+\tfrac14}\right\rfloor,
\left\lfloor\sqrt{n}+\sqrt{n+1}\right\rfloor=\left\lfloor\sqrt{4n+2}\right\rfloor.
The study of Waring's problem has led to an unsolved problem:
Are there any positive integers k ≥ 6 such that[39]
3k-2kl\lfloorl(\tfrac32r)kr\rfloor>2k-l\lfloorl(\tfrac32r)kr\rfloor-2 ?
Mahler has proved there can only be a finite number of such k; none are known.[40]
In most programming languages, the simplest method to convert a floating point number to an integer does not do floor or ceiling, but truncation. The reason for this is historical, as the first machines used ones' complement and truncation was simpler to implement (floor is simpler in two's complement). FORTRAN was defined to require this behavior and thus almost all processors implement conversion this way. Some consider this to be an unfortunate historical design decision that has led to bugs handling negative offsets and graphics on the negative side of the origin.
An arithmetic right-shift of a signed integer
x
n
\left\lfloorx/2n\right\rfloor
Many programming languages (including C, C++,[41] [42] C#,[43] [44] Java,[45] [46] PHP,[47] [48] R,[49] and Python[50]) provide standard functions for floor and ceiling, usually called floor
and ceil
, or less commonly ceiling
.[51] The language APL uses ⌊x
for floor. The J Programming Language, a follow-on to APL that is designed to use standard keyboard symbols, uses <.
for floor and >.
for ceiling.[52] ALGOL usesentier
for floor.
In Microsoft Excel the funtion INT
rounds down rather than toward zero,[53] while FLOOR
rounds toward zero, the opposite of what "int" and "floor" do in other languages. Since 2010 FLOOR
has been changed to error if the number is negative.[54] The OpenDocument file format, as used by OpenOffice.org, Libreoffice and others, INT
[55] and FLOOR
both do floor, and FLOOR
has a third argument to reproduce Excel's earlier behavior.[56]
floor
function . 5 December 2010.ceil
function . 5 December 2010.ceil
function . 18 July 2013.floor
function . 18 July 2013.math
module . 18 July 2013.