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

Method _rescale

Lib/_pydecimal.py:2577–2609  ·  view source on GitHub ↗

Rescale self so that the exponent is exp, either by padding with zeros or by truncating digits, using the given rounding mode. Specials are returned without change. This operation is quiet: it raises no flags, and uses no information from the context. exp =

(self, exp, rounding)

Source from the content-addressed store, hash-verified

2575 return self._exp == other._exp
2576
2577 def _rescale(self, exp, rounding):
2578 """Rescale self so that the exponent is exp, either by padding with zeros
2579 or by truncating digits, using the given rounding mode.
2580
2581 Specials are returned without change. This operation is
2582 quiet: it raises no flags, and uses no information from the
2583 context.
2584
2585 exp = exp to scale to (an integer)
2586 rounding = rounding mode
2587 """
2588 if self._is_special:
2589 return Decimal(self)
2590 if not self:
2591 return _dec_from_triple(self._sign, '0', exp)
2592
2593 if self._exp >= exp:
2594 # pad answer with zeros if necessary
2595 return _dec_from_triple(self._sign,
2596 self._int + '0'*(self._exp - exp), exp)
2597
2598 # too many digits; round and lose data. If self.adjusted() <
2599 # exp-1, replace self by 10**(exp-1) before rounding
2600 digits = len(self._int) + self._exp - exp
2601 if digits < 0:
2602 self = _dec_from_triple(self._sign, '1', exp-1)
2603 digits = 0
2604 this_function = self._pick_rounding_function[rounding]
2605 changed = this_function(self, digits)
2606 coeff = self._int[:digits] or '0'
2607 if changed == 1:
2608 coeff = str(int(coeff)+1)
2609 return _dec_from_triple(self._sign, coeff, exp)
2610
2611 def _round(self, places, rounding):
2612 """Round a nonzero, nonspecial Decimal to a fixed number of

Callers 12

__add__Method · 0.95
_divideMethod · 0.95
remainder_nearMethod · 0.95
__round__Method · 0.95
__floor__Method · 0.95
__ceil__Method · 0.95
quantizeMethod · 0.95
_roundMethod · 0.95
to_integral_exactMethod · 0.95
to_integral_valueMethod · 0.95
__format__Method · 0.95
test_py_rescaleMethod · 0.45

Calls 3

DecimalClass · 0.85
_dec_from_tripleFunction · 0.85
strFunction · 0.85

Tested by 1

test_py_rescaleMethod · 0.36