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

Method log10

Lib/_pydecimal.py:3237–3286  ·  view source on GitHub ↗

Returns the base 10 logarithm of self.

(self, context=None)

Source from the content-addressed store, hash-verified

3235 return len(num) + e - (num < "231") - 1
3236
3237 def log10(self, context=None):
3238 """Returns the base 10 logarithm of self."""
3239
3240 if context is None:
3241 context = getcontext()
3242
3243 # log10(NaN) = NaN
3244 ans = self._check_nans(context=context)
3245 if ans:
3246 return ans
3247
3248 # log10(0.0) == -Infinity
3249 if not self:
3250 return _NegativeInfinity
3251
3252 # log10(Infinity) = Infinity
3253 if self._isinfinity() == 1:
3254 return _Infinity
3255
3256 # log10(negative or -Infinity) raises InvalidOperation
3257 if self._sign == 1:
3258 return context._raise_error(InvalidOperation,
3259 'log10 of a negative value')
3260
3261 # log10(10**n) = n
3262 if self._int[0] == '1' and self._int[1:] == '0'*(len(self._int) - 1):
3263 # answer may need rounding
3264 ans = Decimal(self._exp + len(self._int) - 1)
3265 else:
3266 # result is irrational, so necessarily inexact
3267 op = _WorkRep(self)
3268 c, e = op.int, op.exp
3269 p = context.prec
3270
3271 # correctly rounded result: repeatedly increase precision
3272 # until result is unambiguously roundable
3273 places = p-self._log10_exp_bound()+2
3274 while True:
3275 coeff = _dlog10(c, e, places)
3276 # assert len(str(abs(coeff)))-p >= 1
3277 if coeff % (5*10**(len(str(abs(coeff)))-p-1)):
3278 break
3279 places += 3
3280 ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places)
3281
3282 context = context._shallow_copy()
3283 rounding = context._set_rounding(ROUND_HALF_EVEN)
3284 ans = ans._fix(context)
3285 context.rounding = rounding
3286 return ans
3287
3288 def logb(self, context=None):
3289 """ Returns the exponent of the magnitude of self&#x27;s MSD.

Callers 10

test_none_argsMethod · 0.95
repr_intMethod · 0.45
log10Method · 0.45
XFunction · 0.45
test_logsMethod · 0.45
test_named_parametersMethod · 0.45
test_implicit_contextMethod · 0.45
testLog10Method · 0.45
test_log_huge_integerMethod · 0.45

Calls 14

_check_nansMethod · 0.95
_isinfinityMethod · 0.95
_log10_exp_boundMethod · 0.95
_fixMethod · 0.95
getcontextFunction · 0.85
DecimalClass · 0.85
_WorkRepClass · 0.85
_dlog10Function · 0.85
strFunction · 0.85
absFunction · 0.85
_dec_from_tripleFunction · 0.85
_raise_errorMethod · 0.80

Tested by 7

test_none_argsMethod · 0.76
test_logsMethod · 0.36
test_named_parametersMethod · 0.36
test_implicit_contextMethod · 0.36
testLog10Method · 0.36
test_log_huge_integerMethod · 0.36