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

Method _fix

Lib/_pydecimal.py:1612–1702  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 15

__pos__Method · 0.95
__add__Method · 0.95
remainder_nearMethod · 0.95
normalizeMethod · 0.95
maxMethod · 0.95
minMethod · 0.95
log10Method · 0.95
logbMethod · 0.95
max_magMethod · 0.95
min_magMethod · 0.95
next_minusMethod · 0.95
next_plusMethod · 0.95

Calls 11

_isnanMethod · 0.95
_fix_nanMethod · 0.95
DecimalClass · 0.85
minFunction · 0.85
maxFunction · 0.85
_dec_from_tripleFunction · 0.85
lenFunction · 0.85
strFunction · 0.85
EtinyMethod · 0.80
EtopMethod · 0.80
_raise_errorMethod · 0.80

Tested by

no test coverage detected