[Math] how does a floor function work

ceiling-and-floor-functions

I understand what a floor function does, and got a few explanations here, but none of them had a explanation, which is what i'm after.

Can someone explain to me what is going on behind the scenes of a floor function?

Edit: To clarify, what i want to know, is when i use floor(x), what is the computer actually doing to give me the result of the largest integer below x. For example,someone responded in the linked thread,

$$\left\lfloor \frac{x}{2} \right\rfloor = \frac{x}{2} – \frac{1 – (-1)^x}{4} $$

However, there was no explanation. So, what i really am after is a method of solving floor() mathematically, with an explanation/proof

Best Answer

This is better suited for the programming forums but....

Your computer program is probably working with a binary representation of a number. To compute the floor function, the computer does exactly the same thing you do: e.g. if it holds a representation of the positive binary numeral

$$ 100110.01011101 $$

then it simply replaces every digit to the right of the point with a zero:

$$ 100110.00000000 $$

The processor your program runs on likely has assembly language instructions for performing this exact operation when a number is stored in a register in IEEE 724 format (which is almost always used to store floating-point numbers).

Related Question