Add int.__length__ method

I can fix that! Ignoring behaviour of zeros and negative values, on my macOS 13.6 / Intel machine I see this:

>>> import math
>>> digit_length1 = lambda n: len(str(n))
>>> digit_length2 = lambda n: 1 + math.floor(math.log10(n))
>>> n = 10**15 - 1
>>> digit_length1(n) == digit_length2(n)
False

Your results may vary: this is using the platform libm at some level, so is system-dependent.

7 Likes