Class to compute, store, and allow retrieval of, digits of the constant log(10) = 2.302585.... This constant is needed by Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__.
| 5829 | return _div_nearest(f_log_ten + log_d, 100) |
| 5830 | |
| 5831 | class _Log10Memoize(object): |
| 5832 | """Class to compute, store, and allow retrieval of, digits of the |
| 5833 | constant log(10) = 2.302585.... This constant is needed by |
| 5834 | Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__.""" |
| 5835 | def __init__(self): |
| 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 |
no outgoing calls
no test coverage detected
searching dependent graphs…