MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / _dlog

Function _dlog

tools/python-3.11.9-amd64/Lib/_pydecimal.py:5820–5862  ·  view source on GitHub ↗

Given integers c, e and p with c > 0, compute an integer approximation to 10**p * log(c*10**e), with an absolute error of at most 1. Assumes that c*10**e is not exactly 1.

(c, e, p)

Source from the content-addressed store, hash-verified

5818 return _div_nearest(log_tenpower+log_d, 100)
5819
5820def _dlog(c, e, p):
5821 """Given integers c, e and p with c > 0, compute an integer
5822 approximation to 10**p * log(c*10**e), with an absolute error of
5823 at most 1. Assumes that c*10**e is not exactly 1."""
5824
5825 # Increase precision by 2. The precision increase is compensated
5826 # for at the end with a division by 100.
5827 p += 2
5828
5829 # rewrite c*10**e as d*10**f with either f >= 0 and 1 <= d <= 10,
5830 # or f <= 0 and 0.1 <= d <= 1. Then we can compute 10**p * log(c*10**e)
5831 # as 10**p * log(d) + 10**p*f * log(10).
5832 l = len(str(c))
5833 f = e+l - (e+l >= 1)
5834
5835 # compute approximation to 10**p*log(d), with error < 27
5836 if p > 0:
5837 k = e+p-f
5838 if k >= 0:
5839 c *= 10**k
5840 else:
5841 c = _div_nearest(c, 10**-k) # error of <= 0.5 in c
5842
5843 # _ilog magnifies existing error in c by a factor of at most 10
5844 log_d = _ilog(c, 10**p) # error < 5 + 22 = 27
5845 else:
5846 # p <= 0: just approximate the whole thing by 0; error < 2.31
5847 log_d = 0
5848
5849 # compute approximation to f*10**p*log(10), with error < 11.
5850 if f:
5851 extra = len(str(abs(f)))-1
5852 if p + extra >= 0:
5853 # error in f * _log10_digits(p+extra) < |f| * 1 = |f|
5854 # after division, error < |f|/10**extra + 0.5 < 10 + 0.5 < 11
5855 f_log_ten = _div_nearest(f*_log10_digits(p+extra), 10**extra)
5856 else:
5857 f_log_ten = 0
5858 else:
5859 f_log_ten = 0
5860
5861 # error in sum < 11+27 = 38; error after division < 0.38 + 0.5 < 1
5862 return _div_nearest(f_log_ten + log_d, 100)
5863
5864class _Log10Memoize(object):
5865 """Class to compute, store, and allow retrieval of, digits of the

Callers 2

lnMethod · 0.85
_dpowerFunction · 0.85

Calls 4

strFunction · 0.85
_div_nearestFunction · 0.85
_ilogFunction · 0.85
absFunction · 0.70

Tested by

no test coverage detected