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)
| 1669 | return Decimal(self) |
| 1670 | |
| 1671 | def _fix(self, context): |
| 1672 | """Round if it is necessary to keep self within prec precision. |
| 1673 | |
| 1674 | Rounds and fixes the exponent. Does not raise on a sNaN. |
| 1675 | |
| 1676 | Arguments: |
| 1677 | self - Decimal instance |
| 1678 | context - context used. |
| 1679 | """ |
| 1680 | |
| 1681 | if self._is_special: |
| 1682 | if self._isnan(): |
| 1683 | # decapitate payload if necessary |
| 1684 | return self._fix_nan(context) |
| 1685 | else: |
| 1686 | # self is +/-Infinity; return unaltered |
| 1687 | return Decimal(self) |
| 1688 | |
| 1689 | # if self is zero then exponent should be between Etiny and |
| 1690 | # Emax if clamp==0, and between Etiny and Etop if clamp==1. |
| 1691 | Etiny = context.Etiny() |
| 1692 | Etop = context.Etop() |
| 1693 | if not self: |
| 1694 | exp_max = [context.Emax, Etop][context.clamp] |
| 1695 | new_exp = min(max(self._exp, Etiny), exp_max) |
| 1696 | if new_exp != self._exp: |
| 1697 | context._raise_error(Clamped) |
| 1698 | return _dec_from_triple(self._sign, '0', new_exp) |
| 1699 | else: |
| 1700 | return Decimal(self) |
| 1701 | |
| 1702 | # exp_min is the smallest allowable exponent of the result, |
| 1703 | # equal to max(self.adjusted()-context.prec+1, Etiny) |
| 1704 | exp_min = len(self._int) + self._exp - context.prec |
| 1705 | if exp_min > Etop: |
| 1706 | # overflow: exp_min > Etop iff self.adjusted() > Emax |
| 1707 | ans = context._raise_error(Overflow, 'above Emax', self._sign) |
| 1708 | context._raise_error(Inexact) |
| 1709 | context._raise_error(Rounded) |
| 1710 | return ans |
| 1711 | |
| 1712 | self_is_subnormal = exp_min < Etiny |
| 1713 | if self_is_subnormal: |
| 1714 | exp_min = Etiny |
| 1715 | |
| 1716 | # round if self has too many digits |
| 1717 | if self._exp < exp_min: |
| 1718 | digits = len(self._int) + self._exp - exp_min |
| 1719 | if digits < 0: |
| 1720 | self = _dec_from_triple(self._sign, '1', exp_min-1) |
| 1721 | digits = 0 |
| 1722 | rounding_method = self._pick_rounding_function[context.rounding] |
| 1723 | changed = rounding_method(self, digits) |
| 1724 | coeff = self._int[:digits] or '0' |
| 1725 | if changed > 0: |
| 1726 | coeff = str(int(coeff)+1) |
| 1727 | if len(coeff) > context.prec: |
| 1728 | coeff = coeff[:-1] |
no test coverage detected