Returns e ** self.
(self, context=None)
| 2999 | self._exp, self._is_special) |
| 3000 | |
| 3001 | def exp(self, context=None): |
| 3002 | """Returns e ** self.""" |
| 3003 | |
| 3004 | if context is None: |
| 3005 | context = getcontext() |
| 3006 | |
| 3007 | # exp(NaN) = NaN |
| 3008 | ans = self._check_nans(context=context) |
| 3009 | if ans: |
| 3010 | return ans |
| 3011 | |
| 3012 | # exp(-Infinity) = 0 |
| 3013 | if self._isinfinity() == -1: |
| 3014 | return _Zero |
| 3015 | |
| 3016 | # exp(0) = 1 |
| 3017 | if not self: |
| 3018 | return _One |
| 3019 | |
| 3020 | # exp(Infinity) = Infinity |
| 3021 | if self._isinfinity() == 1: |
| 3022 | return Decimal(self) |
| 3023 | |
| 3024 | # the result is now guaranteed to be inexact (the true |
| 3025 | # mathematical result is transcendental). There's no need to |
| 3026 | # raise Rounded and Inexact here---they'll always be raised as |
| 3027 | # a result of the call to _fix. |
| 3028 | p = context.prec |
| 3029 | adj = self.adjusted() |
| 3030 | |
| 3031 | # we only need to do any computation for quite a small range |
| 3032 | # of adjusted exponents---for example, -29 <= adj <= 10 for |
| 3033 | # the default context. For smaller exponent the result is |
| 3034 | # indistinguishable from 1 at the given precision, while for |
| 3035 | # larger exponent the result either overflows or underflows. |
| 3036 | if self._sign == 0 and adj > len(str((context.Emax+1)*3)): |
| 3037 | # overflow |
| 3038 | ans = _dec_from_triple(0, '1', context.Emax+1) |
| 3039 | elif self._sign == 1 and adj > len(str((-context.Etiny()+1)*3)): |
| 3040 | # underflow to 0 |
| 3041 | ans = _dec_from_triple(0, '1', context.Etiny()-1) |
| 3042 | elif self._sign == 0 and adj < -p: |
| 3043 | # p+1 digits; final round will raise correct flags |
| 3044 | ans = _dec_from_triple(0, '1' + '0'*(p-1) + '1', -p) |
| 3045 | elif self._sign == 1 and adj < -p-1: |
| 3046 | # p+1 digits; final round will raise correct flags |
| 3047 | ans = _dec_from_triple(0, '9'*(p+1), -p-1) |
| 3048 | # general case |
| 3049 | else: |
| 3050 | op = _WorkRep(self) |
| 3051 | c, e = op.int, op.exp |
| 3052 | if op.sign == 1: |
| 3053 | c = -c |
| 3054 | |
| 3055 | # compute correctly rounded result: increase precision by |
| 3056 | # 3 digits at a time until we get an unambiguously |
| 3057 | # roundable result |
| 3058 | extra = 3 |