I'm not a math guy, so I'm kinda confused about this. I have a program that needs to calculate the floor base $2$ number of a float.
Let say a number $4$, that base $2$ floor would be $4$. Other examples :
- $5 \to 4$
- $6 \to 4$
- $8 \to 8$
- $12 \to 8$
- $16 \to 16$
- $0.5 \to 0.5$
- $0.6 \to 0.5$
- $0.13 \to 0.125$
How would I do this and what is the name of this problem?
Let the number whose base 2 floor you want to find be $N$. We want to find the greatest $k\in \mathbb{Z}$ such that $2^k \leq N$. Take the base 2 log of both sides to get $k \leq \log_2{N}$. Since we want the maximum value of $k$ that still fulfills this inequality and that is an integer, we pick $k = \lfloor \log_2{N} \rfloor$. Then you just need to compute $2^k$, which is the actual value of your base 2 floor of $N$.
EDIT: So simply put, your wanted base 2 floor function $f_2$ looks like this:
$$ f_2(N) = 2^{\lfloor \log_2{N} \rfloor} $$
And for a floor in base $b$ (using the above logic), $f_b$ would be
$$ f_b(N) = b^{\lfloor \log_b{N} \rfloor} $$