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

Method _log10_exp_bound

Lib/_pydecimal.py:3207–3235  ·  view source on GitHub ↗

Compute a lower bound for the adjusted exponent of self.log10(). In other words, find r such that self.log10() >= 10**r. Assumes that self is finite and positive and that self != 1.

(self)

Source from the content-addressed store, hash-verified

3205 return ans
3206
3207 def _log10_exp_bound(self):
3208 """Compute a lower bound for the adjusted exponent of self.log10().
3209 In other words, find r such that self.log10() >= 10**r.
3210 Assumes that self is finite and positive and that self != 1.
3211 """
3212
3213 # For x >= 10 or x < 0.1 we only need a bound on the integer
3214 # part of log10(self), and this comes directly from the
3215 # exponent of x. For 0.1 <= x <= 10 we use the inequalities
3216 # 1-1/x <= log(x) <= x-1. If x > 1 we have |log10(x)| >
3217 # (1-1/x)/2.31 > 0. If x < 1 then |log10(x)| > (1-x)/2.31 > 0
3218
3219 adj = self._exp + len(self._int) - 1
3220 if adj >= 1:
3221 # self >= 10
3222 return len(str(adj))-1
3223 if adj <= -2:
3224 # self < 0.1
3225 return len(str(-1-adj))-1
3226 op = _WorkRep(self)
3227 c, e = op.int, op.exp
3228 if adj == 0:
3229 # 1 < self < 10
3230 num = str(c-10**-e)
3231 den = str(231*c)
3232 return len(num) - len(den) - (num < den) + 2
3233 # adj == -1, 0.1 <= self < 1
3234 num = str(10**-e-c)
3235 return len(num) + e - (num < "231") - 1
3236
3237 def log10(self, context=None):
3238 """Returns the base 10 logarithm of self."""

Callers 2

__pow__Method · 0.95
log10Method · 0.95

Calls 2

strFunction · 0.85
_WorkRepClass · 0.85

Tested by

no test coverage detected