TITLE: Finding the number of digits of a given integer.
QUESTION [5 upvotes]: I know that a way to do this is by using log to the base 10. Or more specifically;
$$n=\lfloor\log_{10}x\rfloor + 1\tag{1}\label{1}$$
Where "$\lfloor{z}\rfloor$", rounds the value of $z$. This works really well, but when it comes to values like $x=9999$, we get $n=5$ when using the standard Eq. 1. This is because $9999\approx10000$ and $\lfloor\log_{10}{10000}\rfloor+1=5$. So is there an formula which can take in any value of $x$ and give the number of digits?

REPLY [4 votes]: $$
\log_{10}{9999}=a\Longleftrightarrow 10^a=9999.
$$
$10^3=1000$ and $10^4=10000$. Therefore, $a$ is a number between $3$ and $4$. Applying the floor function to a number that's strictly less than $4$ and strictly greater than $3$ will give you $3$. Adding $1$ to it will give you $4$. That's your answer. Logically, there is absolutely nothing wrong with this method.
More precisely, $\log_{10}{9999}=3.99995656838019248962...$, which is a number close to $4$. Whatever you're using might be rounding $\log_{10}{9999}$ up to $4$. Then, it computes the floor of $4$, which is $4$, and adds $1$ to it and you get the incorrect answer of $5$. That could be one possible explanation why you get $5$ instead of $4$.

REPLY [4 votes]: It doesn't matter that $9999 \approx 10000$.  The floor function always rounds down no matter how close it gets.
$\log_{10} 9999 = 3.9999565683801924896154439559762.....$ and $\lfloor 3.9999565683801924896154439559762..... \rfloor = 3$.  It does not equal $4$.   That is because even though $\log_{10}9999\approx \log_{10}10000$ it is still less than $4$.  And the floor function NEVER rounds up.  It always rounds down.
So your formula always works.