[Python-Dev] Tail recursion

Tim Peters tim.one at comcast.net
Thu Nov 27 12:21:02 EST 2003


[Devin]
> import math
>
> def numdigits(n):
>     assert (n >= 0) and ((n % 1) == 0)
>     if n < 10:
>         return 1
>     return int(math.log10(n)) + 1
>
> (not iterative, but it'll do :)

Nope, integers in Python are unbounded, and this will deliver wrong answers
for "big enough" integers.  Depending on the vagaries of your platform C's
log10 implementation, it may even deliver a wrong answer for small n near an
exact power of 10.




More information about the Python-Dev mailing list