CURIUM
What is the floor of −2.7: −2 or −3?

Floor function

What is the floor of −2.7: −2 or −3?

−3. The floor of a real number x is the greatest integer less than or equal to x, and −3 is indeed the last integer below −2.7. Removing the decimals gives −1, but that is a different operation called truncation, which matches the floor only for positive numbers. For 2.7, both give 2.

MathsMathematical analysis

The three levels

Beginner

What is the floor of −2.7: −2 or −3?

−3. The floor of a real number x is the greatest integer less than or equal to x, and −3 is indeed the last integer below −2.7. Removing the decimals gives −1, but that is a different operation called truncation, which matches the floor only for positive numbers. For 2.7, both give 2.

Intermediate

Is the floor of a sum the sum of the floors?

Not always. For 0.6 and 0.7, the floors are 0 and 0, but for 1.3 it is 1. The equality can fall short by one unit, never more: ⌊x⌋ + ⌊y⌋ ≤ ⌊x + y⌋ ≤ ⌊x⌋ + ⌊y⌋ + 1. It is exact whenever one of the two numbers is an integer: ⌊x + n⌋ = ⌊x⌋ + n for every integer n.

Expert

How does a sum of floors compute a GCD?

Via the formula called Sylvester's or Polezzi's: for a and b positive integers, the sum of ⌊ka/b⌋ for k ranging from 0 to b − 1 equals ((a − 1)(b − 1) + gcd(a, b) − 1)/2. It inverts to gcd(a, b) = a + b − ab + 2 × (that sum). For a = 4 and b = 6, the sum is 0 + 0 + 1 + 2 + 2 + 3 = 8, and 4 + 6 − 24 + 16 indeed gives 2.