'Fix the exponents and return a copy with the exponent in bounds.
Only call if known to not be a special value.'

def _fixexponents(self, context):

    folddown = context._clamp
    Emin = context.Emin
    ans = self
    ans_adjusted = ans.adjusted()
    if (ans_adjusted < Emin):
        Etiny = context.Etiny()
        if (ans._exp < Etiny):
            if (not ans):
                ans = Decimal(self)
                ans._exp = Etiny
                context._raise_error(Clamped)
                return ans
            ans = ans._rescale(Etiny, context=context)
            context._raise_error(Subnormal)
            if context.flags[Inexact]:
                context._raise_error(Underflow)
        elif ans:
            context._raise_error(Subnormal)
    else:
        Etop = context.Etop()
        if (folddown and (ans._exp > Etop)):
            context._raise_error(Clamped)
            ans = ans._rescale(Etop, context=context)
        else:
            Emax = context.Emax
            if (ans_adjusted > Emax):
                if (not ans):
                    ans = Decimal(self)
                    ans._exp = Emax
                    context._raise_error(Clamped)
                    return ans
                context._raise_error(Inexact)
                context._raise_error(Rounded)
                return context._raise_error(Overflow, 'above    Emax', ans._sign)
    return ans
