Finding the number of digits of a given integer.

Actually, your formula does work also for $x = 9999$. This is because "$\lfloor z \rfloor$" rounds the value of $z$ down. It's called the floor function (e.g., see Wikipedia's Floor and ceiling functions article), so it basically just removes any fractional part of non-negative numbers. Also, note the floor function is applied to the result of the logarithm, not the value of $x$ itself. In particular, with $x = 9999$, you have $3 \lt \log_{10}x \lt 4$, so $\lfloor \log_{10}(9999) \rfloor + 1 = 3 + 1 = 4$, as expected.


$\lfloor z\rfloor$ does not round. It is the floor function which returns the greatest integer that does not exceed $z$. The formula is correct for all positive $x$.


This works really well, but when it comes to values like $x=9999$, we get $n=5$ when using the standard Eq. 1.

No, we don't, but I imagine that might depend on your calculator and rounding error. Using Wolfram Alpha,

$$\log_{10} 9999 \approx 3.99996...$$

which floors to $3$, plus one gives $4$, as expected. Indeed, even taking $10^{100} - 1$ (a number of $100$ nines) into Wolfram, we see

$$\log_{10}(10^{100} - 1) \approx 99.\underbrace{999 \cdots 999}_{\text{100 nines}}56570551810...$$

for which the formula still gives the expected result.


My assumption for the source of the discrepancy is either:

  • A rounding error in your calculator when trying to calculate the logarithm.
  • A misunderstanding, either by yourself or the calculator, of what $\lfloor x \rfloor$ "means," in the sense $\lfloor x \rfloor$ is the greatest integer such that $\lfloor x \rfloor \le x$.

Tags:

Logarithms