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
I'm answering a question which I suspect was meant to be asked. Suppose $p$ and $q$ are integers. Floor division $\lfloor p/q\rfloor$ is an operation which is associated with the modulo operator $p\mathbin{\mathrm{mod}}q$, at least when modulo is defined as the remainder in the division algorithm (the Euclidean division theorem). That is, they are related by $$p=q\lfloor p/q\rfloor+(p\mathbin{\mathrm{mod}}q)$$ where $0\leq p\mathbin{\mathrm{mod}}q<q$.
Computationally, an efficient way to compute $\lfloor p/q\rfloor$ is to do something like long division, then ignore the remainder. Because of this, digital computers have an instruction which produces both values simultaneously (if it has a built-in divider at all -- otherwise one can implement long division in software).
However, digital computers tend to use a different division convention, which is to round $p/q$ toward $0$, which explains why modulo on a computer gives different values from expected when $p/q$ is negative.
You can take advantage of the division algorithm to get the identity $\lfloor x/2\rfloor=x/2-(1-(-1)^x)/4$ (when $x$ is an integer, of course). We have $x=2\lfloor x/2\rfloor+(x\mathbin{\mathrm{mod}}2)$ by division, and one can prove $x \mathbin{\mathrm{mod}}2=(1-(-1)^x)/2$ (if $x$ is even, then $(-1)^x=1$ so the expression evaluates to $0$, and if $x$ is odd, then $(-1)^x=-1$, so the expression evaluates to $1$). Then simply solve for $\lfloor x/2\rfloor$.