(self, digits=None)
return num, exp10
Return value as mantissa and decimal exponent.
Return value as mantissa and decimal exponent.
Return value as mantissa and decimal exponent .
def to_decimal(self, digits=None):
        """Return value as mantissa and decimal exponent."""
        if digits is None:
            # we'd be better off storing these (and self._ten) in denormalised form
            lim_bot = self.new().from_bytes(self._lim_bot)
            lim_top = self.new().from_bytes(self._lim_top)
            digits = self.digits
        elif digits > 0:
            lim_bot = self.new().from_int(10**(digits-1))._just_under()
            lim_top = self.new().from_int(10**digits)._just_under()
        else:
            return 0, 0
        tden = lim_top._denormalise()
        bden = lim_bot._denormalise()
        exp10 = 0
        den = self._denormalise()
        while self._abs_gt_den(den, tden):
            den = self._div10_den(den)
            exp10 += 1
        # rounding - gets us close to GW results
        den = self._apply_carry_den(den)
        while self._abs_gt_den(bden, den):
            den = self._mul10_den(den)
            exp10 -= 1
        # rounding - gets us close to GW results
        den = self._apply_carry_den(den)
        # round to int
        exp, man, neg = den
        exp -= self._bias
        if exp > 0:
            man <<= exp
        else:
            man >>= -exp
        # here we don't care about overflowing the mantissa as we output it as int
        if man & 0x80:
            man += 0x80
        num = -(man >> 8) if neg else man >> 8
        return num, exp10
def to_decimal ( self , digits = None ) : if digits is None : # we'd be better off storing these (and self._ten) in denormalised form lim_bot = self . new ( ) . from_bytes ( self . _lim_bot ) lim_top = self . new ( ) . from_bytes ( self . _lim_top ) digits = self . digits elif digits > 0 : lim_bot = self . new ( ) . from_int ( 10 ** ( digits - 1 ) ) . _just_under ( ) lim_top = self . new ( ) . from_int ( 10 ** digits ) . _just_under ( ) else : return 0 , 0 tden = lim_top . _denormalise ( ) bden = lim_bot . _denormalise ( ) exp10 = 0 den = self . _denormalise ( ) while self . _abs_gt_den ( den , tden ) : den = self . _div10_den ( den ) exp10 += 1 # rounding - gets us close to GW results den = self . _apply_carry_den ( den ) while self . _abs_gt_den ( bden , den ) : den = self . _mul10_den ( den ) exp10 -= 1 # rounding - gets us close to GW results den = self . _apply_carry_den ( den ) # round to int exp , man , neg = den exp -= self . _bias if exp > 0 : man <<= exp else : man >>= - exp # here we don't care about overflowing the mantissa as we output it as int if man & 0x80 : man += 0x80 num = - ( man >> 8 ) if neg else man >> 8 return num , exp10