Understanding the Floor Function
The floor function, denoted ⌊x⌋ or floor(x), maps a real number x to the greatest integer less than or equal to x. In other words, it rounds down to the nearest integer toward negative infinity. For positive numbers, this means dropping the decimal part. For negative numbers, the behavior is more subtle: ⌊-3.2⌋ = -4, not -3, because -4 is the greatest integer that is less than or equal to -3.2.
The floor function is fundamental in mathematics and computer science. It appears in number theory, combinatorics, algorithm design, and digital signal processing.
Rounding Functions Compared
Floor Function ⌊x⌋
Greatest integer less than or equal to x. Rounds toward negative infinity.
Ceiling Function ⌈x⌉
Smallest integer greater than or equal to x. Rounds toward positive infinity.
Round Function
Rounds to the nearest integer. Ties (0.5) typically round to even ("banker's rounding") or up.
Truncate Function
Removes the fractional part. Rounds toward zero.
Fractional Part
The difference between x and its floor: {x} = x - ⌊x⌋. Always in [0, 1).
Floor-Ceiling Identity
For non-integer x: ⌈x⌉ = ⌊x⌋ + 1. For integer x: both are equal to x.
Key Properties of the Floor Function
- Idempotent: ⌊⌊x⌋⌋ = ⌊x⌋ (applying floor twice gives the same result).
- Integer shift: ⌊x + n⌋ = ⌊x⌋ + n for any integer n.
- Monotone: If x ≤ y, then ⌊x⌋ ≤ ⌊y⌋.
- Bounds: ⌊x⌋ ≤ x < ⌊x⌋ + 1 for all real x.
- Negation: ⌊-x⌋ = -⌈x⌉ for all real x.
- Hermite's identity: ⌊nx⌋ = sum of ⌊x + k/n⌋ for k = 0 to n-1.
Applications of the Floor Function
- Computing array indices from continuous coordinates in computer graphics.
- Implementing integer division in programming languages.
- Counting multiples of a number in a range (e.g., ⌊n/p⌋ multiples of p up to n).
- Generating discrete distributions in probability and statistics.
- Digital signal processing: sample rate conversion and quantization.
Floor Function in Programming
Most programming languages provide a floor function: Math.floor() in JavaScript, math.floor() in Python, Math.Floor() in C#, and floor() in C/C++. Be aware that integer division in C/C++ and Java truncates toward zero, which differs from floor division for negative numbers.