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

Method _rescale

Lib/_pydecimal.py:2574–2606  ·  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

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

Calls 4

DecimalClass · 0.85
_dec_from_tripleFunction · 0.85
lenFunction · 0.85
strFunction · 0.85

Tested by 1

test_py_rescaleMethod · 0.64