16
int length = (int) floor( log10 (float) number ) + 1;

My question is essentially a math question: WHY does taking the log10() of a number, flooring that number, adding 1, and then casting it into an int correctly calculate the length of number?

I really want to know the deep mathematical explanation please!

user2864740
  • 60,010
  • 15
  • 145
  • 220
user2926999
  • 393
  • 1
  • 3
  • 11
  • What *is* the [log function](http://en.wikipedia.org/wiki/Logarithm) doing? (Note the relation given at the top of the article, then consider the values of 10^1, 10^2, 10^3, etc.) – user2864740 Jun 12 '14 at 05:07
  • 14
    This question appears to be off-topic because even though it has some code, it's actually a maths question. – Yu Hao Jun 12 '14 at 05:10
  • 1
    @RobertDodier I'm not saying it's a bad question, I'm just saying it's off-topic for SO. Maybe it's just my opinion, but the question is not really about programming even though there's some code. – Yu Hao Jun 12 '14 at 05:21
  • 3
    @YuHao The [basic] use of log/exp should be in a programmer's tool belt .. since it's a very basic question, presented with [pseudo]code in a common use-case, I think it's "OK" here. – user2864740 Jun 12 '14 at 05:27
  • 1
    I asked [this relevant question](http://math.stackexchange.com/questions/3987/how-to-calculate-the-number-of-decimal-digits-for-a-binary-number) - not a duplicate - on Mathematics a few years back. Strictly speaking, using `log10` doesn't work - at least not using floating point. For a large enough number, the inaccuracy in the logarithm will be sufficient that your result can be out by any number of digits you choose. You can't experience this with `int` though - you'd have to be using a big-integer type. –  Jun 12 '14 at 05:54
  • 1
    Oh my god, this IS a programming question - it's related to many programming things like changing an integer into a character array (which was the inspiration of asking the question), I wanted to have a deeper understanding of how the log function works - and other programmers can definitely benefit from this knowledge! Other programmers could be in the same bucket - they might want to have an intuition of calculating length of a number. This is a common computer programming question!!! – user2926999 Jun 12 '14 at 07:18
  • 7
    This question appears to be off-topic because it asks for a mathematical proof of a mathematical formula; there is no programming involved here. – user247702 Jul 22 '14 at 10:29
  • Not a explaination, nor a formal proof, but one could "invent" this formula, by sketching graph of this hypothetical function, and noticing that it is shaped a bit like logarithm graph. If you see that, you can try some transformations and voila – gizlu Oct 12 '22 at 17:34

2 Answers2

22

For an integer number that has n digits, it's value is between 10^(n - 1)(included) and 10^n, and so log10(number) is between n - 1(included) and n. Then the function floor cuts down the fractional part, leaves the result as n - 1. Finally, adding 1 to it gives the number of digits.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
11

Consider that a four-digit number x is somewhere between 1000 <= x < 10000. Taking the log base 10 of all three components gives 3.000 <= log(x, 10) < 4.000. Taking the floor (or int) of each component and adding one gives 4 <= int(log(x, 10))+1 <= 4.

Ignoring round-off error, this gives you the number of digits in x.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485