Discrete Mathematics and Combinatorics
Floor and Ceiling Functions and Their Useful Identities
What floor and ceiling mean, the identities worth knowing, how to prove them, and where rounding trips people up.
Key takeaways
- Floor rounds toward negative infinity and ceiling rounds toward positive infinity, so ⌊−2.5⌋ = −3 and ⌈−2.5⌉ = −2.
- The defining inequality n ≤ x < n + 1 for ⌊x⌋ = n proves most identities.
- Integers can be pulled out of a floor or ceiling, but other multipliers and general sums cannot.
- The count of positive multiples of d up to N is ⌊N/d⌋, which drives many counting arguments.
- Programming languages differ on negative division, so check whether yours truncates or floors.
The floor of a real number x, written ⌊x⌋, is the greatest integer that is less than or equal to x. The ceiling of x, written ⌈x⌉, is the least integer that is greater than or equal to x. So ⌊3.7⌋ = 3 and ⌈3.7⌉ = 4, while for a whole number both give the number itself.
These two functions turn continuous quantities into counts. They answer questions such as how many boxes you need, how many multiples of 7 lie below 1,000, or how many digits a huge power has. A handful of identities lets you handle them with confidence.
Definitions that do the work
For an integer n and a real number x, the two functions are pinned down by inequalities:
⌊x⌋ = n exactly when n ≤ x < n + 1
⌈x⌉ = n exactly when n − 1 < x ≤ n Notice which side is strict. These two lines are worth more than any list of identities, because they let you rebuild the rest.
An equivalent form is often the quickest tool. For every integer k:
- k ≤ ⌊x⌋ exactly when k ≤ x
- k ≥ ⌈x⌉ exactly when k ≥ x
The fractional part of x is x − ⌊x⌋, written {x}. It always lies in the interval from 0 up to but not including 1. For x = −2.3, the floor is −3 and the fractional part is 0.7, not −0.3.
| x | ⌊x⌋ | ⌈x⌉ | x − ⌊x⌋ |
|---|---|---|---|
| 4 | 4 | 4 | 0 |
| 4.25 | 4 | 5 | 0.25 |
| −0.5 | −1 | 0 | 0.5 |
| −2.3 | −3 | −2 | 0.7 |
The core identities
In what follows, x and y are real numbers and n is an integer.
⌊x + n⌋ = ⌊x⌋ + n integers slide out
⌈x + n⌉ = ⌈x⌉ + n
⌈−x⌉ = −⌊x⌋ reflection swaps the two
⌈x⌉ − ⌊x⌋ = 0 if x is an integer, otherwise 1
⌊n/2⌋ + ⌈n/2⌉ = n splitting n items into two halves To prove the first one, let m = ⌊x⌋. Then m ≤ x < m + 1. Adding n to all three parts gives m + n ≤ x + n < m + n + 1, which is precisely the statement that ⌊x + n⌋ = m + n.
For sums of two real numbers, you only get a narrow range:
⌊x⌋ + ⌊y⌋ ≤ ⌊x + y⌋ ≤ ⌊x⌋ + ⌊y⌋ + 1 The extra 1 appears exactly when the two fractional parts add to 1 or more. With x = 1.6 and y = 2.7, the floors add to 3, but ⌊4.3⌋ = 4.
Division, remainders and rounding up
For an integer a and a positive integer m, the quotient is q = ⌊a/m⌋ and the remainder is r = a − m·q, with 0 ≤ r < m. This remainder is what mathematicians usually mean by a mod m, and it underlies modular arithmetic.
With a = 17 and m = 5, q = 3 and r = 2. With a = −17, the quotient is ⌊−3.4⌋ = −4 and the remainder is −17 + 20 = 3.
Computers often have fast floored or integer division but no direct ceiling division. The following identity converts one to the other, for an integer n and positive integer m:
⌈n/m⌉ = ⌊(n + m − 1)/m⌋ If 100 people travel in vans holding 8, you need ⌈100/8⌉ = ⌊107/8⌋ = 13 vans. Rounding the ordinary quotient 12.5 down would strand four people.
A second identity lets you divide in stages. For a real x and positive integers m and n:
⌊ ⌊x/m⌋ / n ⌋ = ⌊x/(mn)⌋ To see why, take any integer k. The condition k ≤ ⌊x/m⌋/n means kn ≤ ⌊x/m⌋, which means kn ≤ x/m, which means k ≤ x/(mn). Both sides therefore admit the same integers k, so they are equal.
Counting multiples and digits
The number of positive multiples of d that do not exceed N is ⌊N/d⌋. Below or equal to 1,000 there are ⌊1000/7⌋ = 142 multiples of 7.
For a range of integers from a to b inclusive, subtract the multiples that come before a:
count = ⌊b/d⌋ − ⌊(a − 1)/d⌋
from 50 to 200 with d = 6: ⌊200/6⌋ − ⌊49/6⌋ = 33 − 8 = 25 This is the basic ingredient of inclusion–exclusion problems about divisibility.
Floor also counts digits. A positive integer N has ⌊log₁₀ N⌋ + 1 digits, because N has k digits exactly when 10^(k−1) ≤ N < 10^k. Since log₁₀ 2^100 = 100·log₁₀ 2 ≈ 30.103, the number 2^100 has 31 digits. In binary, the digit count of N is ⌊log₂ N⌋ + 1.
Legendre's formula for factorials
How many times does a prime p divide n!? Each multiple of p contributes one factor, each multiple of p² contributes one more, and so on:
exponent of p in n! = ⌊n/p⌋ + ⌊n/p²⌋ + ⌊n/p³⌋ + ... The sum is finite because the terms become zero once p^k exceeds n. For the exponent of 5 in 100!, you get ⌊100/5⌋ + ⌊100/25⌋ = 20 + 4 = 24. The exponent of 2 is 50 + 25 + 12 + 6 + 3 + 1 = 97.
Each trailing zero needs one 2 and one 5. Fives are scarcer, so 100! ends in exactly 24 zeros.
Hermite's identity and other sums
A pleasing identity links the floors of shifted values to the floor of a multiple:
⌊x⌋ + ⌊x + 1/2⌋ = ⌊2x⌋ Check x = 3.7: the left side is 3 + 4 = 7, and ⌊7.4⌋ = 7. The general version, due to Hermite, says that for a positive integer n the sum of ⌊x + k/n⌋ over k = 0, 1, ..., n − 1 equals ⌊nx⌋.
Here is another sum with a tidy answer:
⌊1/2⌋ + ⌊2/2⌋ + ⌊3/2⌋ + ... + ⌊n/2⌋ = ⌊n²/4⌋ For n = 4 the left side is 0 + 1 + 1 + 2 = 4, and ⌊16/4⌋ = 4. You can prove it in general by pairing consecutive terms and treating even and odd n separately, or by induction in steps of two.
Floor sums also count lattice points, the points with integer coordinates. Below the line y = (q/p)·x, the column at x = k contains ⌊kq/p⌋ points with positive height when p and q share no common factor and 0 < k < p. Counting such points two ways is a classic route to quadratic reciprocity.
Tempting identities that are false
| Claim | Counterexample |
|---|---|
| ⌊2x⌋ = 2⌊x⌋ | x = 0.5 gives 1 on the left and 0 on the right |
| ⌊x + y⌋ = ⌊x⌋ + ⌊y⌋ | x = y = 0.5 gives 1 and 0 |
| ⌊−x⌋ = −⌊x⌋ | x = 1.5 gives −2 and −1 |
| ⌊xy⌋ = ⌊x⌋·⌊y⌋ | x = y = 1.5 gives 2 and 1 |
When you meet a new claim, test a half-integer, a negative non-integer and an exact integer. These three often expose an error quickly. If it survives, prove it by writing x = n + f, where n is an integer and 0 ≤ f < 1.
Some surprising claims are true. For x ≥ 0, ⌊√⌊x⌋⌋ = ⌊√x⌋, because an integer k ≥ 0 satisfies k² ≤ x exactly when k² ≤ ⌊x⌋.
Solving equations that contain a floor
Floor equations look odd at first, but one method handles most of them. Name the floor with an integer variable, translate it into its defining inequality, and find which integers survive.
Suppose you want every real x with ⌊x/2 + 3⌋ = 7. The definition says 7 ≤ x/2 + 3 < 8. Subtract 3 and double each part to get 8 ≤ x < 10. The solution is a half-open interval, not a single number, which is typical because a floor function is constant on stretches.
Now try ⌊x⌋² − 5⌊x⌋ + 6 = 0. Set n = ⌊x⌋. The quadratic factors as (n − 2)(n − 3) = 0, so n is 2 or 3. Translating back gives 2 ≤ x < 4.
A harder one mixes x with its floor, as in x + ⌊x⌋ = 7.5. Write x = n + f, where n = ⌊x⌋ and 0 ≤ f < 1. Then 2n + f = 7.5. Since 2n is an integer, f must be 0.5, which forces 2n = 7 and n = 3.5. That is not an integer, so there is no solution at all.
Change the right side to 8.5 and you get 2n + f = 8.5, with n = 4 and f = 0.5. The single solution is x = 4.5.
The graph helps you see why. The function ⌊x⌋ is a staircase: flat on each interval from n up to n + 1, with a jump of 1 at every integer. Adding x tilts the steps but keeps the jumps, so certain output values are skipped entirely.
Rounding to the nearest value
Ordinary rounding can be written with a floor. The nearest integer to x, with halves rounded upward, is ⌊x + 1/2⌋. So 2.4 becomes ⌊2.9⌋ = 2 and 2.5 becomes ⌊3⌋ = 3. Note that −2.5 becomes −2 under this rule, while some other conventions send it to −3 or to the nearest even integer.
The same idea rounds to any step size s > 0. The largest multiple of s that does not exceed x is s·⌊x/s⌋, and the smallest multiple that is at least x is s·⌈x/s⌉. Pricing in whole quarter hours, a 52-minute call with s = 15 is billed as 15·⌈52/15⌉ = 60 minutes.
Floor and ceiling in code
Software libraries usually offer floor and ceil functions that behave as defined here. Integer division is another matter. In C and Java, dividing −17 by 5 truncates toward zero and gives −3. Python's // operator takes the floor and gives −4. The remainder operators differ to match.
Floating-point rounding adds a second hazard. A quantity that should equal exactly 3 may be stored as a value slightly below it, and its floor then becomes 2. How computers store decimals explains why. When the inputs are integers, prefer integer arithmetic such as (n + m − 1) divided by m for nonnegative n.
You will also meet these functions in algorithm analysis, where a list of n items splits into pieces of size ⌊n/2⌋ and ⌈n/2⌉. The master theorem shows that, in the usual cases, this rounding leaves the growth rate unchanged.