Given integers xc, xe, yc and ye representing Decimals x = xc*10**xe and y = yc*10**ye, compute x**y. Returns a pair of integers (c, e) such that: 10**(p-1) <= c <= 10**p, and (c-1)*10**e < x**y < (c+1)*10**e in other words, c*10**e is an approximation to x**y with p dig
(xc, xe, yc, ye, p)
| 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 |
| 5976 | y = yc*10**ye, compute x**y. Returns a pair of integers (c, e) such that: |
| 5977 | |
| 5978 | 10**(p-1) <= c <= 10**p, and |
| 5979 | (c-1)*10**e < x**y < (c+1)*10**e |
| 5980 | |
| 5981 | in other words, c*10**e is an approximation to x**y with p digits |
| 5982 | of precision, and with an error in c of at most 1. (This is |
| 5983 | almost, but not quite, the same as the error being < 1ulp: when c |
| 5984 | == 10**(p-1) we can only guarantee error < 10ulp.) |
| 5985 | |
| 5986 | We assume that: x is positive and not equal to 1, and y is nonzero. |
| 5987 | """ |
| 5988 | |
| 5989 | # Find b such that 10**(b-1) <= |y| <= 10**b |
| 5990 | b = len(str(abs(yc))) + ye |
| 5991 | |
| 5992 | # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point |
| 5993 | lxc = _dlog(xc, xe, p+b+1) |
| 5994 | |
| 5995 | # compute product y*log(x) = yc*lxc*10**(-p-b-1+ye) = pc*10**(-p-1) |
| 5996 | shift = ye-b |
| 5997 | if shift >= 0: |
| 5998 | pc = lxc*yc*10**shift |
| 5999 | else: |
| 6000 | pc = _div_nearest(lxc*yc, 10**-shift) |
| 6001 | |
| 6002 | if pc == 0: |
| 6003 | # we prefer a result that isn't exactly 1; this makes it |
| 6004 | # easier to compute a correctly rounded result in __pow__ |
| 6005 | if ((len(str(xc)) + xe >= 1) == (yc > 0)): # if x**y > 1: |
| 6006 | coeff, exp = 10**(p-1)+1, 1-p |
| 6007 | else: |
| 6008 | coeff, exp = 10**p-1, -p |
| 6009 | else: |
| 6010 | coeff, exp = _dexp(pc, -(p+1), p+1) |
| 6011 | coeff = _div_nearest(coeff, 10) |
| 6012 | exp += 1 |
| 6013 | |
| 6014 | return coeff, exp |
| 6015 | |
| 6016 | def _log10_lb(c, correction = { |
| 6017 | '1': 100, '2': 70, '3': 53, '4': 40, '5': 31, |