Neha Patil (Editor)

Floor and ceiling functions

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Floor and ceiling functions

In mathematics and computer science, the floor and ceiling functions map a real number to the greatest preceding or the least succeeding integer, respectively. More precisely, floor(x) = x is the greatest integer less than or equal to x and ceiling(x) = x is the least integer greater than or equal to x.

Contents

Notation

Carl Friedrich Gauss introduced the square bracket notation [ x ] for the floor function in his third proof of quadratic reciprocity (1808). This remained the standard in mathematics until Kenneth E. Iverson introduced the names "floor" and "ceiling" and the corresponding notations x and x in his 1962 book A Programming Language. Both notations are now used in mathematics; this article follows Iverson.

The floor function is also called the greatest integer or entier (French for "integer") function, and its value at x is called the integral part or integer part of x; for negative values of x, the latter terms are sometimes instead taken to be the value of the ceiling function, i.e., the value of x rounded to an integer towards 0. The language APL uses ⌊x; other computer languages commonly use notations like entier(x) (ALGOL), INT(x) (BASIC, MS Excel), or floor(x)(C, C++, R, and Python). In mathematics, it can also be written with boldface or double brackets [ [ x ] ] .

The ceiling function is usually denoted by ceil(x) or ceiling(x) in non-APL computer languages that have a notation for this function. The J Programming Language, a follow on to APL that is designed to use standard keyboard symbols, uses >. for ceiling and <. for floor. In mathematics, there is another notation with reversed boldface or double brackets ] ] x [ [ or just using normal reversed brackets ]x[.

The fractional part is the sawtooth function, denoted by { x } for real x and defined by the formula

{ x } = x x .

For all x,

0 { x } < 1.

Typesetting

The floor and ceiling functions are usually typeset with left and right square brackets where the upper (for floor function) or lower (for ceiling function) horizontal bars are missing, and, e.g., in the LaTeX typesetting system these symbols can be specified with the lfloor, floor, lceil and ceil commands in math mode. HTML 4.0 uses the same names: &lfloor;, &rfloor;, &lceil;, and &rceil;. Unicode contains codepoints for these symbols at U+2308U+230B: x, x.

Definition and properties

In the following formulas, x and y are real numbers, k, m, and n are integers, and Z is the set of integers (positive, negative, and zero).

Floor and ceiling may be defined by the set equations

x = max { m Z m x } , x = min { n Z n x } .

Since there is exactly one integer in a half-open interval of length one, for any real x there are unique integers m and n satisfying

x 1 < m x n < x + 1.

Then  x = m  and  x = n  may also be taken as the definition of floor and ceiling.

Equivalences

These formulas can be used to simplify expressions involving floors and ceilings.

x = m  if and only if  m x < m + 1 , x = n  if and only if  n 1 < x n , x = m  if and only if  x 1 < m x , x = n  if and only if  x n < x + 1.

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.

x < n  if and only if  x < n , n < x  if and only if  n < x , x n  if and only if  x n , n x  if and only if  n x .

These formulas show how adding integers to the arguments affect the functions:

x + n = x + n , x + n = x + n , { x + n } = { x } .

The above are never true if n is not an integer; however, for every x, y the following inequalities hold:

x + y x + y x + y + 1 , x + y 1 x + y x + y .

Relations among the functions

It is clear from the definitions that

x x ,   with equality if and only if x is an integer, i.e. x x = { 0  if  x Z 1  if  x Z

In fact, for integers n:

n = n = n .

Negating the argument switches floor and ceiling and changes the sign:

x + x = 0 x = x x = x

and:

x + x = { 0  if  x Z 1  if  x Z , x + x = { 0  if  x Z 1  if  x Z .

Negating the argument complements the fractional part:

{ x } + { x } = { 0  if  x Z 1  if  x Z .

The floor, ceiling, and fractional part functions are idempotent:

x = x , x = x , { { x } } = { x } .

The result of nested floor or ceiling functions is the innermost function:

x = x , x = x .

Quotients

If m and n are integers and n ≠ 0,

0 { m n } 1 1 | n | .

If n is a positive integer

x + m n = x + m n , x + m n = x + m n .

If m is positive

n = n m + n 1 m + + n m + 1 m , n = n m + n + 1 m + + n + m 1 m .

For m = 2 these imply

n = n 2 + n 2 .

More generally, for positive m (See Hermite's identity)

m x = x + x 1 m + + x m 1 m , m x = x + x + 1 m + + x + m 1 m .

The following can be used to convert floors to ceilings and vice versa (m positive)

n m = n + m 1 m = n 1 m + 1 , n m = n m + 1 m = n + 1 m 1 ,

If m and n are positive and coprime, then

i = 1 n 1 i m n = 1 2 ( m 1 ) ( n 1 ) .

Since the right-hand side is symmetrical in m and n, this implies that

m n + 2 m n + + ( n 1 ) m n = n m + 2 n m + + ( m 1 ) n m .

More generally, if m and n are positive,

x n + m + x n + 2 m + x n + + ( n 1 ) m + x n = x m + n + x m + 2 n + x m + + ( m 1 ) n + x m .

This is sometimes called a reciprocity law.

Nested divisions

For positive integer n, and arbitrary real numbers m,x:

x / m n = x m n x / m n = x m n .

Continuity and series expansions

None of the functions discussed in this article are continuous, but all are piecewise linear: the functions x , x , and { x } have discontinuities at the integers.

x   is upper semi-continuous and   x   and { x }   are lower semi-continuous.

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

{ x } = 1 2 1 π k = 1 sin ( 2 π k x ) k

for x 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 floor(x) = x − {x} gives

x = x 1 2 + 1 π k = 1 sin ( 2 π k x ) k

for x not an integer.

Mod operator

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 mod y = x y x y .

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 x mod y < y ,

and if y is negative,

0 x mod y > y .

Quadratic reciprocity

Gauss's third proof of quadratic reciprocity, as modified by Eisenstein, has two basic steps.

Let p and q be distinct positive odd prime numbers, and let

m = p 1 2 , n = q 1 2 .

First, Gauss's lemma is used to show that the Legendre symbols are given by

( q p ) = ( 1 ) q p + 2 q p + + m q p

and

( p q ) = ( 1 ) p q + 2 p q + + n p q .

The second step is to use a geometric argument to show that

q p + 2 q p + + m q p + p q + 2 p q + + n p q = m n .

Combining these formulas gives quadratic reciprocity in the form

( p q ) ( q p ) = ( 1 ) m n = ( 1 ) p 1 2 q 1 2 .

There are formulas that use floor to express the quadratic character of small numbers mod odd primes p:

( 2 p ) = ( 1 ) p + 1 4 , ( 3 p ) = ( 1 ) p + 1 6 .

Rounding

For an arbitrary real number x , rounding x to the nearest integer with tie breaking towards positive infinity is given by rpi ( x ) = x + 1 2 = 2 x 2 ; rounding towards negative infinity is given as rni ( x ) = x 1 2 = 2 x 2 .

If tie-breaking is away from 0, then the rounding function is ri ( x ) = sgn ( x ) | x | + 1 2 , and rounding towards even, as is usual in the nearest integer function, can be expressed with the more cumbersome x = x + 1 2 + 2 x 1 4 2 x 1 4 1 , which is the above expression for rounding towards positive infinity rpi ( x ) minus an integrality indicator for 2 x 1 4 .

Truncation

The truncation of a nonnegative number is given by x . The truncation of a nonpositive number is given by x .

The truncation of any real number can be given by: sgn ( x ) | x | , where sgn(x) is the sign function.

Number of digits

The number of digits in base b of a positive integer k is

Factors of factorials

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 the formula

n p + n p 2 + n p 3 + = n k a k p 1

where n = k a k p k is the way of writing n in base p. Note that this is a finite sum, since the floors are zero when pk > n.

Beatty sequence

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.

Euler's constant (γ)

There are formulas for Euler's constant γ = 0.57721 56649 ... that involve the floor and ceiling, e.g.

γ = 1 ( 1 x 1 x ) d x , γ = lim n 1 n k = 1 n ( n k n k ) ,

and

γ = k = 2 ( 1 ) k log 2 k k = 1 2 1 3 + 2 ( 1 4 1 5 + 1 6 1 7 ) + 3 ( 1 8 1 15 ) +

Riemann function (ζ)

The fractional part function also shows up in integral representations of the Riemann zeta function. It is straightforward to prove (using integration by parts) that if φ(x) is any function with a continuous derivative in the closed interval [a, b],

a < n b ϕ ( n ) = a b ϕ ( x ) d x + a b ( { x } 1 2 ) ϕ ( x ) d x + ( { a } 1 2 ) ϕ ( a ) ( { b } 1 2 ) ϕ ( b ) .

Letting φ(n) = n−s for real part of s greater than 1 and letting a and b be integers, and letting b approach infinity gives

ζ ( s ) = s 1 1 2 { x } x s + 1 d x + 1 s 1 + 1 2 .

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 {x} can be used to extend the zeta function to the entire complex plane and to prove its functional equation.

For s = σ + i t in the critical strip (i.e. 0 < σ < 1),

ζ ( s ) = s e σ ω ( e ω e ω ) e i t ω d ω .

In 1947 van der Pol used this representation to construct an analogue computer for finding roots of the zeta function.

Formulas for prime numbers

n is a prime if and only if

m = 1 n + 1 ( n m n 1 m ) = 1.

Let r > 1 be an integer, pn be the nth prime, and define

α = m = 1 p m r m 2 .

Then

p n = r n 2 α r 2 n 1 r ( n 1 ) 2 α .

There is a number θ = 1.3064... (Mills' constant) with the property that

θ 3 , θ 9 , θ 27 ,

are all prime.

There is also a number ω = 1.9287800... with the property that

2 ω , 2 2 ω , 2 2 2 ω ,

are all prime.

π(x) is the number of primes less than or equal to x. It is a straightforward deduction from Wilson's theorem that

π ( n ) = j = 2 n ( j 1 ) ! + 1 j ( j 1 ) ! j .

Also, if n ≥ 2,

π ( n ) = j = 2 n 1 k = 2 j j k k j .

None of the formulas in this section are of any practical use.

Solved problems

Ramanujan submitted these problems to the Journal of the Indian Mathematical Society.

If n is a positive integer, prove that

(i)     n 3 + n + 2 6 + n + 4 6 = n 2 + n + 3 6 ,

(ii)     1 2 + n + 1 2 = 1 2 + n + 1 4 ,

(iii)     n + n + 1 = 4 n + 2 .

Unsolved problem

The study of Waring's problem has led to an unsolved problem:

Are there any positive integers k ≥ 6 such that

3 k 2 k ( 3 2 ) k > 2 k ( 3 2 ) k 2  ?

Mahler has proved there can only be a finite number of such k; none are known.

Computer implementations

Many programming languages (including C, C++, PHP, and Python) provide standard functions for floor and ceiling.

Spreadsheet software

Most spreadsheet programs support some form of a ceiling function. Although the details differ between programs, most implementations support a second parameter—a multiple of which the given number is to be rounded to. For example, ceiling(2, 3) rounds 2 up to the nearest multiple of 3, giving 3. The definition of what "round up" means, however, differs from program to program.

Until Excel 2010, Microsoft Excel's ceiling function was incorrect for negative arguments; ceiling(-4.5) was -5. This has followed through to the Office Open XML file format. Excel 2010 now follows the standard definition. Meanwhile its roundup function continues to round away from zero.

The OpenDocument file format, as used by OpenOffice.org and others, follows the mathematical definition of ceiling for its ceiling function, with an optional parameter for Excel compatibility. For example, CEILING(-4.5) returns −4.

References

Floor and ceiling functions Wikipedia