MCPcopy Index your code
hub / github.com/python/cpython / getdigits

Method getdigits

Lib/_pydecimal.py:5838–5864  ·  view source on GitHub ↗

Given an integer p >= 0, return floor(10**p)*log(10). For example, self.getdigits(3) returns 2302.

(self, p)

Source from the content-addressed store, hash-verified

5836 self.digits = "23025850929940456840179914546843642076011014886"
5837
5838 def getdigits(self, p):
5839 """Given an integer p >= 0, return floor(10**p)*log(10).
5840
5841 For example, self.getdigits(3) returns 2302.
5842 """
5843 # digits are stored as a string, for quick conversion to
5844 # integer in the case that we've already computed enough
5845 # digits; the stored digits should always be correct
5846 # (truncated, not rounded to nearest).
5847 if p < 0:
5848 raise ValueError("p should be nonnegative")
5849
5850 if p >= len(self.digits):
5851 # compute p+3, p+6, p+9, ... digits; continue until at
5852 # least one of the extra digits is nonzero
5853 extra = 3
5854 while True:
5855 # compute p+extra digits, correct to within 1ulp
5856 M = 10**(p+extra+2)
5857 digits = str(_div_nearest(_ilog(10*M, M), 100))
5858 if digits[-extra:] != '0'*extra:
5859 break
5860 extra += 3
5861 # keep all reliable digits so far; remove trailing zeros
5862 # and next nonzero digit
5863 self.digits = digits.rstrip('0')[:-1]
5864 return int(self.digits[:p+1])
5865
5866_log10_digits = _Log10Memoize().getdigits
5867

Callers

nothing calls this directly

Calls 4

strFunction · 0.85
_div_nearestFunction · 0.85
_ilogFunction · 0.85
rstripMethod · 0.45

Tested by

no test coverage detected