Round if it is necessary to keep self within prec precision. Rounds and fixes the exponent. Does not raise on a sNaN. Arguments: self - Decimal instance context - context used.
(self, context)
| 1613 | return Decimal(self) |
| 1614 | |
| 1615 | def _fix(self, context): |
| 1616 | """Round if it is necessary to keep self within prec precision. |
| 1617 | |
| 1618 | Rounds and fixes the exponent. Does not raise on a sNaN. |
| 1619 | |
| 1620 | Arguments: |
| 1621 | self - Decimal instance |
| 1622 | context - context used. |
| 1623 | """ |
| 1624 | |
| 1625 | if self._is_special: |
| 1626 | if self._isnan(): |
| 1627 | # decapitate payload if necessary |
| 1628 | return self._fix_nan(context) |
| 1629 | else: |
| 1630 | # self is +/-Infinity; return unaltered |
| 1631 | return Decimal(self) |
| 1632 | |
| 1633 | # if self is zero then exponent should be between Etiny and |
| 1634 | # Emax if clamp==0, and between Etiny and Etop if clamp==1. |
| 1635 | Etiny = context.Etiny() |
| 1636 | Etop = context.Etop() |
| 1637 | if not self: |
| 1638 | exp_max = [context.Emax, Etop][context.clamp] |
| 1639 | new_exp = min(max(self._exp, Etiny), exp_max) |
| 1640 | if new_exp != self._exp: |
| 1641 | context._raise_error(Clamped) |
| 1642 | return _dec_from_triple(self._sign, '0', new_exp) |
| 1643 | else: |
| 1644 | return Decimal(self) |
| 1645 | |
| 1646 | # exp_min is the smallest allowable exponent of the result, |
| 1647 | # equal to max(self.adjusted()-context.prec+1, Etiny) |
| 1648 | exp_min = len(self._int) + self._exp - context.prec |
| 1649 | if exp_min > Etop: |
| 1650 | # overflow: exp_min > Etop iff self.adjusted() > Emax |
| 1651 | ans = context._raise_error(Overflow, 'above Emax', self._sign) |
| 1652 | context._raise_error(Inexact) |
| 1653 | context._raise_error(Rounded) |
| 1654 | return ans |
| 1655 | |
| 1656 | self_is_subnormal = exp_min < Etiny |
| 1657 | if self_is_subnormal: |
| 1658 | exp_min = Etiny |
| 1659 | |
| 1660 | # round if self has too many digits |
| 1661 | if self._exp < exp_min: |
| 1662 | digits = len(self._int) + self._exp - exp_min |
| 1663 | if digits < 0: |
| 1664 | self = _dec_from_triple(self._sign, '1', exp_min-1) |
| 1665 | digits = 0 |
| 1666 | rounding_method = self._pick_rounding_function[context.rounding] |
| 1667 | changed = rounding_method(self, digits) |
| 1668 | coeff = self._int[:digits] or '0' |
| 1669 | if changed > 0: |
| 1670 | coeff = str(int(coeff)+1) |
| 1671 | if len(coeff) > context.prec: |
| 1672 | coeff = coeff[:-1] |
no test coverage detected