MCPcopy Index your code
hub / github.com/python/cpython / _power_exact

Method _power_exact

Lib/_pydecimal.py:2005–2251  ·  view source on GitHub ↗

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 th

(self, other, p)

Source from the content-addressed store, hash-verified

2003 return _dec_from_triple(sign, str(base), 0)
2004
2005 def _power_exact(self, other, p):
2006 """Attempt to compute self**other exactly.
2007
2008 Given Decimals self and other and an integer p, attempt to
2009 compute an exact result for the power self**other, with p
2010 digits of precision. Return None if self**other is not
2011 exactly representable in p digits.
2012
2013 Assumes that elimination of special cases has already been
2014 performed: self and other must both be nonspecial; self must
2015 be positive and not numerically equal to 1; other must be
2016 nonzero. For efficiency, other._exp should not be too large,
2017 so that 10**abs(other._exp) is a feasible calculation."""
2018
2019 # In the comments below, we write x for the value of self and y for the
2020 # value of other. Write x = xc*10**xe and abs(y) = yc*10**ye, with xc
2021 # and yc positive integers not divisible by 10.
2022
2023 # The main purpose of this method is to identify the *failure*
2024 # of x**y to be exactly representable with as little effort as
2025 # possible. So we look for cheap and easy tests that
2026 # eliminate the possibility of x**y being exact. Only if all
2027 # these tests are passed do we go on to actually compute x**y.
2028
2029 # Here's the main idea. Express y as a rational number m/n, with m and
2030 # n relatively prime and n>0. Then for x**y to be exactly
2031 # representable (at *any* precision), xc must be the nth power of a
2032 # positive integer and xe must be divisible by n. If y is negative
2033 # then additionally xc must be a power of either 2 or 5, hence a power
2034 # of 2**n or 5**n.
2035 #
2036 # There's a limit to how small |y| can be: if y=m/n as above
2037 # then:
2038 #
2039 # (1) if xc != 1 then for the result to be representable we
2040 # need xc**(1/n) >= 2, and hence also xc**|y| >= 2. So
2041 # if |y| <= 1/nbits(xc) then xc < 2**nbits(xc) <=
2042 # 2**(1/|y|), hence xc**|y| < 2 and the result is not
2043 # representable.
2044 #
2045 # (2) if xe != 0, |xe|*(1/n) >= 1, so |xe|*|y| >= 1. Hence if
2046 # |y| < 1/|xe| then the result is not representable.
2047 #
2048 # Note that since x is not equal to 1, at least one of (1) and
2049 # (2) must apply. Now |y| < 1/nbits(xc) iff |yc|*nbits(xc) <
2050 # 10**-ye iff len(str(|yc|*nbits(xc)) <= -ye.
2051 #
2052 # There's also a limit to how large y can be, at least if it's
2053 # positive: the normalized result will have coefficient xc**y,
2054 # so if it's representable then xc**y < 10**p, and y <
2055 # p/log10(xc). Hence if y*log10(xc) >= p then the result is
2056 # not exactly representable.
2057
2058 # if len(str(abs(yc*xe)) <= -ye then abs(yc*xe) < 10**-ye,
2059 # so |y| < 1/xe and the result is not representable.
2060 # Similarly, len(str(abs(yc)*xc_bits)) <= -ye implies |y|
2061 # < 1/nbits(xc).
2062

Callers 1

__pow__Method · 0.95

Calls 7

_WorkRepClass · 0.85
_dec_from_tripleFunction · 0.85
strFunction · 0.85
_decimal_lshift_exactFunction · 0.85
absFunction · 0.85
_log10_lbFunction · 0.85
_isintegerMethod · 0.80

Tested by

no test coverage detected