Compute an approximation to exp(c*10**e), with p decimal places of precision. Returns integers d, f such that: 10**(p-1) <= d <= 10**p, and (d-1)*10**f < exp(c*10**e) < (d+1)*10**f In other words, d*10**f is an approximation to exp(c*10**e) with p digits of pr
(c, e, p)
| 5936 | return M+y |
| 5937 | |
| 5938 | def _dexp(c, e, p): |
| 5939 | """Compute an approximation to exp(c*10**e), with p decimal places of |
| 5940 | precision. |
| 5941 | |
| 5942 | Returns integers d, f such that: |
| 5943 | |
| 5944 | 10**(p-1) <= d <= 10**p, and |
| 5945 | (d-1)*10**f < exp(c*10**e) < (d+1)*10**f |
| 5946 | |
| 5947 | In other words, d*10**f is an approximation to exp(c*10**e) with p |
| 5948 | digits of precision, and with an error in d of at most 1. This is |
| 5949 | almost, but not quite, the same as the error being < 1ulp: when d |
| 5950 | = 10**(p-1) the error could be up to 10 ulp.""" |
| 5951 | |
| 5952 | # we'll call iexp with M = 10**(p+2), giving p+3 digits of precision |
| 5953 | p += 2 |
| 5954 | |
| 5955 | # compute log(10) with extra precision = adjusted exponent of c*10**e |
| 5956 | extra = max(0, e + len(str(c)) - 1) |
| 5957 | q = p + extra |
| 5958 | |
| 5959 | # compute quotient c*10**e/(log(10)) = c*10**(e+q)/(log(10)*10**q), |
| 5960 | # rounding down |
| 5961 | shift = e+q |
| 5962 | if shift >= 0: |
| 5963 | cshift = c*10**shift |
| 5964 | else: |
| 5965 | cshift = c//10**-shift |
| 5966 | quot, rem = divmod(cshift, _log10_digits(q)) |
| 5967 | |
| 5968 | # reduce remainder back to original precision |
| 5969 | rem = _div_nearest(rem, 10**extra) |
| 5970 | |
| 5971 | # error in result of _iexp < 120; error after division < 0.62 |
| 5972 | return _div_nearest(_iexp(rem, 10**p), 1000), quot - p + 3 |
| 5973 | |
| 5974 | def _dpower(xc, xe, yc, ye, p): |
| 5975 | """Given integers xc, xe, yc and ye representing Decimals x = xc*10**xe and |