MCPcopy Index your code
hub / github.com/RustPython/RustPython / log10

Method log10

Lib/_pydecimal.py:3234–3283  ·  view source on GitHub ↗

Returns the base 10 logarithm of self.

(self, context=None)

Source from the content-addressed store, hash-verified

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

Callers 9

test_none_argsMethod · 0.95
repr_intMethod · 0.45
log10Method · 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 15

_check_nansMethod · 0.95
_isinfinityMethod · 0.95
_log10_exp_boundMethod · 0.95
_fixMethod · 0.95
getcontextFunction · 0.85
lenFunction · 0.85
DecimalClass · 0.85
_WorkRepClass · 0.85
_dlog10Function · 0.85
strFunction · 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