[Python-checkins] r57322 - in python/branches/decimal-branch/Lib: decimal.py test/decimaltestdata/extra.decTest test/test_decimal.py

facundo.batista python-checkins at python.org
Thu Aug 23 17:40:16 CEST 2007


Author: facundo.batista
Date: Thu Aug 23 17:40:15 2007
New Revision: 57322

Modified:
   python/branches/decimal-branch/Lib/decimal.py
   python/branches/decimal-branch/Lib/test/decimaltestdata/extra.decTest
   python/branches/decimal-branch/Lib/test/test_decimal.py
Log:

Removed the checkMath function because it was to comply to the Cowlishaw
implementation, and not to the Specification. Also now we avoid some
tests from Cowlishaw set when he's testing his implementation, and not
functionality or behaviour defined in the Spec.

Also coded the __pow__() function, and now it comply with all the tests,
even with some added ones in the extra file.

Thanks Mark Dickinson.


Modified: python/branches/decimal-branch/Lib/decimal.py
==============================================================================
--- python/branches/decimal-branch/Lib/decimal.py	(original)
+++ python/branches/decimal-branch/Lib/decimal.py	Thu Aug 23 17:40:15 2007
@@ -1851,153 +1851,492 @@
         context._set_rounding_decision(rounding_decision)
         return product.__add__(third, context)
 
-    def __pow__(self, n, modulo=None, context=None):
-        """Return self ** n (mod modulo)
+    def _power_modulo(self, other, modulo, context=None):
+        """Three argument version of __pow__"""
 
-        If modulo is None (default), don't take it mod modulo.
-        """
-        n = _convert_other(n)
-        if n is NotImplemented:
-            return n
+        # if can't convert other and modulo to Decimal, raise
+        # TypeError; there's no point returning NotImplemented (no
+        # equivalent of __rpow__ for three argument pow)
+        other = _convert_other(other)
+        if other is NotImplemented:
+            raise TypeError("The second argument to pow should be " +
+                            "an integer or an instance of Decimal.  Got " +
+                            str(other))
+
+        modulo = _convert_other(modulo)
+        if modulo is NotImplemented:
+            raise TypeError("The third argument to pow should be " +
+                            "an integer or an instance of Decimal.  Got " +
+                            str(modulo))
 
         if context is None:
             context = getcontext()
-        if n._exp == 0 and n._sign == 0 and len(n._int) > context.prec:
-            return context._raise_error(InvalidContext)
 
-#        # FIXME: this block code was against the new handling of Infinities
-#        # I think we could remove them safely, do not know what the "n.adjusted()>8"
-#        # actually means, so I won't be sure until all the tests passes ok.
-#        #         . Facundo
-#        if self._is_special or n._is_special:# or n.adjusted() > 8:
-#            # Because the spot << doesn't work with really big exponents
-#            if n._isinfinity() or n.adjusted() > 8:
-#                return context._raise_error(InvalidOperation, 'x ** INF')
+        # deal with NaNs: if there are any sNaNs then first one wins,
+        # (i.e. behaviour for NaNs is identical to that of fma)
+        self_is_nan = self._isnan()
+        other_is_nan = other._isnan()
+        modulo_is_nan = modulo._isnan()
+        if self_is_nan or other_is_nan or modulo_is_nan:
+            if self_is_nan == 2:
+                return context._raise_error(InvalidOperation, 'sNaN',
+                                        1, self)
+            if other_is_nan == 2:
+                return context._raise_error(InvalidOperation, 'sNaN',
+                                        1, other)
+            if modulo_is_nan == 2:
+                return context._raise_error(InvalidOperation, 'sNaN',
+                                        1, modulo)
+            if self_is_nan:
+                return self
+            if other_is_nan:
+                return other
+            return modulo
+
+        # check inputs: we apply same restrictions as Python's pow()
+        if not (self._isinteger() and
+                other._isinteger() and
+                modulo._isinteger()):
+            return context._raise_error(InvalidOperation,
+                                        'pow() 3rd argument not allowed '
+                                        'unless all arguments are integers')
+        if other < 0:
+            return context._raise_error(InvalidOperation,
+                                        'pow() 2nd argument cannot be '
+                                        'negative when 3rd argument specified')
+        if not modulo:
+            return context._raise_error(InvalidOperation,
+                                        'pow() 3rd argument cannot be 0')
+
+        # additional restriction for decimal: the modulus must be less
+        # than 10**prec in absolute value
+        if modulo.adjusted() >= context.prec:
+            return context._raise_error(InvalidOperation,
+                                        'insufficient precision: pow() 3rd '
+                                        'argument must not have more than '
+                                        'precision digits')
+
+        # define 0**0 == NaN, for consistency with two-argument pow
+        # (even though it hurts!)
+        if not other and not self:
+            return context._raise_error(InvalidOperation,
+                                        'at least one of pow() 1st argument '
+                                        'and 2nd argument must be nonzero ;'
+                                        '0**0 is not defined')
+
+        # compute sign of result
+        if other._iseven():
+            sign = 0
+        else:
+            sign = self._sign
+
+        # convert modulo to a Python integer, and self and other to
+        # Decimal integers (i.e. force their exponents to be >= 0)
+        modulo = abs(int(modulo))
+        base = _WorkRep(self.to_integral_value())
+        exponent = _WorkRep(other.to_integral_value())
+
+        # compute result using integer pow()
+        base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo
+        for i in xrange(exponent.exp):
+            base = pow(base, 10, modulo)
+        base = pow(base, exponent.int, modulo)
+
+        return Decimal((sign, map(int, str(base)), 0))
+
+    def _power_exact(self, other, p):
+        """Attempt to compute self**other exactly.
+
+        Given Decimals self and other and an integer p, attempt to
+        compute an exact result for the power self**other, with p
+        digits of precision.  Return None if self**other is not
+        exactly representable in p digits.
+
+        Assumes that elimination of special cases has already been
+        performed: self and other must both be nonspecial; self must
+        be positive and not numerically equal to 1; other must be
+        nonzero.  For efficiency, other._exp should not be too large,
+        so that 10**abs(other._exp) is a feasible calculation."""
+
+        # In the comments below, we write x for the value of self and
+        # y for the value of other.  Write x = xc*10**xe and y =
+        # yc*10**ye.
+
+        # The main purpose of this method is to identify the *failure*
+        # of x**y to be exactly representable with as little effort as
+        # possible.  So we look for cheap and easy tests that
+        # eliminate the possibility of x**y being exact.  Only if all
+        # these tests are passed do we go on to actually compute x**y.
+
+        # Here's the main idea.  First normalize both x and y.  We
+        # express y as a rational m/n, with m and n relatively prime
+        # and n>0.  Then for x**y to be exactly representable (at
+        # *any* precision), xc must be the nth power of a positive
+        # integer and xe must be divisible by n.  If m is negative
+        # then additionally xc must be a power of either 2 or 5, hence
+        # a power of 2**n or 5**n.
+        #
+        # There's a limit to how small |y| can be: if y=m/n as above
+        # then:
+        #
+        #  (1) if xc != 1 then for the result to be representable we
+        #      need xc**(1/n) >= 2, and hence also xc**|y| >= 2.  So
+        #      if |y| <= 1/nbits(xc) then xc < 2**nbits(xc) <=
+        #      2**(1/|y|), hence xc**|y| < 2 and the result is not
+        #      representable.
+        #
+        #  (2) if xe != 0, |xe|*(1/n) >= 1, so |xe|*|y| >= 1.  Hence if
+        #      |y| < 1/|xe| then the result is not representable.
+        #
+        # Note that since x is not equal to 1, at least one of (1) and
+        # (2) must apply.  Now |y| < 1/nbits(xc) iff |yc|*nbits(xc) <
+        # 10**-ye iff len(str(|yc|*nbits(xc)) <= -ye.
+        #
+        # There's also a limit to how large y can be, at least if it's
+        # positive: the normalized result will have coefficient xc**y,
+        # so if it's representable then xc**y < 10**p, and y <
+        # p/log10(xc).  Hence if y*log10(xc) >= p then the result is
+        # not exactly representable.
+
+        # if len(str(abs(yc*xe)) <= -ye then abs(yc*xe) < 10**-ye,
+        # so |y| < 1/xe and the result is not representable.
+        # Similarly, len(str(abs(yc)*xc_bits)) <= -ye implies |y|
+        # < 1/nbits(xc).
+
+        x = _WorkRep(self)
+        xc, xe = x.int, x.exp
+        while xc % 10 == 0:
+            xc //= 10
+            xe += 1
+
+        y = _WorkRep(other)
+        yc, ye = y.int, y.exp
+        while yc % 10 == 0:
+            yc //= 10
+            ye += 1
+
+        # case where xc == 1: result is 10**(xe*y), with xe*y
+        # required to be an integer
+        if xc == 1:
+            if ye >= 0:
+                exponent = xe*yc*10**ye
+            else:
+                exponent, remainder = divmod(xe*yc, 10**-ye)
+                if remainder:
+                    return None
+            if y.sign == 1:
+                exponent = -exponent
+            # if other is a nonnegative integer, use ideal exponent
+            if other._isinteger() and other._sign == 0:
+                ideal_exponent = self._exp*int(other)
+                zeros = min(exponent-ideal_exponent, p-1)
+            else:
+                zeros = 0
+            return Decimal((0, (1,) + (0,)*zeros, exponent-zeros))
+
+        # case where y is negative: xc must be either a power
+        # of 2 or a power of 5.
+        if y.sign == 1:
+            last_digit = xc % 10
+            if last_digit in (2,4,6,8):
+                # quick test for power of 2
+                if xc & -xc != xc:
+                    return None
+                # now xc is a power of 2; e is its exponent
+                e = _nbits(xc)-1
+                # find e*y and xe*y; both must be integers
+                if ye >= 0:
+                    y_as_int = yc*10**ye
+                    e = e*y_as_int
+                    xe = xe*y_as_int
+                else:
+                    ten_pow = 10**-ye
+                    e, remainder = divmod(e*yc, ten_pow)
+                    if remainder:
+                        return None
+                    xe, remainder = divmod(xe*yc, ten_pow)
+                    if remainder:
+                        return None
+
+                if e*65 >= p*93: # 93/65 > log(10)/log(5)
+                    return None
+                xc = 5**e
+
+            elif last_digit == 5:
+                # e >= log_5(xc) if xc is a power of 5; we have
+                # equality all the way up to xc=5**2658
+                e = _nbits(xc)*28//65
+                xc, remainder = divmod(5**e, xc)
+                if remainder:
+                    return None
+                while xc % 5 == 0:
+                    xc //= 5
+                    e -= 1
+                if ye >= 0:
+                    y_as_integer = yc*10**ye
+                    e = e*y_as_integer
+                    xe = xe*y_as_integer
+                else:
+                    ten_pow = 10**-ye
+                    e, remainder = divmod(e*yc, ten_pow)
+                    if remainder:
+                        return None
+                    xe, remainder = divmod(xe*yc, ten_pow)
+                    if remainder:
+                        return None
+                if e*3 >= p*10: # 10/3 > log(10)/log(2)
+                    return None
+                xc = 2**e
+            else:
+                return None
+
+            if xc >= 10**p:
+                return None
+            xe = -e-xe
+            return Decimal((0, map(int, str(xc)), xe))
+
+        # now y is positive; find m and n such that y = m/n
+        if ye >= 0:
+            m, n = yc*10**ye, 1
+        else:
+            if xe != 0 and len(str(abs(yc*xe))) <= -ye:
+                return None
+            xc_bits = _nbits(xc)
+            if xc != 1 and len(str(abs(yc)*xc_bits)) <= -ye:
+                return None
+            m, n = yc, 10**(-ye)
+            while m % 2 == n % 2 == 0:
+                m //= 2
+                n //= 2
+            while m % 5 == n % 5 == 0:
+                m //= 5
+                n //= 5
+
+        # compute nth root of xc*10**xe
+        if n > 1:
+            # if 1 < xc < 2**n then xc isn't an nth power
+            if xc != 1 and xc_bits <= n:
+                return None
+
+            xe, rem = divmod(xe, n)
+            if rem != 0:
+                return None
+
+            # compute nth root of xc using Newton's method
+            a = 1 << -(-_nbits(xc)//n) # initial estimate
+            while True:
+                q, r = divmod(xc, a**(n-1))
+                if a <= q:
+                    break
+                else:
+                    a = (a*(n-1) + q)//n
+            if not (a == q and r == 0):
+                return None
+            xc = a
+
+        # now xc*10**xe is the nth root of the original xc*10**xe
+        # compute mth power of xc*10**xe
+
+        # if m > p*100//_log10_lb(xc) then m > p/log10(xc), hence xc**m >
+        # 10**p and the result is not representable.
+        if xc > 1 and m > p*100//_log10_lb(xc):
+            return None
+        xc = xc**m
+        xe *= m
+        if xc > 10**p:
+            return None
+
+        # by this point the result *is* exactly representable
+        # adjust the exponent to get as close as possible to the ideal
+        # exponent, if necessary
+        str_xc = str(xc)
+        if other._isinteger() and other._sign == 0:
+            ideal_exponent = self._exp*int(other)
+            zeros = min(xe-ideal_exponent, p-len(str_xc))
+        else:
+            zeros = 0
+        return Decimal((0, map(int, str_xc)+[0,]*zeros, xe-zeros))
+
+    def __pow__(self, other, modulo=None, context=None):
+        """Return self ** other [ % modulo].
+
+        With two arguments, compute self**other.
+
+        With three arguments, compute (self**other) % modulo.  For the
+        three argument form, the following restrictions on the
+        arguments hold:
+
+         - all three arguments must be integral
+         - other must be nonnegative
+         - either self or other (or both) must be nonzero
+         - modulo must be nonzero and must have at most p digits,
+           where p is the context precision.
+
+        If any of these restrictions is violated the InvalidOperation
+        flag is raised.
+
+        The result of pow(self, other, modulo) is identical to the
+        result that would be obtained by computing (self**other) %
+        modulo with unbounded precision, but is computed more
+        efficiently.  It is always exact.
+        """
+
+        if modulo is not None:
+            return self._power_modulo(other, modulo, context)
+
+        other = _convert_other(other)
+        if other is NotImplemented:
+            return other
+
+        if context is None:
+            context = getcontext()
 
-        ans = self._check_nans(n, context)
+        # either argument is a NaN => result is NaN
+        ans = self._check_nans(other, context)
         if ans:
             return ans
 
-        if not n:
-            if self:
-                return Decimal(1)  # something ** 0
-            else:
+        # 0**0 = NaN (!), x**0 = 1 for nonzero x (including +/-Infinity)
+        if not other:
+            if not self:
                 return context._raise_error(InvalidOperation, '0 ** 0')
+            else:
+                return Decimal((0, (1,), 0))
+
+        # result has sign 1 iff self._sign is 1 and other is an odd integer
+        result_sign = 0
+        if self._sign == 1:
+            if other._isinteger():
+                if not other._iseven():
+                    result_sign = 1
+            else:
+                # -ve**noninteger = NaN
+                # (-0)**noninteger = 0**noninteger
+                if self:
+                    return context._raise_error(InvalidOperation,
+                        'x ** y with x negative and y not an integer')
+            # negate self, without doing any unwanted rounding
+            self = Decimal((0, self._int, self._exp))
 
+        # 0**(+ve or Inf)= 0; 0**(-ve or -Inf) = Infinity
         if not self:
-            if n._sign == 0:
-                zero = Decimal(0)
-                if n._iseven():
-                    return zero
-                zero._sign = self._sign
-                return zero
-            # n is negative
-            if self._sign == 0:
-                return Inf
-            # also self is negative
-            if n._iseven():
-                return Inf
+            if other._sign == 0:
+                return Decimal((result_sign, (0,), 0))
+            else:
+                return Infsign[result_sign]
+
+        # Inf**(+ve or Inf) = Inf; Inf**(-ve or -Inf) = 0
+        if self._isinfinity():
+            if other._sign == 0:
+                return Infsign[result_sign]
             else:
-                return negInf
+                return Decimal((result_sign, (0,), 0))
 
-        self_inf = self._isinfinity()
-        n_inf = n._isinfinity()
+        # 1**other = 1, but the choice of exponent and the flags
+        # depend on the exponent of self, and on whether other is a
+        # positive integer, a negative integer, or neither
         if self == Decimal(1):
-            if n_inf:
+            if other._isinteger():
+                # exp = max(self._exp*max(int(other), 0),
+                # 1-context.prec) but evaluating int(other) directly
+                # is dangerous until we know other is small (other
+                # could be 1e999999999)
+                if other._sign == 1:
+                    multiplier = 0
+                elif other > Decimal(context.prec):
+                    multiplier = context.prec
+                else:
+                    multiplier = int(other)
+
+                exp = self._exp * multiplier
+                if exp < 1-context.prec:
+                    exp = 1-context.prec
+                    context._raise_error(Rounded)
+            else:
                 context._raise_error(Inexact)
                 context._raise_error(Rounded)
-                digits = (1,)+(0,)*(context.prec-1)
-                return Decimal((0, digits, -context.prec+1))
-            return Decimal(1)
+                exp = 1-context.prec
 
-        if self_inf == -1 and n_inf:
-            return context._raise_error(InvalidOperation, '-Inf ** +-Inf')
+            return Decimal((result_sign, (1,)+(0,)*-exp, exp))
 
-        if self_inf:
-            if modulo:
-                return context._raise_error(InvalidOperation, 'INF % x')
-            if not n:
-                return Decimal(1)
-        if self_inf == 1:
-            if n._sign == 1:
-                return Decimal(0)
-            return Inf
-        if self_inf == -1:
-            if abs(n) < 1:
-                return context._raise_error(InvalidOperation, '-INF ** -1<n<1')
-            if n._sign == 0:
-                if n._iseven():
-                    return Inf
-                else:
-                    return negInf
-            if n._iseven():
-                return Decimal(0)
-            else:
-                return Decimal((1, (0,), 0))
+        # compute adjusted exponent of self
+        self_adj = self.adjusted()
 
-        if n_inf and self._sign == 1:
-            return context._raise_error(InvalidOperation, '-num ** Inf')
-        if n_inf == 1:
-            if self < 1:
-                return Decimal(0)
-            else:
-                return Inf
-        if n_inf == -1:
-            if self > 1:
-                return Decimal(0)
+        # self ** infinity is infinity if self > 1, 0 if self < 1
+        # self ** -infinity is infinity if self < 1, 0 if self > 1
+        if other._isinfinity():
+            if (other._sign == 0) == (self_adj < 0):
+                return Decimal((result_sign, (0,), 0))
             else:
-                return Inf
+                return Infsign[result_sign]
 
-        sign = self._sign and not n._iseven()
-        n = int(n)
-        # With ludicrously large exponent, just raise an overflow
-        # and return inf.
-        if not modulo and n > 0 and \
-           (self._exp + len(self._int) - 1) * n > context.Emax and self:
-
-            tmp = Decimal('inf')
-            tmp._sign = sign
-            context._raise_error(Rounded)
-            context._raise_error(Inexact)
-            context._raise_error(Overflow, 'Big power', sign)
-            return tmp
+        # from here on, the result always goes through the call
+        # to _fix at the end of this function.
+        ans = None
+
+        # crude test to catch cases of extreme overflow/underflow.  If
+        # log10(self)*other >= 10**bound and bound >= len(str(Emax))
+        # then 10**bound >= 10**len(str(Emax)) >= Emax+1 and hence
+        # self**other >= 10**(Emax+1), so overflow occurs.  The test
+        # for underflow is similar.
+        bound = self._log10_exp_bound() + other.adjusted()
+        if (self_adj >= 0) == (other._sign == 0):
+            # self > 1 and other +ve, or self < 1 and other -ve
+            # possibility of overflow
+            if bound >= len(str(context.Emax)):
+                ans = Decimal((result_sign, (1,), context.Emax+1))
+        else:
+            # self > 1 and other -ve, or self < 1 and other +ve
+            # possibility of underflow to 0
+            Etiny = context.Etiny()
+            if bound >= len(str(-Etiny)):
+                ans = Decimal((result_sign, (1,), Etiny-1))
+
+        # try for an exact result with precision +1
+        if ans is None:
+            ans = self._power_exact(other, context.prec + 1)
+            if ans is not None and result_sign == 1:
+                ans = Decimal((1, ans._int, ans._exp))
 
-        elength = len(str(abs(n)))
-        firstprec = context.prec
+        # usual case: inexact result, x**y computed directly as exp(y*log(x))
+        if ans is None:
+            p = context.prec
+            x = _WorkRep(self)
+            xc, xe = x.int, x.exp
+            y = _WorkRep(other)
+            yc, ye = y.int, y.exp
+            if y.sign == 1:
+                yc = -yc
 
-        if not modulo and firstprec + elength + 1 > DefaultContext.Emax:
-            return context._raise_error(Overflow, 'Too much precision.', sign)
+            # compute correctly rounded result:  start with precision +3,
+            # then increase precision until result is unambiguously roundable
+            extra = 3
+            while True:
+                coeff, exp = _dpower(xc, xe, yc, ye, p+extra)
+                if coeff % (5*10**(len(str(coeff))-p-1)):
+                    break
+                extra += 3
 
-        mul = Decimal(self)
-        val = Decimal(1)
-        context = context._shallow_copy()
-        context.prec = firstprec + elength + 1
-        if n < 0:
-            # n is a long now, not Decimal instance
-            n = -n
-            mul = Decimal(1).__div__(mul, context=context)
-
-        spot = 1
-        while spot <= n:
-            spot <<= 1
-
-        spot >>= 1
-        # spot is the highest power of 2 less than n
-        while spot:
-            val = val.__mul__(val, context=context)
-            if val._isinfinity():
-                val = Infsign[sign]
-                break
-            if spot & n:
-                val = val.__mul__(mul, context=context)
-            if modulo is not None:
-                val = val.__mod__(modulo, context=context)
-            spot >>= 1
-        context.prec = firstprec
+            ans = Decimal((result_sign, map(int, str(coeff)), exp))
 
-        if context._rounding_decision == ALWAYS_ROUND:
-            return val._fix(context)
-        return val
+        # the specification says that for non-integer other we need to
+        # raise Inexact, even when the result is actually exact.  In
+        # the same way, we need to raise Underflow here if the result
+        # is subnormal.  (The call to _fix will take care of raising
+        # Rounded and Subnormal, as usual.)
+        if not other._isinteger():
+            context._raise_error(Inexact)
+            # pad with zeros up to length context.prec+1 if necessary
+            if len(ans._int) <= context.prec:
+                expdiff = context.prec+1 - len(ans._int)
+                ans = Decimal((ans._sign, ans._int+(0,)*expdiff, ans._exp-expdiff))
+            if ans.adjusted() < context.Emin:
+                context._raise_error(Underflow)
+
+        # unlike exp, ln and log10, the power function respects the
+        # rounding mode; no need to use ROUND_HALF_EVEN here
+        ans = ans._fix(context)
+        return ans
 
     def __rpow__(self, other, context=None):
         """Swaps self/other and returns __pow__."""
@@ -2351,6 +2690,8 @@
 
     def _isinteger(self):
         """Returns whether self is an integer"""
+        if self._is_special:
+            return False
         if self._exp >= 0:
             return True
         rest = self._int[self._exp:]
@@ -2358,7 +2699,7 @@
 
     def _iseven(self):
         """Returns 1 if self is even.  Assumes self is an integer."""
-        if self._exp > 0:
+        if not self or self._exp > 0:
             return 1
         return self._int[-1+self._exp] & 1 == 0
 
@@ -2477,39 +2818,12 @@
         """Returns self with the sign of other."""
         return Decimal((other._sign, self._int, self._exp))
 
-    def _checkMath(self, context):
-        """Imitation of decNumber's decCheckMath.
-
-        This is to adjust all the possible to original
-        behaviour in order to pass some tests.
-        """
-
-        DEC_MAX_MATH = 999999
-        if (context.prec > DEC_MAX_MATH or
-            context.Emax > DEC_MAX_MATH or
-            -context.Emin > DEC_MAX_MATH):
-            return context._raise_error(InvalidContext)
-
-        if self._is_special:
-            return
-
-        if (len(self._int) > DEC_MAX_MATH or
-            self._exp + len(self._int) > DEC_MAX_MATH+1 or
-            self._exp + len(self._int) < 2*(1-DEC_MAX_MATH)) and self:
-            return context._raise_error(InvalidOperation,
-                        "operand outside bounds for mathematical functions")
-
     def exp(self, context=None):
         """Returns e ** self."""
 
         if context is None:
             context = getcontext()
 
-        # check context and operand
-        ans = self._checkMath(context)
-        if ans:
-            return ans
-
         # exp(NaN) = NaN
         ans = self._check_nans(context=context)
         if ans:
@@ -2676,11 +2990,6 @@
         if context is None:
             context = getcontext()
 
-        # check context and operand
-        ans = self._checkMath(context)
-        if ans:
-            return ans
-
         # ln(NaN) = NaN
         ans = self._check_nans(context=context)
         if ans:
@@ -2761,11 +3070,6 @@
         if context is None:
             context = getcontext()
 
-        # check context and operand
-        ans = self._checkMath(context)
-        if ans:
-            return ans
-
         # log10(NaN) = NaN
         ans = self._check_nans(context=context)
         if ans:
@@ -4151,49 +4455,69 @@
     def power(self, a, b, modulo=None):
         """Raises a to the power of b, to modulo if given.
 
-        The right-hand operand must be a whole number whose integer part (after
-        any exponent has been applied) has no more than 9 digits and whose
-        fractional part (if any) is all zeros before any rounding.  The operand
-        may be positive, negative, or zero; if negative, the absolute value of
-        the power is used, and the left-hand operand is inverted (divided into
-        1) before use.
-
-        If the increased precision needed for the intermediate calculations
-        exceeds the capabilities of the implementation then an Invalid
-        operation condition is raised.
-
-        If, when raising to a negative power, an underflow occurs during the
-        division into 1, the operation is not halted at that point but
-        continues.
+        With two arguments, compute a**b.  If a is negative then b
+        must be integral.  The result will be inexact unless b is
+        integral and the result is finite and can be expressed exactly
+        in 'precision' digits.
+
+        With three arguments, compute (a**b) % modulo.  For the
+        three argument form, the following restrictions on the
+        arguments hold:
+
+         - all three arguments must be integral
+         - b must be nonnegative
+         - at least one of a or b must be nonzero
+         - modulo must be nonzero and have at most 'precision' digits
+
+        The result of pow(a, b, modulo) is identical to the result
+        that would be obtained by computing (a**b) % modulo with
+        unbounded precision, but is computed more efficiently.  It is
+        always exact.
 
-        >>> ExtendedContext.power(Decimal('2'), Decimal('3'))
+        >>> c = ExtendedContext.copy()
+        >>> c.Emin = -999
+        >>> c.Emax = 999
+        >>> c.power(Decimal('2'), Decimal('3'))
         Decimal("8")
-        >>> ExtendedContext.power(Decimal('2'), Decimal('-3'))
+        >>> c.power(Decimal('-2'), Decimal('3'))
+        Decimal("-8")
+        >>> c.power(Decimal('2'), Decimal('-3'))
         Decimal("0.125")
-        >>> ExtendedContext.power(Decimal('1.7'), Decimal('8'))
+        >>> c.power(Decimal('1.7'), Decimal('8'))
         Decimal("69.7575744")
-        >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-2'))
-        Decimal("0")
-        >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-1'))
+        >>> c.power(Decimal('10'), Decimal('0.301029996'))
+        Decimal("2.00000000")
+        >>> c.power(Decimal('Infinity'), Decimal('-1'))
         Decimal("0")
-        >>> ExtendedContext.power(Decimal('Infinity'), Decimal('0'))
+        >>> c.power(Decimal('Infinity'), Decimal('0'))
         Decimal("1")
-        >>> ExtendedContext.power(Decimal('Infinity'), Decimal('1'))
-        Decimal("Infinity")
-        >>> ExtendedContext.power(Decimal('Infinity'), Decimal('2'))
+        >>> c.power(Decimal('Infinity'), Decimal('1'))
         Decimal("Infinity")
-        >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-2'))
-        Decimal("0")
-        >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-1'))
+        >>> c.power(Decimal('-Infinity'), Decimal('-1'))
         Decimal("-0")
-        >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('0'))
+        >>> c.power(Decimal('-Infinity'), Decimal('0'))
         Decimal("1")
-        >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('1'))
+        >>> c.power(Decimal('-Infinity'), Decimal('1'))
         Decimal("-Infinity")
-        >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('2'))
+        >>> c.power(Decimal('-Infinity'), Decimal('2'))
         Decimal("Infinity")
-        >>> ExtendedContext.power(Decimal('0'), Decimal('0'))
+        >>> c.power(Decimal('0'), Decimal('0'))
         Decimal("NaN")
+
+        >>> c.power(Decimal('3'), Decimal('7'), Decimal('16'))
+        Decimal("11")
+        >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16'))
+        Decimal("-11")
+        >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16'))
+        Decimal("1")
+        >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16'))
+        Decimal("11")
+        >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789'))
+        Decimal("11729830")
+        >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729'))
+        Decimal("-0")
+        >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537'))
+        Decimal("1")
         """
         return a.__pow__(b, modulo, context=self)
 
@@ -4849,6 +5173,56 @@
     # error in result of _iexp < 120;  error after division < 0.62
     return _div_nearest(_iexp(rem, 10**p), 1000), quot - p + 3
 
+def _dpower(xc, xe, yc, ye, p):
+    """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 digits
+    of precision, and with an error in c of at most 1.  (This is
+    almost, but not quite, the same as the error being < 1ulp: when c
+    == 10**(p-1) we can only guarantee error < 10ulp.)
+
+    We assume that: x is positive and not equal to 1, and y is nonzero.
+    """
+
+    # Find b such that 10**(b-1) <= |y| <= 10**b
+    b = len(str(abs(yc))) + ye
+
+    # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point
+    lxc = _dlog(xc, xe, p+b+1)
+
+    # compute product y*log(x) = yc*lxc*10**(-p-b-1+ye) = pc*10**(-p-1)
+    shift = ye-b
+    if shift >= 0:
+        pc = lxc*yc*10**shift
+    else:
+        pc = _div_nearest(lxc*yc, 10**-shift)
+
+    if pc == 0:
+        # we prefer a result that isn't exactly 1; this makes it
+        # easier to compute a correctly rounded result in __pow__
+        if ((len(str(xc)) + xe >= 1) == (yc > 0)): # if x**y > 1:
+            coeff, exp = 10**(p-1)+1, 1-p
+        else:
+            coeff, exp = 10**p-1, -p
+    else:
+        coeff, exp = _dexp(pc, -(p+1), p+1)
+        coeff = _div_nearest(coeff, 10)
+        exp += 1
+
+    return coeff, exp
+
+def _log10_lb(c, correction = {
+        '1': 100, '2': 70, '3': 53, '4': 40, '5': 31,
+        '6': 23, '7': 16, '8': 10, '9': 5}):
+    """Compute a lower bound for 100*log10(c) for a positive integer c."""
+    if c <= 0:
+        raise ValueError("The argument to _log10_lb should be nonnegative.")
+    str_c = str(c)
+    return 100*len(str_c) - correction[str_c[0]]
 
 ##### Helper Functions ####################################################
 

Modified: python/branches/decimal-branch/Lib/test/decimaltestdata/extra.decTest
==============================================================================
--- python/branches/decimal-branch/Lib/test/decimaltestdata/extra.decTest	(original)
+++ python/branches/decimal-branch/Lib/test/decimaltestdata/extra.decTest	Thu Aug 23 17:40:15 2007
@@ -34,8 +34,8 @@
 extr0023 apply 1E-12 -> 1E-12 Subnormal
 extr0024 apply 1E-13 -> 1E-13 Subnormal
 extr0025 apply 1E-14 -> 1E-14 Subnormal
-extr0026 apply 1E-15 -> 0E-14 Inexact Rounded Subnormal Underflow Clamped 
-extr0027 apply 1E-16 -> 0E-14 Inexact Rounded Subnormal Underflow Clamped 
+extr0026 apply 1E-15 -> 0E-14 Inexact Rounded Subnormal Underflow Clamped
+extr0027 apply 1E-16 -> 0E-14 Inexact Rounded Subnormal Underflow Clamped
 clamp: 0
 
 -- large precision, small minimum and maximum exponent; in this case
@@ -88,8 +88,8 @@
 extr0141 apply 1E-30 -> 1E-30 Subnormal
 extr0142 apply 1E-31 -> 1E-31 Subnormal
 extr0143 apply 1E-32 -> 1E-32 Subnormal
-extr0144 apply 1E-33 -> 0E-32 Inexact Rounded Subnormal Underflow Clamped 
-extr0145 apply 1E-34 -> 0E-32 Inexact Rounded Subnormal Underflow Clamped 
+extr0144 apply 1E-33 -> 0E-32 Inexact Rounded Subnormal Underflow Clamped
+extr0145 apply 1E-34 -> 0E-32 Inexact Rounded Subnormal Underflow Clamped
 clamp: 0
 
 -- some buggy addition cases from Python 2.5.x
@@ -110,3 +110,415 @@
 precision: 6
 extr1100 add 0E+1000 1E+1000 -> Infinity Overflow Inexact Rounded
 extr1101 remainder 1E+1000 2E+1000 -> Infinity Overflow Inexact Rounded
+
+------------------------------------------------------------------------
+-- The following tests (pwmx0 through pwmx440) are for the            --
+-- three-argument version of power:                                   --
+--                                                                    --
+--   pow(x, y, z) := x**y % z                                         --
+--                                                                    --
+-- Note that the three-argument version of power is *not* part of     --
+-- the IBM General Decimal Arithmetic specification.  Questions       --
+-- about it, or about these testcases, should go to one of the        --
+-- Python decimal authors.                                            --
+------------------------------------------------------------------------
+
+extended: 1
+precision: 9
+rounding: down
+maxExponent: 999
+minExponent: -999
+
+-- Small numbers
+-- Note that power(0, 0, m) is an error for any m
+pwmx0 power 0 -0 1 -> NaN Invalid_operation
+pwmx1 power 0 -0 2 -> NaN Invalid_operation
+pwmx2 power 0 -0 3 -> NaN Invalid_operation
+pwmx3 power 0 -0 4 -> NaN Invalid_operation
+pwmx4 power 0 -0 -1 -> NaN Invalid_operation
+pwmx5 power 0 -0 -2 -> NaN Invalid_operation
+pwmx6 power 0 0 1 -> NaN Invalid_operation
+pwmx7 power 0 0 2 -> NaN Invalid_operation
+pwmx8 power 0 0 3 -> NaN Invalid_operation
+pwmx9 power 0 0 4 -> NaN Invalid_operation
+pwmx10 power 0 0 -1 -> NaN Invalid_operation
+pwmx11 power 0 0 -2 -> NaN Invalid_operation
+pwmx12 power 0 1 1 -> 0
+pwmx13 power 0 1 2 -> 0
+pwmx14 power 0 1 3 -> 0
+pwmx15 power 0 1 4 -> 0
+pwmx16 power 0 1 -1 -> 0
+pwmx17 power 0 1 -2 -> 0
+pwmx18 power 0 2 1 -> 0
+pwmx19 power 0 2 2 -> 0
+pwmx20 power 0 2 3 -> 0
+pwmx21 power 0 2 4 -> 0
+pwmx22 power 0 2 -1 -> 0
+pwmx23 power 0 2 -2 -> 0
+pwmx24 power 0 3 1 -> 0
+pwmx25 power 0 3 2 -> 0
+pwmx26 power 0 3 3 -> 0
+pwmx27 power 0 3 4 -> 0
+pwmx28 power 0 3 -1 -> 0
+pwmx29 power 0 3 -2 -> 0
+pwmx30 power 0 4 1 -> 0
+pwmx31 power 0 4 2 -> 0
+pwmx32 power 0 4 3 -> 0
+pwmx33 power 0 4 4 -> 0
+pwmx34 power 0 4 -1 -> 0
+pwmx35 power 0 4 -2 -> 0
+pwmx36 power 0 5 1 -> 0
+pwmx37 power 0 5 2 -> 0
+pwmx38 power 0 5 3 -> 0
+pwmx39 power 0 5 4 -> 0
+pwmx40 power 0 5 -1 -> 0
+pwmx41 power 0 5 -2 -> 0
+pwmx42 power 1 -0 1 -> 0
+pwmx43 power 1 -0 2 -> 1
+pwmx44 power 1 -0 3 -> 1
+pwmx45 power 1 -0 4 -> 1
+pwmx46 power 1 -0 -1 -> 0
+pwmx47 power 1 -0 -2 -> 1
+pwmx48 power 1 0 1 -> 0
+pwmx49 power 1 0 2 -> 1
+pwmx50 power 1 0 3 -> 1
+pwmx51 power 1 0 4 -> 1
+pwmx52 power 1 0 -1 -> 0
+pwmx53 power 1 0 -2 -> 1
+pwmx54 power 1 1 1 -> 0
+pwmx55 power 1 1 2 -> 1
+pwmx56 power 1 1 3 -> 1
+pwmx57 power 1 1 4 -> 1
+pwmx58 power 1 1 -1 -> 0
+pwmx59 power 1 1 -2 -> 1
+pwmx60 power 1 2 1 -> 0
+pwmx61 power 1 2 2 -> 1
+pwmx62 power 1 2 3 -> 1
+pwmx63 power 1 2 4 -> 1
+pwmx64 power 1 2 -1 -> 0
+pwmx65 power 1 2 -2 -> 1
+pwmx66 power 1 3 1 -> 0
+pwmx67 power 1 3 2 -> 1
+pwmx68 power 1 3 3 -> 1
+pwmx69 power 1 3 4 -> 1
+pwmx70 power 1 3 -1 -> 0
+pwmx71 power 1 3 -2 -> 1
+pwmx72 power 1 4 1 -> 0
+pwmx73 power 1 4 2 -> 1
+pwmx74 power 1 4 3 -> 1
+pwmx75 power 1 4 4 -> 1
+pwmx76 power 1 4 -1 -> 0
+pwmx77 power 1 4 -2 -> 1
+pwmx78 power 1 5 1 -> 0
+pwmx79 power 1 5 2 -> 1
+pwmx80 power 1 5 3 -> 1
+pwmx81 power 1 5 4 -> 1
+pwmx82 power 1 5 -1 -> 0
+pwmx83 power 1 5 -2 -> 1
+pwmx84 power 2 -0 1 -> 0
+pwmx85 power 2 -0 2 -> 1
+pwmx86 power 2 -0 3 -> 1
+pwmx87 power 2 -0 4 -> 1
+pwmx88 power 2 -0 -1 -> 0
+pwmx89 power 2 -0 -2 -> 1
+pwmx90 power 2 0 1 -> 0
+pwmx91 power 2 0 2 -> 1
+pwmx92 power 2 0 3 -> 1
+pwmx93 power 2 0 4 -> 1
+pwmx94 power 2 0 -1 -> 0
+pwmx95 power 2 0 -2 -> 1
+pwmx96 power 2 1 1 -> 0
+pwmx97 power 2 1 2 -> 0
+pwmx98 power 2 1 3 -> 2
+pwmx99 power 2 1 4 -> 2
+pwmx100 power 2 1 -1 -> 0
+pwmx101 power 2 1 -2 -> 0
+pwmx102 power 2 2 1 -> 0
+pwmx103 power 2 2 2 -> 0
+pwmx104 power 2 2 3 -> 1
+pwmx105 power 2 2 4 -> 0
+pwmx106 power 2 2 -1 -> 0
+pwmx107 power 2 2 -2 -> 0
+pwmx108 power 2 3 1 -> 0
+pwmx109 power 2 3 2 -> 0
+pwmx110 power 2 3 3 -> 2
+pwmx111 power 2 3 4 -> 0
+pwmx112 power 2 3 -1 -> 0
+pwmx113 power 2 3 -2 -> 0
+pwmx114 power 2 4 1 -> 0
+pwmx115 power 2 4 2 -> 0
+pwmx116 power 2 4 3 -> 1
+pwmx117 power 2 4 4 -> 0
+pwmx118 power 2 4 -1 -> 0
+pwmx119 power 2 4 -2 -> 0
+pwmx120 power 2 5 1 -> 0
+pwmx121 power 2 5 2 -> 0
+pwmx122 power 2 5 3 -> 2
+pwmx123 power 2 5 4 -> 0
+pwmx124 power 2 5 -1 -> 0
+pwmx125 power 2 5 -2 -> 0
+pwmx126 power 3 -0 1 -> 0
+pwmx127 power 3 -0 2 -> 1
+pwmx128 power 3 -0 3 -> 1
+pwmx129 power 3 -0 4 -> 1
+pwmx130 power 3 -0 -1 -> 0
+pwmx131 power 3 -0 -2 -> 1
+pwmx132 power 3 0 1 -> 0
+pwmx133 power 3 0 2 -> 1
+pwmx134 power 3 0 3 -> 1
+pwmx135 power 3 0 4 -> 1
+pwmx136 power 3 0 -1 -> 0
+pwmx137 power 3 0 -2 -> 1
+pwmx138 power 3 1 1 -> 0
+pwmx139 power 3 1 2 -> 1
+pwmx140 power 3 1 3 -> 0
+pwmx141 power 3 1 4 -> 3
+pwmx142 power 3 1 -1 -> 0
+pwmx143 power 3 1 -2 -> 1
+pwmx144 power 3 2 1 -> 0
+pwmx145 power 3 2 2 -> 1
+pwmx146 power 3 2 3 -> 0
+pwmx147 power 3 2 4 -> 1
+pwmx148 power 3 2 -1 -> 0
+pwmx149 power 3 2 -2 -> 1
+pwmx150 power 3 3 1 -> 0
+pwmx151 power 3 3 2 -> 1
+pwmx152 power 3 3 3 -> 0
+pwmx153 power 3 3 4 -> 3
+pwmx154 power 3 3 -1 -> 0
+pwmx155 power 3 3 -2 -> 1
+pwmx156 power 3 4 1 -> 0
+pwmx157 power 3 4 2 -> 1
+pwmx158 power 3 4 3 -> 0
+pwmx159 power 3 4 4 -> 1
+pwmx160 power 3 4 -1 -> 0
+pwmx161 power 3 4 -2 -> 1
+pwmx162 power 3 5 1 -> 0
+pwmx163 power 3 5 2 -> 1
+pwmx164 power 3 5 3 -> 0
+pwmx165 power 3 5 4 -> 3
+pwmx166 power 3 5 -1 -> 0
+pwmx167 power 3 5 -2 -> 1
+pwmx168 power -0 -0 1 -> NaN Invalid_operation
+pwmx169 power -0 -0 2 -> NaN Invalid_operation
+pwmx170 power -0 -0 3 -> NaN Invalid_operation
+pwmx171 power -0 -0 4 -> NaN Invalid_operation
+pwmx172 power -0 -0 -1 -> NaN Invalid_operation
+pwmx173 power -0 -0 -2 -> NaN Invalid_operation
+pwmx174 power -0 0 1 -> NaN Invalid_operation
+pwmx175 power -0 0 2 -> NaN Invalid_operation
+pwmx176 power -0 0 3 -> NaN Invalid_operation
+pwmx177 power -0 0 4 -> NaN Invalid_operation
+pwmx178 power -0 0 -1 -> NaN Invalid_operation
+pwmx179 power -0 0 -2 -> NaN Invalid_operation
+pwmx180 power -0 1 1 -> -0
+pwmx181 power -0 1 2 -> -0
+pwmx182 power -0 1 3 -> -0
+pwmx183 power -0 1 4 -> -0
+pwmx184 power -0 1 -1 -> -0
+pwmx185 power -0 1 -2 -> -0
+pwmx186 power -0 2 1 -> 0
+pwmx187 power -0 2 2 -> 0
+pwmx188 power -0 2 3 -> 0
+pwmx189 power -0 2 4 -> 0
+pwmx190 power -0 2 -1 -> 0
+pwmx191 power -0 2 -2 -> 0
+pwmx192 power -0 3 1 -> -0
+pwmx193 power -0 3 2 -> -0
+pwmx194 power -0 3 3 -> -0
+pwmx195 power -0 3 4 -> -0
+pwmx196 power -0 3 -1 -> -0
+pwmx197 power -0 3 -2 -> -0
+pwmx198 power -0 4 1 -> 0
+pwmx199 power -0 4 2 -> 0
+pwmx200 power -0 4 3 -> 0
+pwmx201 power -0 4 4 -> 0
+pwmx202 power -0 4 -1 -> 0
+pwmx203 power -0 4 -2 -> 0
+pwmx204 power -0 5 1 -> -0
+pwmx205 power -0 5 2 -> -0
+pwmx206 power -0 5 3 -> -0
+pwmx207 power -0 5 4 -> -0
+pwmx208 power -0 5 -1 -> -0
+pwmx209 power -0 5 -2 -> -0
+pwmx210 power -1 -0 1 -> 0
+pwmx211 power -1 -0 2 -> 1
+pwmx212 power -1 -0 3 -> 1
+pwmx213 power -1 -0 4 -> 1
+pwmx214 power -1 -0 -1 -> 0
+pwmx215 power -1 -0 -2 -> 1
+pwmx216 power -1 0 1 -> 0
+pwmx217 power -1 0 2 -> 1
+pwmx218 power -1 0 3 -> 1
+pwmx219 power -1 0 4 -> 1
+pwmx220 power -1 0 -1 -> 0
+pwmx221 power -1 0 -2 -> 1
+pwmx222 power -1 1 1 -> -0
+pwmx223 power -1 1 2 -> -1
+pwmx224 power -1 1 3 -> -1
+pwmx225 power -1 1 4 -> -1
+pwmx226 power -1 1 -1 -> -0
+pwmx227 power -1 1 -2 -> -1
+pwmx228 power -1 2 1 -> 0
+pwmx229 power -1 2 2 -> 1
+pwmx230 power -1 2 3 -> 1
+pwmx231 power -1 2 4 -> 1
+pwmx232 power -1 2 -1 -> 0
+pwmx233 power -1 2 -2 -> 1
+pwmx234 power -1 3 1 -> -0
+pwmx235 power -1 3 2 -> -1
+pwmx236 power -1 3 3 -> -1
+pwmx237 power -1 3 4 -> -1
+pwmx238 power -1 3 -1 -> -0
+pwmx239 power -1 3 -2 -> -1
+pwmx240 power -1 4 1 -> 0
+pwmx241 power -1 4 2 -> 1
+pwmx242 power -1 4 3 -> 1
+pwmx243 power -1 4 4 -> 1
+pwmx244 power -1 4 -1 -> 0
+pwmx245 power -1 4 -2 -> 1
+pwmx246 power -1 5 1 -> -0
+pwmx247 power -1 5 2 -> -1
+pwmx248 power -1 5 3 -> -1
+pwmx249 power -1 5 4 -> -1
+pwmx250 power -1 5 -1 -> -0
+pwmx251 power -1 5 -2 -> -1
+
+-- Randomly chosen larger values
+pwmx252 power 0 4 7 -> 0
+pwmx253 power -4 5 -9 -> -7
+pwmx254 power -5 4 -9 -> 4
+pwmx255 power -50 29 2 -> -0
+pwmx256 power -1 83 3 -> -1
+pwmx257 power -55 65 -75 -> -25
+pwmx258 power -613 151 -302 -> -9
+pwmx259 power 551 23 -35 -> 31
+pwmx260 power 51 142 942 -> 9
+pwmx261 power 6886 9204 -6091 -> 5034
+pwmx262 power 3057 5890 -3 -> 0
+pwmx263 power 56 4438 5365 -> 521
+pwmx264 power 96237 35669 -46669 -> 30717
+pwmx265 power 40011 34375 -57611 -> 625
+pwmx266 power 44317 38493 -12196 -> 11081
+pwmx267 power -282368 895633 -235870 -> -220928
+pwmx268 power 77328 852553 -405529 -> 129173
+pwmx269 power -929659 855713 650348 -> -90803
+pwmx270 power 907057 6574309 4924768 -> 3018257
+pwmx271 power -2887757 3198492 -5864352 -> 3440113
+pwmx272 power -247310 657371 -7415739 -> -1301840
+pwmx273 power -8399046 45334087 -22395020 -> -18515896
+pwmx274 power 79621397 4850236 1486555 -> 928706
+pwmx275 power 96012251 27971901 69609031 -> 50028729
+pwmx276 power -907335481 74127986 582330017 -> 51527187
+pwmx277 power -141192960 821063826 -260877928 -> 112318560
+pwmx278 power -501711702 934355994 82135143 -> 66586995
+pwmx279 power -9256358075 8900900138 -467222031 -> 95800246
+pwmx280 power -7031964291 1751257483 -935334498 -> -607626609
+pwmx281 power 8494314971 8740197252 107522491 -> 17373655
+pwmx282 power 88306216890 87477374166 -23498076 -> 15129528
+pwmx283 power -33939432478 7170196239 22133583 -> -11017036
+pwmx284 power 19466222767 30410710614 305752056 -> 191509537
+pwmx285 power -864942494008 370558899638 346688856 -> 56956768
+pwmx286 power -525406225603 345700226898 237163621 -> 56789534
+pwmx287 power 464612215955 312474621651 -329485700 -> 1853975
+pwmx288 power -1664283031244 3774474669855 919022867 -> -516034520
+pwmx289 power -3472438506913 7407327549995 -451206854 -> -74594761
+pwmx290 power -4223662152949 6891069279069 499843503 -> -80135290
+pwmx291 power -44022119276816 8168266170326 569679509 -> 375734475
+pwmx292 power -66195891207902 12532690555875 -243262129 -> -113186833
+pwmx293 power -69039911263164 52726605857673 360625196 -> -268662748
+pwmx294 power -299010116699208 885092589359231 -731310123 -> -104103765
+pwmx295 power -202495776299758 501159122943145 -686234870 -> -135511878
+pwmx296 power -595411478087676 836269270472481 -214614901 -> -183440819
+pwmx297 power -139555381056229 1324808520020507 -228944738 -> -218991473
+pwmx298 power 7846356250770543 1798045051036814 -101028985 -> 7805179
+pwmx299 power -4298015862709415 604966944844209 880212893 -> -87408671
+pwmx300 power -37384897538910893 76022206995659295 -930512842 -> -697757157
+pwmx301 power 82166659028005443 23375408251767704 817270700 -> 770697001
+pwmx302 power 97420301198165641 72213282983416924 947519716 -> 610711721
+pwmx303 power 913382043453243607 449681707248500262 211135545 -> 79544899
+pwmx304 power -313823613418052171 534579409610142937 -943062968 -> -446001379
+pwmx305 power -928106516894494093 760020177330116509 -50043994 -> -46010575
+pwmx306 power 4692146601679439796 4565354511806767804 -667339075 -> 480272081
+pwmx307 power 9722256633509177930 7276568791860505790 792675321 -> 182879752
+pwmx308 power 8689899484830064228 429082967129615261 -844555637 -> 270374557
+
+-- All inputs must be integers
+pwmx309 power 2.1 3 1 -> NaN Invalid_operation
+pwmx310 power 0.4 1 5 -> NaN Invalid_operation
+pwmx311 power 2 3.1 5 -> NaN Invalid_operation
+pwmx312 power 13 -1.2 10 -> NaN Invalid_operation
+pwmx313 power 2 3 5.1 -> NaN Invalid_operation
+
+-- Second argument must be nonnegative (-0 is okay)
+pwmx314 power 2 -3 5 -> NaN Invalid_operation
+pwmx315 power 7 -1 1 -> NaN Invalid_operation
+pwmx316 power 0 -2 6 -> NaN Invalid_operation
+
+-- Third argument must be nonzero
+pwmx317 power 13 1003 0 -> NaN Invalid_operation
+pwmx318 power 1 0 0E+987 -> NaN Invalid_operation
+pwmx319 power 0 2 -0 -> NaN Invalid_operation
+
+-- Integers are fine, no matter how they're expressed
+pwmx320 power 13.0 117.00 1E+2 -> 33
+pwmx321 power -2E+3 1.1E+10 -12323 -> 4811
+pwmx322 power 20 0E-300 143 -> 1
+pwmx323 power -20 -0E+1005 1179 -> 1
+pwmx324 power 0E-1001 17 5.6E+4 -> 0
+
+-- Modulus must not exceed precision
+pwmx325 power 0 1 1234567890 -> NaN Invalid_operation
+pwmx326 power 1 0 1000000000 -> NaN Invalid_operation
+pwmx327 power -23 5 -1000000000 -> NaN Invalid_operation
+pwmx328 power 41557 213 -999999999 -> 47650456
+pwmx329 power -2134 199 999999997 -> -946957912
+
+-- Huge base shouldn't present any problems
+pwmx330 power 1.23E+123456791 10123898 17291065 -> 5674045
+
+-- Large exponent, may be slow
+-- (if second argument is 1En then expect O(n) running time)
+pwmx331 power 1000288896 9.87E+12347 93379908 -> 43224924
+
+-- Triple NaN propagation (adapted from examples in fma.decTest)
+pwmx400 power  NaN2  NaN3  NaN5   ->  NaN2
+pwmx401 power  1     NaN3  NaN5   ->  NaN3
+pwmx402 power  1     1     NaN5   ->  NaN5
+pwmx403 power  sNaN1 sNaN2 sNaN3  ->  NaN1 Invalid_operation
+pwmx404 power  1     sNaN2 sNaN3  ->  NaN2 Invalid_operation
+pwmx405 power  1     1     sNaN3  ->  NaN3 Invalid_operation
+pwmx406 power  sNaN1 sNaN2 sNaN3  ->  NaN1 Invalid_operation
+pwmx407 power  NaN7  sNaN2 sNaN3  ->  NaN2 Invalid_operation
+pwmx408 power  NaN7  NaN5  sNaN3  ->  NaN3 Invalid_operation
+
+-- Infinities not allowed
+pwmx410 power Inf 1 1 -> NaN Invalid_operation
+pwmx411 power 1 Inf 1 -> NaN Invalid_operation
+pwmx412 power 1 1 Inf -> NaN Invalid_operation
+pwmx413 power -Inf 1 1 -> NaN Invalid_operation
+pwmx414 power 1 -Inf 1 -> NaN Invalid_operation
+pwmx415 power 1 1 -Inf -> NaN Invalid_operation
+
+-- Just for fun: 1729 is a Carmichael number
+pwmx420 power 0 1728 1729 -> 0
+pwmx421 power 1 1728 1729 -> 1
+pwmx422 power 2 1728 1729 -> 1
+pwmx423 power 3 1728 1729 -> 1
+pwmx424 power 4 1728 1729 -> 1
+pwmx425 power 5 1728 1729 -> 1
+pwmx426 power 6 1728 1729 -> 1
+pwmx427 power 7 1728 1729 -> 742
+pwmx428 power 8 1728 1729 -> 1
+pwmx429 power 9 1728 1729 -> 1
+pwmx430 power 10 1728 1729 -> 1
+pwmx431 power 11 1728 1729 -> 1
+pwmx432 power 12 1728 1729 -> 1
+pwmx433 power 13 1728 1729 -> 533
+pwmx434 power 14 1728 1729 -> 742
+pwmx435 power 15 1728 1729 -> 1
+pwmx436 power 16 1728 1729 -> 1
+pwmx437 power 17 1728 1729 -> 1
+pwmx438 power 18 1728 1729 -> 1
+pwmx439 power 19 1728 1729 -> 456
+pwmx440 power 20 1728 1729 -> 1

Modified: python/branches/decimal-branch/Lib/test/test_decimal.py
==============================================================================
--- python/branches/decimal-branch/Lib/test/test_decimal.py	(original)
+++ python/branches/decimal-branch/Lib/test/test_decimal.py	Thu Aug 23 17:40:15 2007
@@ -126,6 +126,28 @@
                'nexttoward':'next_toward',
               }
 
+# For some operations (currently exp, ln, log10, power), the decNumber
+# reference implementation imposes additional restrictions on the
+# context and operands.  These restrictions are not part of the
+# specification; however, the effect of these restrictions does show
+# up in some of the testcases.  We skip testcases that violate these
+# restrictions, since Decimal behaves differently from decNumber for
+# these testcases so these testcases would otherwise fail.
+
+decNumberRestricted = ('power', 'ln', 'log10', 'exp')
+DEC_MAX_MATH = 999999
+def outside_decNumber_bounds(v, context):
+    if (context.prec > DEC_MAX_MATH or
+        context.Emax > DEC_MAX_MATH or
+        -context.Emin > DEC_MAX_MATH):
+        return True
+    if not v._is_special and v and (
+        len(v._int) > DEC_MAX_MATH or
+        v.adjusted() > DEC_MAX_MATH or
+        v.adjusted() < 1-2*DEC_MAX_MATH):
+        return True
+    return False
+
 class DecimalTest(unittest.TestCase):
     """Class which tests the Decimal class against the test cases.
 
@@ -268,6 +290,22 @@
 
         ans = FixQuotes(ans)
 
+        # skip tests that are related to bounds imposed in the decNumber
+        # reference implementation
+        if fname in decNumberRestricted:
+            if fname == 'power':
+                if not (vals[1]._isinteger() and
+                        -1999999997 <= vals[1] <= 999999999):
+                    if outside_decNumber_bounds(vals[0], self.context) or \
+                            outside_decNumber_bounds(vals[1], self.context):
+                        #print "Skipping test %s" % s
+                        return
+            else:
+                if outside_decNumber_bounds(vals[0], self.context):
+                    #print "Skipping test %s" % s
+                    return
+
+
         if EXTENDEDERRORTEST and fname not in ('to_sci_string', 'to_eng_string'):
             for error in theirexceptions:
                 self.context.traps[error] = 1


More information about the Python-checkins mailing list