Returns the base 10 logarithm of self.
(self, context=None)
| 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's MSD. |