<s> """<STR_LIT>""" <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> __version__ = '<STR_LIT>' <EOL> import copy as _copy <EOL> import math as _math <EOL> import numbers as _numbers <EOL> try : <EOL> from collections import namedtuple as _namedtuple <EOL> DecimalTuple = _namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except ImportError : <EOL> DecimalTuple = lambda * args : args <EOL> ROUND_DOWN = '<STR_LIT>' <EOL> ROUND_HALF_UP = '<STR_LIT>' <EOL> ROUND_HALF_EVEN = '<STR_LIT>' <EOL> ROUND_CEILING = '<STR_LIT>' <EOL> ROUND_FLOOR = '<STR_LIT>' <EOL> ROUND_UP = '<STR_LIT>' <EOL> ROUND_HALF_DOWN = '<STR_LIT>' <EOL> ROUND_05UP = '<STR_LIT>' <EOL> class DecimalException ( ArithmeticError ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self , context , * args ) : <EOL> pass <EOL> class Clamped ( DecimalException ) : <EOL> """<STR_LIT>""" <EOL> class InvalidOperation ( DecimalException ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self , context , * args ) : <EOL> if args : <EOL> ans = _dec_from_triple ( args [ <NUM_LIT:0> ] . _sign , args [ <NUM_LIT:0> ] . _int , '<STR_LIT:n>' , True ) <EOL> return ans . _fix_nan ( context ) <EOL> return _NaN <EOL> class ConversionSyntax ( InvalidOperation ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self , context , * args ) : <EOL> return _NaN <EOL> class DivisionByZero ( DecimalException , ZeroDivisionError ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self , context , sign , * args ) : <EOL> return _SignedInfinity [ sign ] <EOL> class DivisionImpossible ( InvalidOperation ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self , context , * args ) : <EOL> return _NaN <EOL> class DivisionUndefined ( InvalidOperation , ZeroDivisionError ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self , context , * args ) : <EOL> return _NaN <EOL> class Inexact ( DecimalException ) : <EOL> """<STR_LIT>""" <EOL> class InvalidContext ( InvalidOperation ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self , context , * args ) : <EOL> return _NaN <EOL> class Rounded ( DecimalException ) : <EOL> """<STR_LIT>""" <EOL> class Subnormal ( DecimalException ) : <EOL> """<STR_LIT>""" <EOL> class Overflow ( Inexact , Rounded ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self , context , sign , * args ) : <EOL> if context . rounding in ( ROUND_HALF_UP , ROUND_HALF_EVEN , <EOL> ROUND_HALF_DOWN , ROUND_UP ) : <EOL> return _SignedInfinity [ sign ] <EOL> if sign == <NUM_LIT:0> : <EOL> if context . rounding == ROUND_CEILING : <EOL> return _SignedInfinity [ sign ] <EOL> return _dec_from_triple ( sign , '<STR_LIT>' * context . prec , <EOL> context . Emax - context . prec + <NUM_LIT:1> ) <EOL> if sign == <NUM_LIT:1> : <EOL> if context . rounding == ROUND_FLOOR : <EOL> return _SignedInfinity [ sign ] <EOL> return _dec_from_triple ( sign , '<STR_LIT>' * context . prec , <EOL> context . Emax - context . prec + <NUM_LIT:1> ) <EOL> class Underflow ( Inexact , Rounded , Subnormal ) : <EOL> """<STR_LIT>""" <EOL> _signals = [ Clamped , DivisionByZero , Inexact , Overflow , Rounded , <EOL> Underflow , InvalidOperation , Subnormal ] <EOL> _condition_map = { ConversionSyntax : InvalidOperation , <EOL> DivisionImpossible : InvalidOperation , <EOL> DivisionUndefined : InvalidOperation , <EOL> InvalidContext : InvalidOperation } <EOL> try : <EOL> import threading <EOL> except ImportError : <EOL> import sys <EOL> class MockThreading ( object ) : <EOL> def local ( self , sys = sys ) : <EOL> return sys . modules [ __name__ ] <EOL> threading = MockThreading ( ) <EOL> del sys , MockThreading <EOL> try : <EOL> threading . local <EOL> except AttributeError : <EOL> if hasattr ( threading . currentThread ( ) , '<STR_LIT>' ) : <EOL> del threading . currentThread ( ) . __decimal_context__ <EOL> def setcontext ( context ) : <EOL> """<STR_LIT>""" <EOL> if context in ( DefaultContext , BasicContext , ExtendedContext ) : <EOL> context = context . copy ( ) <EOL> context . clear_flags ( ) <EOL> threading . currentThread ( ) . __decimal_context__ = context <EOL> def getcontext ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return threading . currentThread ( ) . __decimal_context__ <EOL> except AttributeError : <EOL> context = Context ( ) <EOL> threading . currentThread ( ) . __decimal_context__ = context <EOL> return context <EOL> else : <EOL> local = threading . local ( ) <EOL> if hasattr ( local , '<STR_LIT>' ) : <EOL> del local . __decimal_context__ <EOL> def getcontext ( _local = local ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return _local . __decimal_context__ <EOL> except AttributeError : <EOL> context = Context ( ) <EOL> _local . __decimal_context__ = context <EOL> return context <EOL> def setcontext ( context , _local = local ) : <EOL> """<STR_LIT>""" <EOL> if context in ( DefaultContext , BasicContext , ExtendedContext ) : <EOL> context = context . copy ( ) <EOL> context . clear_flags ( ) <EOL> _local . __decimal_context__ = context <EOL> del threading , local <EOL> def localcontext ( ctx = None ) : <EOL> """<STR_LIT>""" <EOL> if ctx is None : ctx = getcontext ( ) <EOL> return _ContextManager ( ctx ) <EOL> class Decimal ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __new__ ( cls , value = "<STR_LIT:0>" , context = None ) : <EOL> """<STR_LIT>""" <EOL> self = object . __new__ ( cls ) <EOL> if isinstance ( value , basestring ) : <EOL> m = _parser ( value . strip ( ) ) <EOL> if m is None : <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> return context . _raise_error ( ConversionSyntax , <EOL> "<STR_LIT>" % value ) <EOL> if m . group ( '<STR_LIT>' ) == "<STR_LIT:->" : <EOL> self . _sign = <NUM_LIT:1> <EOL> else : <EOL> self . _sign = <NUM_LIT:0> <EOL> intpart = m . group ( '<STR_LIT:int>' ) <EOL> if intpart is not None : <EOL> fracpart = m . group ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> exp = int ( m . group ( '<STR_LIT>' ) or '<STR_LIT:0>' ) <EOL> self . _int = str ( int ( intpart + fracpart ) ) <EOL> self . _exp = exp - len ( fracpart ) <EOL> self . _is_special = False <EOL> else : <EOL> diag = m . group ( '<STR_LIT>' ) <EOL> if diag is not None : <EOL> self . _int = str ( int ( diag or '<STR_LIT:0>' ) ) . lstrip ( '<STR_LIT:0>' ) <EOL> if m . group ( '<STR_LIT>' ) : <EOL> self . _exp = '<STR_LIT:N>' <EOL> else : <EOL> self . _exp = '<STR_LIT:n>' <EOL> else : <EOL> self . _int = '<STR_LIT:0>' <EOL> self . _exp = '<STR_LIT:F>' <EOL> self . _is_special = True <EOL> return self <EOL> if isinstance ( value , ( int , long ) ) : <EOL> if value >= <NUM_LIT:0> : <EOL> self . _sign = <NUM_LIT:0> <EOL> else : <EOL> self . _sign = <NUM_LIT:1> <EOL> self . _exp = <NUM_LIT:0> <EOL> self . _int = str ( abs ( value ) ) <EOL> self . _is_special = False <EOL> return self <EOL> if isinstance ( value , Decimal ) : <EOL> self . _exp = value . _exp <EOL> self . _sign = value . _sign <EOL> self . _int = value . _int <EOL> self . _is_special = value . _is_special <EOL> return self <EOL> if isinstance ( value , _WorkRep ) : <EOL> self . _sign = value . sign <EOL> self . _int = str ( value . int ) <EOL> self . _exp = int ( value . exp ) <EOL> self . _is_special = False <EOL> return self <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> if len ( value ) != <NUM_LIT:3> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if not ( isinstance ( value [ <NUM_LIT:0> ] , ( int , long ) ) and value [ <NUM_LIT:0> ] in ( <NUM_LIT:0> , <NUM_LIT:1> ) ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _sign = value [ <NUM_LIT:0> ] <EOL> if value [ <NUM_LIT:2> ] == '<STR_LIT:F>' : <EOL> self . _int = '<STR_LIT:0>' <EOL> self . _exp = value [ <NUM_LIT:2> ] <EOL> self . _is_special = True <EOL> else : <EOL> digits = [ ] <EOL> for digit in value [ <NUM_LIT:1> ] : <EOL> if isinstance ( digit , ( int , long ) ) and <NUM_LIT:0> <= digit <= <NUM_LIT:9> : <EOL> if digits or digit != <NUM_LIT:0> : <EOL> digits . append ( digit ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if value [ <NUM_LIT:2> ] in ( '<STR_LIT:n>' , '<STR_LIT:N>' ) : <EOL> self . _int = '<STR_LIT>' . join ( map ( str , digits ) ) <EOL> self . _exp = value [ <NUM_LIT:2> ] <EOL> self . _is_special = True <EOL> elif isinstance ( value [ <NUM_LIT:2> ] , ( int , long ) ) : <EOL> self . _int = '<STR_LIT>' . join ( map ( str , digits or [ <NUM_LIT:0> ] ) ) <EOL> self . _exp = value [ <NUM_LIT:2> ] <EOL> self . _is_special = False <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return self <EOL> if isinstance ( value , float ) : <EOL> value = Decimal . from_float ( value ) <EOL> self . _exp = value . _exp <EOL> self . _sign = value . _sign <EOL> self . _int = value . _int <EOL> self . _is_special = value . _is_special <EOL> return self <EOL> raise TypeError ( "<STR_LIT>" % value ) <EOL> def from_float ( cls , f ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( f , ( int , long ) ) : <EOL> return cls ( f ) <EOL> if _math . isinf ( f ) or _math . isnan ( f ) : <EOL> return cls ( repr ( f ) ) <EOL> if _math . copysign ( <NUM_LIT:1.0> , f ) == <NUM_LIT:1.0> : <EOL> sign = <NUM_LIT:0> <EOL> else : <EOL> sign = <NUM_LIT:1> <EOL> n , d = abs ( f ) . as_integer_ratio ( ) <EOL> k = d . bit_length ( ) - <NUM_LIT:1> <EOL> result = _dec_from_triple ( sign , str ( n * <NUM_LIT:5> ** k ) , - k ) <EOL> if cls is Decimal : <EOL> return result <EOL> else : <EOL> return cls ( result ) <EOL> from_float = classmethod ( from_float ) <EOL> def _isnan ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special : <EOL> exp = self . _exp <EOL> if exp == '<STR_LIT:n>' : <EOL> return <NUM_LIT:1> <EOL> elif exp == '<STR_LIT:N>' : <EOL> return <NUM_LIT:2> <EOL> return <NUM_LIT:0> <EOL> def _isinfinity ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _exp == '<STR_LIT:F>' : <EOL> if self . _sign : <EOL> return - <NUM_LIT:1> <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def _check_nans ( self , other = None , context = None ) : <EOL> """<STR_LIT>""" <EOL> self_is_nan = self . _isnan ( ) <EOL> if other is None : <EOL> other_is_nan = False <EOL> else : <EOL> other_is_nan = other . _isnan ( ) <EOL> if self_is_nan or other_is_nan : <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self_is_nan == <NUM_LIT:2> : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' , <EOL> self ) <EOL> if other_is_nan == <NUM_LIT:2> : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' , <EOL> other ) <EOL> if self_is_nan : <EOL> return self . _fix_nan ( context ) <EOL> return other . _fix_nan ( context ) <EOL> return <NUM_LIT:0> <EOL> def _compare_check_nans ( self , other , context ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . _is_special or other . _is_special : <EOL> if self . is_snan ( ) : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' , <EOL> self ) <EOL> elif other . is_snan ( ) : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' , <EOL> other ) <EOL> elif self . is_qnan ( ) : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' , <EOL> self ) <EOL> elif other . is_qnan ( ) : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' , <EOL> other ) <EOL> return <NUM_LIT:0> <EOL> def __nonzero__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _is_special or self . _int != '<STR_LIT:0>' <EOL> def _cmp ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special or other . _is_special : <EOL> self_inf = self . _isinfinity ( ) <EOL> other_inf = other . _isinfinity ( ) <EOL> if self_inf == other_inf : <EOL> return <NUM_LIT:0> <EOL> elif self_inf < other_inf : <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:1> <EOL> if not self : <EOL> if not other : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return - ( ( - <NUM_LIT:1> ) ** other . _sign ) <EOL> if not other : <EOL> return ( - <NUM_LIT:1> ) ** self . _sign <EOL> if other . _sign < self . _sign : <EOL> return - <NUM_LIT:1> <EOL> if self . _sign < other . _sign : <EOL> return <NUM_LIT:1> <EOL> self_adjusted = self . adjusted ( ) <EOL> other_adjusted = other . adjusted ( ) <EOL> if self_adjusted == other_adjusted : <EOL> self_padded = self . _int + '<STR_LIT:0>' * ( self . _exp - other . _exp ) <EOL> other_padded = other . _int + '<STR_LIT:0>' * ( other . _exp - self . _exp ) <EOL> if self_padded == other_padded : <EOL> return <NUM_LIT:0> <EOL> elif self_padded < other_padded : <EOL> return - ( - <NUM_LIT:1> ) ** self . _sign <EOL> else : <EOL> return ( - <NUM_LIT:1> ) ** self . _sign <EOL> elif self_adjusted > other_adjusted : <EOL> return ( - <NUM_LIT:1> ) ** self . _sign <EOL> else : <EOL> return - ( ( - <NUM_LIT:1> ) ** self . _sign ) <EOL> def __eq__ ( self , other , context = None ) : <EOL> other = _convert_other ( other , allow_float = True ) <EOL> if other is NotImplemented : <EOL> return other <EOL> if self . _check_nans ( other , context ) : <EOL> return False <EOL> return self . _cmp ( other ) == <NUM_LIT:0> <EOL> def __ne__ ( self , other , context = None ) : <EOL> other = _convert_other ( other , allow_float = True ) <EOL> if other is NotImplemented : <EOL> return other <EOL> if self . _check_nans ( other , context ) : <EOL> return True <EOL> return self . _cmp ( other ) != <NUM_LIT:0> <EOL> def __lt__ ( self , other , context = None ) : <EOL> other = _convert_other ( other , allow_float = True ) <EOL> if other is NotImplemented : <EOL> return other <EOL> ans = self . _compare_check_nans ( other , context ) <EOL> if ans : <EOL> return False <EOL> return self . _cmp ( other ) < <NUM_LIT:0> <EOL> def __le__ ( self , other , context = None ) : <EOL> other = _convert_other ( other , allow_float = True ) <EOL> if other is NotImplemented : <EOL> return other <EOL> ans = self . _compare_check_nans ( other , context ) <EOL> if ans : <EOL> return False <EOL> return self . _cmp ( other ) <= <NUM_LIT:0> <EOL> def __gt__ ( self , other , context = None ) : <EOL> other = _convert_other ( other , allow_float = True ) <EOL> if other is NotImplemented : <EOL> return other <EOL> ans = self . _compare_check_nans ( other , context ) <EOL> if ans : <EOL> return False <EOL> return self . _cmp ( other ) > <NUM_LIT:0> <EOL> def __ge__ ( self , other , context = None ) : <EOL> other = _convert_other ( other , allow_float = True ) <EOL> if other is NotImplemented : <EOL> return other <EOL> ans = self . _compare_check_nans ( other , context ) <EOL> if ans : <EOL> return False <EOL> return self . _cmp ( other ) >= <NUM_LIT:0> <EOL> def compare ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if ( self . _is_special or other and other . _is_special ) : <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> return Decimal ( self . _cmp ( other ) ) <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special : <EOL> if self . is_snan ( ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> elif self . is_nan ( ) : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> if self . _sign : <EOL> return - <NUM_LIT> <EOL> else : <EOL> return <NUM_LIT> <EOL> self_as_float = float ( self ) <EOL> if Decimal . from_float ( self_as_float ) == self : <EOL> return hash ( self_as_float ) <EOL> if self . _isinteger ( ) : <EOL> op = _WorkRep ( self . to_integral_value ( ) ) <EOL> return hash ( ( - <NUM_LIT:1> ) ** op . sign * op . int * pow ( <NUM_LIT:10> , op . exp , <NUM_LIT:2> ** <NUM_LIT:64> - <NUM_LIT:1> ) ) <EOL> return hash ( ( self . _sign , <EOL> self . _exp + len ( self . _int ) , <EOL> self . _int . rstrip ( '<STR_LIT:0>' ) ) ) <EOL> def as_tuple ( self ) : <EOL> """<STR_LIT>""" <EOL> return DecimalTuple ( self . _sign , tuple ( map ( int , self . _int ) ) , self . _exp ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % str ( self ) <EOL> def __str__ ( self , eng = False , context = None ) : <EOL> """<STR_LIT>""" <EOL> sign = [ '<STR_LIT>' , '<STR_LIT:->' ] [ self . _sign ] <EOL> if self . _is_special : <EOL> if self . _exp == '<STR_LIT:F>' : <EOL> return sign + '<STR_LIT>' <EOL> elif self . _exp == '<STR_LIT:n>' : <EOL> return sign + '<STR_LIT>' + self . _int <EOL> else : <EOL> return sign + '<STR_LIT>' + self . _int <EOL> leftdigits = self . _exp + len ( self . _int ) <EOL> if self . _exp <= <NUM_LIT:0> and leftdigits > - <NUM_LIT:6> : <EOL> dotplace = leftdigits <EOL> elif not eng : <EOL> dotplace = <NUM_LIT:1> <EOL> elif self . _int == '<STR_LIT:0>' : <EOL> dotplace = ( leftdigits + <NUM_LIT:1> ) % <NUM_LIT:3> - <NUM_LIT:1> <EOL> else : <EOL> dotplace = ( leftdigits - <NUM_LIT:1> ) % <NUM_LIT:3> + <NUM_LIT:1> <EOL> if dotplace <= <NUM_LIT:0> : <EOL> intpart = '<STR_LIT:0>' <EOL> fracpart = '<STR_LIT:.>' + '<STR_LIT:0>' * ( - dotplace ) + self . _int <EOL> elif dotplace >= len ( self . _int ) : <EOL> intpart = self . _int + '<STR_LIT:0>' * ( dotplace - len ( self . _int ) ) <EOL> fracpart = '<STR_LIT>' <EOL> else : <EOL> intpart = self . _int [ : dotplace ] <EOL> fracpart = '<STR_LIT:.>' + self . _int [ dotplace : ] <EOL> if leftdigits == dotplace : <EOL> exp = '<STR_LIT>' <EOL> else : <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> exp = [ '<STR_LIT:e>' , '<STR_LIT:E>' ] [ context . capitals ] + "<STR_LIT>" % ( leftdigits - dotplace ) <EOL> return sign + intpart + fracpart + exp <EOL> def to_eng_string ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> return self . __str__ ( eng = True , context = context ) <EOL> def __neg__ ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special : <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if not self and context . rounding != ROUND_FLOOR : <EOL> ans = self . copy_abs ( ) <EOL> else : <EOL> ans = self . copy_negate ( ) <EOL> return ans . _fix ( context ) <EOL> def __pos__ ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special : <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if not self and context . rounding != ROUND_FLOOR : <EOL> ans = self . copy_abs ( ) <EOL> else : <EOL> ans = Decimal ( self ) <EOL> return ans . _fix ( context ) <EOL> def __abs__ ( self , round = True , context = None ) : <EOL> """<STR_LIT>""" <EOL> if not round : <EOL> return self . copy_abs ( ) <EOL> if self . _is_special : <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> if self . _sign : <EOL> ans = self . __neg__ ( context = context ) <EOL> else : <EOL> ans = self . __pos__ ( context = context ) <EOL> return ans <EOL> def __add__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . _is_special or other . _is_special : <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> if self . _isinfinity ( ) : <EOL> if self . _sign != other . _sign and other . _isinfinity ( ) : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' ) <EOL> return Decimal ( self ) <EOL> if other . _isinfinity ( ) : <EOL> return Decimal ( other ) <EOL> exp = min ( self . _exp , other . _exp ) <EOL> negativezero = <NUM_LIT:0> <EOL> if context . rounding == ROUND_FLOOR and self . _sign != other . _sign : <EOL> negativezero = <NUM_LIT:1> <EOL> if not self and not other : <EOL> sign = min ( self . _sign , other . _sign ) <EOL> if negativezero : <EOL> sign = <NUM_LIT:1> <EOL> ans = _dec_from_triple ( sign , '<STR_LIT:0>' , exp ) <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> if not self : <EOL> exp = max ( exp , other . _exp - context . prec - <NUM_LIT:1> ) <EOL> ans = other . _rescale ( exp , context . rounding ) <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> if not other : <EOL> exp = max ( exp , self . _exp - context . prec - <NUM_LIT:1> ) <EOL> ans = self . _rescale ( exp , context . rounding ) <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> op1 = _WorkRep ( self ) <EOL> op2 = _WorkRep ( other ) <EOL> op1 , op2 = _normalize ( op1 , op2 , context . prec ) <EOL> result = _WorkRep ( ) <EOL> if op1 . sign != op2 . sign : <EOL> if op1 . int == op2 . int : <EOL> ans = _dec_from_triple ( negativezero , '<STR_LIT:0>' , exp ) <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> if op1 . int < op2 . int : <EOL> op1 , op2 = op2 , op1 <EOL> if op1 . sign == <NUM_LIT:1> : <EOL> result . sign = <NUM_LIT:1> <EOL> op1 . sign , op2 . sign = op2 . sign , op1 . sign <EOL> else : <EOL> result . sign = <NUM_LIT:0> <EOL> elif op1 . sign == <NUM_LIT:1> : <EOL> result . sign = <NUM_LIT:1> <EOL> op1 . sign , op2 . sign = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> else : <EOL> result . sign = <NUM_LIT:0> <EOL> if op2 . sign == <NUM_LIT:0> : <EOL> result . int = op1 . int + op2 . int <EOL> else : <EOL> result . int = op1 . int - op2 . int <EOL> result . exp = op1 . exp <EOL> ans = Decimal ( result ) <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> __radd__ = __add__ <EOL> def __sub__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> if self . _is_special or other . _is_special : <EOL> ans = self . _check_nans ( other , context = context ) <EOL> if ans : <EOL> return ans <EOL> return self . __add__ ( other . copy_negate ( ) , context = context ) <EOL> def __rsub__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> return other . __sub__ ( self , context = context ) <EOL> def __mul__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> resultsign = self . _sign ^ other . _sign <EOL> if self . _is_special or other . _is_special : <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> if self . _isinfinity ( ) : <EOL> if not other : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' ) <EOL> return _SignedInfinity [ resultsign ] <EOL> if other . _isinfinity ( ) : <EOL> if not self : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' ) <EOL> return _SignedInfinity [ resultsign ] <EOL> resultexp = self . _exp + other . _exp <EOL> if not self or not other : <EOL> ans = _dec_from_triple ( resultsign , '<STR_LIT:0>' , resultexp ) <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> if self . _int == '<STR_LIT:1>' : <EOL> ans = _dec_from_triple ( resultsign , other . _int , resultexp ) <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> if other . _int == '<STR_LIT:1>' : <EOL> ans = _dec_from_triple ( resultsign , self . _int , resultexp ) <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> op1 = _WorkRep ( self ) <EOL> op2 = _WorkRep ( other ) <EOL> ans = _dec_from_triple ( resultsign , str ( op1 . int * op2 . int ) , resultexp ) <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> __rmul__ = __mul__ <EOL> def __truediv__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return NotImplemented <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> sign = self . _sign ^ other . _sign <EOL> if self . _is_special or other . _is_special : <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> if self . _isinfinity ( ) and other . _isinfinity ( ) : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' ) <EOL> if self . _isinfinity ( ) : <EOL> return _SignedInfinity [ sign ] <EOL> if other . _isinfinity ( ) : <EOL> context . _raise_error ( Clamped , '<STR_LIT>' ) <EOL> return _dec_from_triple ( sign , '<STR_LIT:0>' , context . Etiny ( ) ) <EOL> if not other : <EOL> if not self : <EOL> return context . _raise_error ( DivisionUndefined , '<STR_LIT>' ) <EOL> return context . _raise_error ( DivisionByZero , '<STR_LIT>' , sign ) <EOL> if not self : <EOL> exp = self . _exp - other . _exp <EOL> coeff = <NUM_LIT:0> <EOL> else : <EOL> shift = len ( other . _int ) - len ( self . _int ) + context . prec + <NUM_LIT:1> <EOL> exp = self . _exp - other . _exp - shift <EOL> op1 = _WorkRep ( self ) <EOL> op2 = _WorkRep ( other ) <EOL> if shift >= <NUM_LIT:0> : <EOL> coeff , remainder = divmod ( op1 . int * <NUM_LIT:10> ** shift , op2 . int ) <EOL> else : <EOL> coeff , remainder = divmod ( op1 . int , op2 . int * <NUM_LIT:10> ** - shift ) <EOL> if remainder : <EOL> if coeff % <NUM_LIT:5> == <NUM_LIT:0> : <EOL> coeff += <NUM_LIT:1> <EOL> else : <EOL> ideal_exp = self . _exp - other . _exp <EOL> while exp < ideal_exp and coeff % <NUM_LIT:10> == <NUM_LIT:0> : <EOL> coeff //= <NUM_LIT:10> <EOL> exp += <NUM_LIT:1> <EOL> ans = _dec_from_triple ( sign , str ( coeff ) , exp ) <EOL> return ans . _fix ( context ) <EOL> def _divide ( self , other , context ) : <EOL> """<STR_LIT>""" <EOL> sign = self . _sign ^ other . _sign <EOL> if other . _isinfinity ( ) : <EOL> ideal_exp = self . _exp <EOL> else : <EOL> ideal_exp = min ( self . _exp , other . _exp ) <EOL> expdiff = self . adjusted ( ) - other . adjusted ( ) <EOL> if not self or other . _isinfinity ( ) or expdiff <= - <NUM_LIT:2> : <EOL> return ( _dec_from_triple ( sign , '<STR_LIT:0>' , <NUM_LIT:0> ) , <EOL> self . _rescale ( ideal_exp , context . rounding ) ) <EOL> if expdiff <= context . prec : <EOL> op1 = _WorkRep ( self ) <EOL> op2 = _WorkRep ( other ) <EOL> if op1 . exp >= op2 . exp : <EOL> op1 . int *= <NUM_LIT:10> ** ( op1 . exp - op2 . exp ) <EOL> else : <EOL> op2 . int *= <NUM_LIT:10> ** ( op2 . exp - op1 . exp ) <EOL> q , r = divmod ( op1 . int , op2 . int ) <EOL> if q < <NUM_LIT:10> ** context . prec : <EOL> return ( _dec_from_triple ( sign , str ( q ) , <NUM_LIT:0> ) , <EOL> _dec_from_triple ( self . _sign , str ( r ) , ideal_exp ) ) <EOL> ans = context . _raise_error ( DivisionImpossible , <EOL> '<STR_LIT>' ) <EOL> return ans , ans <EOL> def __rtruediv__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> return other . __truediv__ ( self , context = context ) <EOL> __div__ = __truediv__ <EOL> __rdiv__ = __rtruediv__ <EOL> def __divmod__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ( ans , ans ) <EOL> sign = self . _sign ^ other . _sign <EOL> if self . _isinfinity ( ) : <EOL> if other . _isinfinity ( ) : <EOL> ans = context . _raise_error ( InvalidOperation , '<STR_LIT>' ) <EOL> return ans , ans <EOL> else : <EOL> return ( _SignedInfinity [ sign ] , <EOL> context . _raise_error ( InvalidOperation , '<STR_LIT>' ) ) <EOL> if not other : <EOL> if not self : <EOL> ans = context . _raise_error ( DivisionUndefined , '<STR_LIT>' ) <EOL> return ans , ans <EOL> else : <EOL> return ( context . _raise_error ( DivisionByZero , '<STR_LIT>' , sign ) , <EOL> context . _raise_error ( InvalidOperation , '<STR_LIT>' ) ) <EOL> quotient , remainder = self . _divide ( other , context ) <EOL> remainder = remainder . _fix ( context ) <EOL> return quotient , remainder <EOL> def __rdivmod__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> return other . __divmod__ ( self , context = context ) <EOL> def __mod__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> if self . _isinfinity ( ) : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' ) <EOL> elif not other : <EOL> if self : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' ) <EOL> else : <EOL> return context . _raise_error ( DivisionUndefined , '<STR_LIT>' ) <EOL> remainder = self . _divide ( other , context ) [ <NUM_LIT:1> ] <EOL> remainder = remainder . _fix ( context ) <EOL> return remainder <EOL> def __rmod__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> return other . __mod__ ( self , context = context ) <EOL> def remainder_near ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> other = _convert_other ( other , raiseit = True ) <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> if self . _isinfinity ( ) : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> if not other : <EOL> if self : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> else : <EOL> return context . _raise_error ( DivisionUndefined , <EOL> '<STR_LIT>' ) <EOL> if other . _isinfinity ( ) : <EOL> ans = Decimal ( self ) <EOL> return ans . _fix ( context ) <EOL> ideal_exponent = min ( self . _exp , other . _exp ) <EOL> if not self : <EOL> ans = _dec_from_triple ( self . _sign , '<STR_LIT:0>' , ideal_exponent ) <EOL> return ans . _fix ( context ) <EOL> expdiff = self . adjusted ( ) - other . adjusted ( ) <EOL> if expdiff >= context . prec + <NUM_LIT:1> : <EOL> return context . _raise_error ( DivisionImpossible ) <EOL> if expdiff <= - <NUM_LIT:2> : <EOL> ans = self . _rescale ( ideal_exponent , context . rounding ) <EOL> return ans . _fix ( context ) <EOL> op1 = _WorkRep ( self ) <EOL> op2 = _WorkRep ( other ) <EOL> if op1 . exp >= op2 . exp : <EOL> op1 . int *= <NUM_LIT:10> ** ( op1 . exp - op2 . exp ) <EOL> else : <EOL> op2 . int *= <NUM_LIT:10> ** ( op2 . exp - op1 . exp ) <EOL> q , r = divmod ( op1 . int , op2 . int ) <EOL> if <NUM_LIT:2> * r + ( q & <NUM_LIT:1> ) > op2 . int : <EOL> r -= op2 . int <EOL> q += <NUM_LIT:1> <EOL> if q >= <NUM_LIT:10> ** context . prec : <EOL> return context . _raise_error ( DivisionImpossible ) <EOL> sign = self . _sign <EOL> if r < <NUM_LIT:0> : <EOL> sign = <NUM_LIT:1> - sign <EOL> r = - r <EOL> ans = _dec_from_triple ( sign , str ( r ) , ideal_exponent ) <EOL> return ans . _fix ( context ) <EOL> def __floordiv__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> if self . _isinfinity ( ) : <EOL> if other . _isinfinity ( ) : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' ) <EOL> else : <EOL> return _SignedInfinity [ self . _sign ^ other . _sign ] <EOL> if not other : <EOL> if self : <EOL> return context . _raise_error ( DivisionByZero , '<STR_LIT>' , <EOL> self . _sign ^ other . _sign ) <EOL> else : <EOL> return context . _raise_error ( DivisionUndefined , '<STR_LIT>' ) <EOL> return self . _divide ( other , context ) [ <NUM_LIT:0> ] <EOL> def __rfloordiv__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> return other . __floordiv__ ( self , context = context ) <EOL> def __float__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _isnan ( ) : <EOL> if self . is_snan ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> s = "<STR_LIT>" if self . _sign else "<STR_LIT>" <EOL> else : <EOL> s = str ( self ) <EOL> return float ( s ) <EOL> def __int__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special : <EOL> if self . _isnan ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif self . _isinfinity ( ) : <EOL> raise OverflowError ( "<STR_LIT>" ) <EOL> s = ( - <NUM_LIT:1> ) ** self . _sign <EOL> if self . _exp >= <NUM_LIT:0> : <EOL> return s * int ( self . _int ) * <NUM_LIT:10> ** self . _exp <EOL> else : <EOL> return s * int ( self . _int [ : self . _exp ] or '<STR_LIT:0>' ) <EOL> __trunc__ = __int__ <EOL> def real ( self ) : <EOL> return self <EOL> real = property ( real ) <EOL> def imag ( self ) : <EOL> return Decimal ( <NUM_LIT:0> ) <EOL> imag = property ( imag ) <EOL> def conjugate ( self ) : <EOL> return self <EOL> def __complex__ ( self ) : <EOL> return complex ( float ( self ) ) <EOL> def __long__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return long ( self . __int__ ( ) ) <EOL> def _fix_nan ( self , context ) : <EOL> """<STR_LIT>""" <EOL> payload = self . _int <EOL> max_payload_len = context . prec - context . _clamp <EOL> if len ( payload ) > max_payload_len : <EOL> payload = payload [ len ( payload ) - max_payload_len : ] . lstrip ( '<STR_LIT:0>' ) <EOL> return _dec_from_triple ( self . _sign , payload , self . _exp , True ) <EOL> return Decimal ( self ) <EOL> def _fix ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special : <EOL> if self . _isnan ( ) : <EOL> return self . _fix_nan ( context ) <EOL> else : <EOL> return Decimal ( self ) <EOL> Etiny = context . Etiny ( ) <EOL> Etop = context . Etop ( ) <EOL> if not self : <EOL> exp_max = [ context . Emax , Etop ] [ context . _clamp ] <EOL> new_exp = min ( max ( self . _exp , Etiny ) , exp_max ) <EOL> if new_exp != self . _exp : <EOL> context . _raise_error ( Clamped ) <EOL> return _dec_from_triple ( self . _sign , '<STR_LIT:0>' , new_exp ) <EOL> else : <EOL> return Decimal ( self ) <EOL> exp_min = len ( self . _int ) + self . _exp - context . prec <EOL> if exp_min > Etop : <EOL> ans = context . _raise_error ( Overflow , '<STR_LIT>' , self . _sign ) <EOL> context . _raise_error ( Inexact ) <EOL> context . _raise_error ( Rounded ) <EOL> return ans <EOL> self_is_subnormal = exp_min < Etiny <EOL> if self_is_subnormal : <EOL> exp_min = Etiny <EOL> if self . _exp < exp_min : <EOL> digits = len ( self . _int ) + self . _exp - exp_min <EOL> if digits < <NUM_LIT:0> : <EOL> self = _dec_from_triple ( self . _sign , '<STR_LIT:1>' , exp_min - <NUM_LIT:1> ) <EOL> digits = <NUM_LIT:0> <EOL> rounding_method = self . _pick_rounding_function [ context . rounding ] <EOL> changed = rounding_method ( self , digits ) <EOL> coeff = self . _int [ : digits ] or '<STR_LIT:0>' <EOL> if changed > <NUM_LIT:0> : <EOL> coeff = str ( int ( coeff ) + <NUM_LIT:1> ) <EOL> if len ( coeff ) > context . prec : <EOL> coeff = coeff [ : - <NUM_LIT:1> ] <EOL> exp_min += <NUM_LIT:1> <EOL> if exp_min > Etop : <EOL> ans = context . _raise_error ( Overflow , '<STR_LIT>' , self . _sign ) <EOL> else : <EOL> ans = _dec_from_triple ( self . _sign , coeff , exp_min ) <EOL> if changed and self_is_subnormal : <EOL> context . _raise_error ( Underflow ) <EOL> if self_is_subnormal : <EOL> context . _raise_error ( Subnormal ) <EOL> if changed : <EOL> context . _raise_error ( Inexact ) <EOL> context . _raise_error ( Rounded ) <EOL> if not ans : <EOL> context . _raise_error ( Clamped ) <EOL> return ans <EOL> if self_is_subnormal : <EOL> context . _raise_error ( Subnormal ) <EOL> if context . _clamp == <NUM_LIT:1> and self . _exp > Etop : <EOL> context . _raise_error ( Clamped ) <EOL> self_padded = self . _int + '<STR_LIT:0>' * ( self . _exp - Etop ) <EOL> return _dec_from_triple ( self . _sign , self_padded , Etop ) <EOL> return Decimal ( self ) <EOL> def _round_down ( self , prec ) : <EOL> """<STR_LIT>""" <EOL> if _all_zeros ( self . _int , prec ) : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> def _round_up ( self , prec ) : <EOL> """<STR_LIT>""" <EOL> return - self . _round_down ( prec ) <EOL> def _round_half_up ( self , prec ) : <EOL> """<STR_LIT>""" <EOL> if self . _int [ prec ] in '<STR_LIT>' : <EOL> return <NUM_LIT:1> <EOL> elif _all_zeros ( self . _int , prec ) : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> def _round_half_down ( self , prec ) : <EOL> """<STR_LIT>""" <EOL> if _exact_half ( self . _int , prec ) : <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> return self . _round_half_up ( prec ) <EOL> def _round_half_even ( self , prec ) : <EOL> """<STR_LIT>""" <EOL> if _exact_half ( self . _int , prec ) and ( prec == <NUM_LIT:0> or self . _int [ prec - <NUM_LIT:1> ] in '<STR_LIT>' ) : <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> return self . _round_half_up ( prec ) <EOL> def _round_ceiling ( self , prec ) : <EOL> """<STR_LIT>""" <EOL> if self . _sign : <EOL> return self . _round_down ( prec ) <EOL> else : <EOL> return - self . _round_down ( prec ) <EOL> def _round_floor ( self , prec ) : <EOL> """<STR_LIT>""" <EOL> if not self . _sign : <EOL> return self . _round_down ( prec ) <EOL> else : <EOL> return - self . _round_down ( prec ) <EOL> def _round_05up ( self , prec ) : <EOL> """<STR_LIT>""" <EOL> if prec and self . _int [ prec - <NUM_LIT:1> ] not in '<STR_LIT>' : <EOL> return self . _round_down ( prec ) <EOL> else : <EOL> return - self . _round_down ( prec ) <EOL> _pick_rounding_function = dict ( <EOL> ROUND_DOWN = _round_down , <EOL> ROUND_UP = _round_up , <EOL> ROUND_HALF_UP = _round_half_up , <EOL> ROUND_HALF_DOWN = _round_half_down , <EOL> ROUND_HALF_EVEN = _round_half_even , <EOL> ROUND_CEILING = _round_ceiling , <EOL> ROUND_FLOOR = _round_floor , <EOL> ROUND_05UP = _round_05up , <EOL> ) <EOL> def fma ( self , other , third , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if self . _is_special or other . _is_special : <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . _exp == '<STR_LIT:N>' : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' , self ) <EOL> if other . _exp == '<STR_LIT:N>' : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' , other ) <EOL> if self . _exp == '<STR_LIT:n>' : <EOL> product = self <EOL> elif other . _exp == '<STR_LIT:n>' : <EOL> product = other <EOL> elif self . _exp == '<STR_LIT:F>' : <EOL> if not other : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> product = _SignedInfinity [ self . _sign ^ other . _sign ] <EOL> elif other . _exp == '<STR_LIT:F>' : <EOL> if not self : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> product = _SignedInfinity [ self . _sign ^ other . _sign ] <EOL> else : <EOL> product = _dec_from_triple ( self . _sign ^ other . _sign , <EOL> str ( int ( self . _int ) * int ( other . _int ) ) , <EOL> self . _exp + other . _exp ) <EOL> third = _convert_other ( third , raiseit = True ) <EOL> return product . __add__ ( third , context ) <EOL> def _power_modulo ( self , other , modulo , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> modulo = _convert_other ( modulo , raiseit = True ) <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> self_is_nan = self . _isnan ( ) <EOL> other_is_nan = other . _isnan ( ) <EOL> modulo_is_nan = modulo . _isnan ( ) <EOL> if self_is_nan or other_is_nan or modulo_is_nan : <EOL> if self_is_nan == <NUM_LIT:2> : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' , <EOL> self ) <EOL> if other_is_nan == <NUM_LIT:2> : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' , <EOL> other ) <EOL> if modulo_is_nan == <NUM_LIT:2> : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' , <EOL> modulo ) <EOL> if self_is_nan : <EOL> return self . _fix_nan ( context ) <EOL> if other_is_nan : <EOL> return other . _fix_nan ( context ) <EOL> return modulo . _fix_nan ( context ) <EOL> if not ( self . _isinteger ( ) and <EOL> other . _isinteger ( ) and <EOL> modulo . _isinteger ( ) ) : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if other < <NUM_LIT:0> : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if not modulo : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> if modulo . adjusted ( ) >= context . prec : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if not other and not self : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if other . _iseven ( ) : <EOL> sign = <NUM_LIT:0> <EOL> else : <EOL> sign = self . _sign <EOL> modulo = abs ( int ( modulo ) ) <EOL> base = _WorkRep ( self . to_integral_value ( ) ) <EOL> exponent = _WorkRep ( other . to_integral_value ( ) ) <EOL> base = ( base . int % modulo * pow ( <NUM_LIT:10> , base . exp , modulo ) ) % modulo <EOL> for i in xrange ( exponent . exp ) : <EOL> base = pow ( base , <NUM_LIT:10> , modulo ) <EOL> base = pow ( base , exponent . int , modulo ) <EOL> return _dec_from_triple ( sign , str ( base ) , <NUM_LIT:0> ) <EOL> def _power_exact ( self , other , p ) : <EOL> """<STR_LIT>""" <EOL> x = _WorkRep ( self ) <EOL> xc , xe = x . int , x . exp <EOL> while xc % <NUM_LIT:10> == <NUM_LIT:0> : <EOL> xc //= <NUM_LIT:10> <EOL> xe += <NUM_LIT:1> <EOL> y = _WorkRep ( other ) <EOL> yc , ye = y . int , y . exp <EOL> while yc % <NUM_LIT:10> == <NUM_LIT:0> : <EOL> yc //= <NUM_LIT:10> <EOL> ye += <NUM_LIT:1> <EOL> if xc == <NUM_LIT:1> : <EOL> xe *= yc <EOL> while xe % <NUM_LIT:10> == <NUM_LIT:0> : <EOL> xe //= <NUM_LIT:10> <EOL> ye += <NUM_LIT:1> <EOL> if ye < <NUM_LIT:0> : <EOL> return None <EOL> exponent = xe * <NUM_LIT:10> ** ye <EOL> if y . sign == <NUM_LIT:1> : <EOL> exponent = - exponent <EOL> if other . _isinteger ( ) and other . _sign == <NUM_LIT:0> : <EOL> ideal_exponent = self . _exp * int ( other ) <EOL> zeros = min ( exponent - ideal_exponent , p - <NUM_LIT:1> ) <EOL> else : <EOL> zeros = <NUM_LIT:0> <EOL> return _dec_from_triple ( <NUM_LIT:0> , '<STR_LIT:1>' + '<STR_LIT:0>' * zeros , exponent - zeros ) <EOL> if y . sign == <NUM_LIT:1> : <EOL> last_digit = xc % <NUM_LIT:10> <EOL> if last_digit in ( <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> ) : <EOL> if xc & - xc != xc : <EOL> return None <EOL> e = _nbits ( xc ) - <NUM_LIT:1> <EOL> emax = p * <NUM_LIT> // <NUM_LIT> <EOL> if ye >= len ( str ( emax ) ) : <EOL> return None <EOL> e = _decimal_lshift_exact ( e * yc , ye ) <EOL> xe = _decimal_lshift_exact ( xe * yc , ye ) <EOL> if e is None or xe is None : <EOL> return None <EOL> if e > emax : <EOL> return None <EOL> xc = <NUM_LIT:5> ** e <EOL> elif last_digit == <NUM_LIT:5> : <EOL> e = _nbits ( xc ) * <NUM_LIT> // <NUM_LIT> <EOL> xc , remainder = divmod ( <NUM_LIT:5> ** e , xc ) <EOL> if remainder : <EOL> return None <EOL> while xc % <NUM_LIT:5> == <NUM_LIT:0> : <EOL> xc //= <NUM_LIT:5> <EOL> e -= <NUM_LIT:1> <EOL> emax = p * <NUM_LIT:10> // <NUM_LIT:3> <EOL> if ye >= len ( str ( emax ) ) : <EOL> return None <EOL> e = _decimal_lshift_exact ( e * yc , ye ) <EOL> xe = _decimal_lshift_exact ( xe * yc , ye ) <EOL> if e is None or xe is None : <EOL> return None <EOL> if e > emax : <EOL> return None <EOL> xc = <NUM_LIT:2> ** e <EOL> else : <EOL> return None <EOL> if xc >= <NUM_LIT:10> ** p : <EOL> return None <EOL> xe = - e - xe <EOL> return _dec_from_triple ( <NUM_LIT:0> , str ( xc ) , xe ) <EOL> if ye >= <NUM_LIT:0> : <EOL> m , n = yc * <NUM_LIT:10> ** ye , <NUM_LIT:1> <EOL> else : <EOL> if xe != <NUM_LIT:0> and len ( str ( abs ( yc * xe ) ) ) <= - ye : <EOL> return None <EOL> xc_bits = _nbits ( xc ) <EOL> if xc != <NUM_LIT:1> and len ( str ( abs ( yc ) * xc_bits ) ) <= - ye : <EOL> return None <EOL> m , n = yc , <NUM_LIT:10> ** ( - ye ) <EOL> while m % <NUM_LIT:2> == n % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> m //= <NUM_LIT:2> <EOL> n //= <NUM_LIT:2> <EOL> while m % <NUM_LIT:5> == n % <NUM_LIT:5> == <NUM_LIT:0> : <EOL> m //= <NUM_LIT:5> <EOL> n //= <NUM_LIT:5> <EOL> if n > <NUM_LIT:1> : <EOL> if xc != <NUM_LIT:1> and xc_bits <= n : <EOL> return None <EOL> xe , rem = divmod ( xe , n ) <EOL> if rem != <NUM_LIT:0> : <EOL> return None <EOL> a = <NUM_LIT:1> L << - ( - _nbits ( xc ) // n ) <EOL> while True : <EOL> q , r = divmod ( xc , a ** ( n - <NUM_LIT:1> ) ) <EOL> if a <= q : <EOL> break <EOL> else : <EOL> a = ( a * ( n - <NUM_LIT:1> ) + q ) // n <EOL> if not ( a == q and r == <NUM_LIT:0> ) : <EOL> return None <EOL> xc = a <EOL> if xc > <NUM_LIT:1> and m > p * <NUM_LIT:100> // _log10_lb ( xc ) : <EOL> return None <EOL> xc = xc ** m <EOL> xe *= m <EOL> if xc > <NUM_LIT:10> ** p : <EOL> return None <EOL> str_xc = str ( xc ) <EOL> if other . _isinteger ( ) and other . _sign == <NUM_LIT:0> : <EOL> ideal_exponent = self . _exp * int ( other ) <EOL> zeros = min ( xe - ideal_exponent , p - len ( str_xc ) ) <EOL> else : <EOL> zeros = <NUM_LIT:0> <EOL> return _dec_from_triple ( <NUM_LIT:0> , str_xc + '<STR_LIT:0>' * zeros , xe - zeros ) <EOL> def __pow__ ( self , other , modulo = None , context = None ) : <EOL> """<STR_LIT>""" <EOL> if modulo is not None : <EOL> return self . _power_modulo ( other , modulo , context ) <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> if not other : <EOL> if not self : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' ) <EOL> else : <EOL> return _One <EOL> result_sign = <NUM_LIT:0> <EOL> if self . _sign == <NUM_LIT:1> : <EOL> if other . _isinteger ( ) : <EOL> if not other . _iseven ( ) : <EOL> result_sign = <NUM_LIT:1> <EOL> else : <EOL> if self : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> self = self . copy_negate ( ) <EOL> if not self : <EOL> if other . _sign == <NUM_LIT:0> : <EOL> return _dec_from_triple ( result_sign , '<STR_LIT:0>' , <NUM_LIT:0> ) <EOL> else : <EOL> return _SignedInfinity [ result_sign ] <EOL> if self . _isinfinity ( ) : <EOL> if other . _sign == <NUM_LIT:0> : <EOL> return _SignedInfinity [ result_sign ] <EOL> else : <EOL> return _dec_from_triple ( result_sign , '<STR_LIT:0>' , <NUM_LIT:0> ) <EOL> if self == _One : <EOL> if other . _isinteger ( ) : <EOL> if other . _sign == <NUM_LIT:1> : <EOL> multiplier = <NUM_LIT:0> <EOL> elif other > context . prec : <EOL> multiplier = context . prec <EOL> else : <EOL> multiplier = int ( other ) <EOL> exp = self . _exp * multiplier <EOL> if exp < <NUM_LIT:1> - context . prec : <EOL> exp = <NUM_LIT:1> - context . prec <EOL> context . _raise_error ( Rounded ) <EOL> else : <EOL> context . _raise_error ( Inexact ) <EOL> context . _raise_error ( Rounded ) <EOL> exp = <NUM_LIT:1> - context . prec <EOL> return _dec_from_triple ( result_sign , '<STR_LIT:1>' + '<STR_LIT:0>' * - exp , exp ) <EOL> self_adj = self . adjusted ( ) <EOL> if other . _isinfinity ( ) : <EOL> if ( other . _sign == <NUM_LIT:0> ) == ( self_adj < <NUM_LIT:0> ) : <EOL> return _dec_from_triple ( result_sign , '<STR_LIT:0>' , <NUM_LIT:0> ) <EOL> else : <EOL> return _SignedInfinity [ result_sign ] <EOL> ans = None <EOL> exact = False <EOL> bound = self . _log10_exp_bound ( ) + other . adjusted ( ) <EOL> if ( self_adj >= <NUM_LIT:0> ) == ( other . _sign == <NUM_LIT:0> ) : <EOL> if bound >= len ( str ( context . Emax ) ) : <EOL> ans = _dec_from_triple ( result_sign , '<STR_LIT:1>' , context . Emax + <NUM_LIT:1> ) <EOL> else : <EOL> Etiny = context . Etiny ( ) <EOL> if bound >= len ( str ( - Etiny ) ) : <EOL> ans = _dec_from_triple ( result_sign , '<STR_LIT:1>' , Etiny - <NUM_LIT:1> ) <EOL> if ans is None : <EOL> ans = self . _power_exact ( other , context . prec + <NUM_LIT:1> ) <EOL> if ans is not None : <EOL> if result_sign == <NUM_LIT:1> : <EOL> ans = _dec_from_triple ( <NUM_LIT:1> , ans . _int , ans . _exp ) <EOL> exact = True <EOL> if ans is None : <EOL> p = context . prec <EOL> x = _WorkRep ( self ) <EOL> xc , xe = x . int , x . exp <EOL> y = _WorkRep ( other ) <EOL> yc , ye = y . int , y . exp <EOL> if y . sign == <NUM_LIT:1> : <EOL> yc = - yc <EOL> extra = <NUM_LIT:3> <EOL> while True : <EOL> coeff , exp = _dpower ( xc , xe , yc , ye , p + extra ) <EOL> if coeff % ( <NUM_LIT:5> * <NUM_LIT:10> ** ( len ( str ( coeff ) ) - p - <NUM_LIT:1> ) ) : <EOL> break <EOL> extra += <NUM_LIT:3> <EOL> ans = _dec_from_triple ( result_sign , str ( coeff ) , exp ) <EOL> if exact and not other . _isinteger ( ) : <EOL> if len ( ans . _int ) <= context . prec : <EOL> expdiff = context . prec + <NUM_LIT:1> - len ( ans . _int ) <EOL> ans = _dec_from_triple ( ans . _sign , ans . _int + '<STR_LIT:0>' * expdiff , <EOL> ans . _exp - expdiff ) <EOL> newcontext = context . copy ( ) <EOL> newcontext . clear_flags ( ) <EOL> for exception in _signals : <EOL> newcontext . traps [ exception ] = <NUM_LIT:0> <EOL> ans = ans . _fix ( newcontext ) <EOL> newcontext . _raise_error ( Inexact ) <EOL> if newcontext . flags [ Subnormal ] : <EOL> newcontext . _raise_error ( Underflow ) <EOL> if newcontext . flags [ Overflow ] : <EOL> context . _raise_error ( Overflow , '<STR_LIT>' , ans . _sign ) <EOL> for exception in Underflow , Subnormal , Inexact , Rounded , Clamped : <EOL> if newcontext . flags [ exception ] : <EOL> context . _raise_error ( exception ) <EOL> else : <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> def __rpow__ ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other ) <EOL> if other is NotImplemented : <EOL> return other <EOL> return other . __pow__ ( self , context = context ) <EOL> def normalize ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . _is_special : <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> dup = self . _fix ( context ) <EOL> if dup . _isinfinity ( ) : <EOL> return dup <EOL> if not dup : <EOL> return _dec_from_triple ( dup . _sign , '<STR_LIT:0>' , <NUM_LIT:0> ) <EOL> exp_max = [ context . Emax , context . Etop ( ) ] [ context . _clamp ] <EOL> end = len ( dup . _int ) <EOL> exp = dup . _exp <EOL> while dup . _int [ end - <NUM_LIT:1> ] == '<STR_LIT:0>' and exp < exp_max : <EOL> exp += <NUM_LIT:1> <EOL> end -= <NUM_LIT:1> <EOL> return _dec_from_triple ( dup . _sign , dup . _int [ : end ] , exp ) <EOL> def quantize ( self , exp , rounding = None , context = None , watchexp = True ) : <EOL> """<STR_LIT>""" <EOL> exp = _convert_other ( exp , raiseit = True ) <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if rounding is None : <EOL> rounding = context . rounding <EOL> if self . _is_special or exp . _is_special : <EOL> ans = self . _check_nans ( exp , context ) <EOL> if ans : <EOL> return ans <EOL> if exp . _isinfinity ( ) or self . _isinfinity ( ) : <EOL> if exp . _isinfinity ( ) and self . _isinfinity ( ) : <EOL> return Decimal ( self ) <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> if not watchexp : <EOL> ans = self . _rescale ( exp . _exp , rounding ) <EOL> if ans . _exp > self . _exp : <EOL> context . _raise_error ( Rounded ) <EOL> if ans != self : <EOL> context . _raise_error ( Inexact ) <EOL> return ans <EOL> if not ( context . Etiny ( ) <= exp . _exp <= context . Emax ) : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> if not self : <EOL> ans = _dec_from_triple ( self . _sign , '<STR_LIT:0>' , exp . _exp ) <EOL> return ans . _fix ( context ) <EOL> self_adjusted = self . adjusted ( ) <EOL> if self_adjusted > context . Emax : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> if self_adjusted - exp . _exp + <NUM_LIT:1> > context . prec : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> ans = self . _rescale ( exp . _exp , rounding ) <EOL> if ans . adjusted ( ) > context . Emax : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> if len ( ans . _int ) > context . prec : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> if ans and ans . adjusted ( ) < context . Emin : <EOL> context . _raise_error ( Subnormal ) <EOL> if ans . _exp > self . _exp : <EOL> if ans != self : <EOL> context . _raise_error ( Inexact ) <EOL> context . _raise_error ( Rounded ) <EOL> ans = ans . _fix ( context ) <EOL> return ans <EOL> def same_quantum ( self , other ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if self . _is_special or other . _is_special : <EOL> return ( self . is_nan ( ) and other . is_nan ( ) or <EOL> self . is_infinite ( ) and other . is_infinite ( ) ) <EOL> return self . _exp == other . _exp <EOL> def _rescale ( self , exp , rounding ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special : <EOL> return Decimal ( self ) <EOL> if not self : <EOL> return _dec_from_triple ( self . _sign , '<STR_LIT:0>' , exp ) <EOL> if self . _exp >= exp : <EOL> return _dec_from_triple ( self . _sign , <EOL> self . _int + '<STR_LIT:0>' * ( self . _exp - exp ) , exp ) <EOL> digits = len ( self . _int ) + self . _exp - exp <EOL> if digits < <NUM_LIT:0> : <EOL> self = _dec_from_triple ( self . _sign , '<STR_LIT:1>' , exp - <NUM_LIT:1> ) <EOL> digits = <NUM_LIT:0> <EOL> this_function = self . _pick_rounding_function [ rounding ] <EOL> changed = this_function ( self , digits ) <EOL> coeff = self . _int [ : digits ] or '<STR_LIT:0>' <EOL> if changed == <NUM_LIT:1> : <EOL> coeff = str ( int ( coeff ) + <NUM_LIT:1> ) <EOL> return _dec_from_triple ( self . _sign , coeff , exp ) <EOL> def _round ( self , places , rounding ) : <EOL> """<STR_LIT>""" <EOL> if places <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . _is_special or not self : <EOL> return Decimal ( self ) <EOL> ans = self . _rescale ( self . adjusted ( ) + <NUM_LIT:1> - places , rounding ) <EOL> if ans . adjusted ( ) != self . adjusted ( ) : <EOL> ans = ans . _rescale ( ans . adjusted ( ) + <NUM_LIT:1> - places , rounding ) <EOL> return ans <EOL> def to_integral_exact ( self , rounding = None , context = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special : <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> return Decimal ( self ) <EOL> if self . _exp >= <NUM_LIT:0> : <EOL> return Decimal ( self ) <EOL> if not self : <EOL> return _dec_from_triple ( self . _sign , '<STR_LIT:0>' , <NUM_LIT:0> ) <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if rounding is None : <EOL> rounding = context . rounding <EOL> ans = self . _rescale ( <NUM_LIT:0> , rounding ) <EOL> if ans != self : <EOL> context . _raise_error ( Inexact ) <EOL> context . _raise_error ( Rounded ) <EOL> return ans <EOL> def to_integral_value ( self , rounding = None , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if rounding is None : <EOL> rounding = context . rounding <EOL> if self . _is_special : <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> return Decimal ( self ) <EOL> if self . _exp >= <NUM_LIT:0> : <EOL> return Decimal ( self ) <EOL> else : <EOL> return self . _rescale ( <NUM_LIT:0> , rounding ) <EOL> to_integral = to_integral_value <EOL> def sqrt ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . _is_special : <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> if self . _isinfinity ( ) and self . _sign == <NUM_LIT:0> : <EOL> return Decimal ( self ) <EOL> if not self : <EOL> ans = _dec_from_triple ( self . _sign , '<STR_LIT:0>' , self . _exp // <NUM_LIT:2> ) <EOL> return ans . _fix ( context ) <EOL> if self . _sign == <NUM_LIT:1> : <EOL> return context . _raise_error ( InvalidOperation , '<STR_LIT>' ) <EOL> prec = context . prec + <NUM_LIT:1> <EOL> op = _WorkRep ( self ) <EOL> e = op . exp >> <NUM_LIT:1> <EOL> if op . exp & <NUM_LIT:1> : <EOL> c = op . int * <NUM_LIT:10> <EOL> l = ( len ( self . _int ) >> <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> else : <EOL> c = op . int <EOL> l = len ( self . _int ) + <NUM_LIT:1> >> <NUM_LIT:1> <EOL> shift = prec - l <EOL> if shift >= <NUM_LIT:0> : <EOL> c *= <NUM_LIT:100> ** shift <EOL> exact = True <EOL> else : <EOL> c , remainder = divmod ( c , <NUM_LIT:100> ** - shift ) <EOL> exact = not remainder <EOL> e -= shift <EOL> n = <NUM_LIT:10> ** prec <EOL> while True : <EOL> q = c // n <EOL> if n <= q : <EOL> break <EOL> else : <EOL> n = n + q >> <NUM_LIT:1> <EOL> exact = exact and n * n == c <EOL> if exact : <EOL> if shift >= <NUM_LIT:0> : <EOL> n //= <NUM_LIT:10> ** shift <EOL> else : <EOL> n *= <NUM_LIT:10> ** - shift <EOL> e += shift <EOL> else : <EOL> if n % <NUM_LIT:5> == <NUM_LIT:0> : <EOL> n += <NUM_LIT:1> <EOL> ans = _dec_from_triple ( <NUM_LIT:0> , str ( n ) , e ) <EOL> context = context . _shallow_copy ( ) <EOL> rounding = context . _set_rounding ( ROUND_HALF_EVEN ) <EOL> ans = ans . _fix ( context ) <EOL> context . rounding = rounding <EOL> return ans <EOL> def max ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . _is_special or other . _is_special : <EOL> sn = self . _isnan ( ) <EOL> on = other . _isnan ( ) <EOL> if sn or on : <EOL> if on == <NUM_LIT:1> and sn == <NUM_LIT:0> : <EOL> return self . _fix ( context ) <EOL> if sn == <NUM_LIT:1> and on == <NUM_LIT:0> : <EOL> return other . _fix ( context ) <EOL> return self . _check_nans ( other , context ) <EOL> c = self . _cmp ( other ) <EOL> if c == <NUM_LIT:0> : <EOL> c = self . compare_total ( other ) <EOL> if c == - <NUM_LIT:1> : <EOL> ans = other <EOL> else : <EOL> ans = self <EOL> return ans . _fix ( context ) <EOL> def min ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . _is_special or other . _is_special : <EOL> sn = self . _isnan ( ) <EOL> on = other . _isnan ( ) <EOL> if sn or on : <EOL> if on == <NUM_LIT:1> and sn == <NUM_LIT:0> : <EOL> return self . _fix ( context ) <EOL> if sn == <NUM_LIT:1> and on == <NUM_LIT:0> : <EOL> return other . _fix ( context ) <EOL> return self . _check_nans ( other , context ) <EOL> c = self . _cmp ( other ) <EOL> if c == <NUM_LIT:0> : <EOL> c = self . compare_total ( other ) <EOL> if c == - <NUM_LIT:1> : <EOL> ans = self <EOL> else : <EOL> ans = other <EOL> return ans . _fix ( context ) <EOL> def _isinteger ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special : <EOL> return False <EOL> if self . _exp >= <NUM_LIT:0> : <EOL> return True <EOL> rest = self . _int [ self . _exp : ] <EOL> return rest == '<STR_LIT:0>' * len ( rest ) <EOL> def _iseven ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self or self . _exp > <NUM_LIT:0> : <EOL> return True <EOL> return self . _int [ - <NUM_LIT:1> + self . _exp ] in '<STR_LIT>' <EOL> def adjusted ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _exp + len ( self . _int ) - <NUM_LIT:1> <EOL> except TypeError : <EOL> return <NUM_LIT:0> <EOL> def canonical ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def compare_signal ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> ans = self . _compare_check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> return self . compare ( other , context = context ) <EOL> def compare_total ( self , other ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if self . _sign and not other . _sign : <EOL> return _NegativeOne <EOL> if not self . _sign and other . _sign : <EOL> return _One <EOL> sign = self . _sign <EOL> self_nan = self . _isnan ( ) <EOL> other_nan = other . _isnan ( ) <EOL> if self_nan or other_nan : <EOL> if self_nan == other_nan : <EOL> self_key = len ( self . _int ) , self . _int <EOL> other_key = len ( other . _int ) , other . _int <EOL> if self_key < other_key : <EOL> if sign : <EOL> return _One <EOL> else : <EOL> return _NegativeOne <EOL> if self_key > other_key : <EOL> if sign : <EOL> return _NegativeOne <EOL> else : <EOL> return _One <EOL> return _Zero <EOL> if sign : <EOL> if self_nan == <NUM_LIT:1> : <EOL> return _NegativeOne <EOL> if other_nan == <NUM_LIT:1> : <EOL> return _One <EOL> if self_nan == <NUM_LIT:2> : <EOL> return _NegativeOne <EOL> if other_nan == <NUM_LIT:2> : <EOL> return _One <EOL> else : <EOL> if self_nan == <NUM_LIT:1> : <EOL> return _One <EOL> if other_nan == <NUM_LIT:1> : <EOL> return _NegativeOne <EOL> if self_nan == <NUM_LIT:2> : <EOL> return _One <EOL> if other_nan == <NUM_LIT:2> : <EOL> return _NegativeOne <EOL> if self < other : <EOL> return _NegativeOne <EOL> if self > other : <EOL> return _One <EOL> if self . _exp < other . _exp : <EOL> if sign : <EOL> return _One <EOL> else : <EOL> return _NegativeOne <EOL> if self . _exp > other . _exp : <EOL> if sign : <EOL> return _NegativeOne <EOL> else : <EOL> return _One <EOL> return _Zero <EOL> def compare_total_mag ( self , other ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> s = self . copy_abs ( ) <EOL> o = other . copy_abs ( ) <EOL> return s . compare_total ( o ) <EOL> def copy_abs ( self ) : <EOL> """<STR_LIT>""" <EOL> return _dec_from_triple ( <NUM_LIT:0> , self . _int , self . _exp , self . _is_special ) <EOL> def copy_negate ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _sign : <EOL> return _dec_from_triple ( <NUM_LIT:0> , self . _int , self . _exp , self . _is_special ) <EOL> else : <EOL> return _dec_from_triple ( <NUM_LIT:1> , self . _int , self . _exp , self . _is_special ) <EOL> def copy_sign ( self , other ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> return _dec_from_triple ( other . _sign , self . _int , <EOL> self . _exp , self . _is_special ) <EOL> def exp ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> if self . _isinfinity ( ) == - <NUM_LIT:1> : <EOL> return _Zero <EOL> if not self : <EOL> return _One <EOL> if self . _isinfinity ( ) == <NUM_LIT:1> : <EOL> return Decimal ( self ) <EOL> p = context . prec <EOL> adj = self . adjusted ( ) <EOL> if self . _sign == <NUM_LIT:0> and adj > len ( str ( ( context . Emax + <NUM_LIT:1> ) * <NUM_LIT:3> ) ) : <EOL> ans = _dec_from_triple ( <NUM_LIT:0> , '<STR_LIT:1>' , context . Emax + <NUM_LIT:1> ) <EOL> elif self . _sign == <NUM_LIT:1> and adj > len ( str ( ( - context . Etiny ( ) + <NUM_LIT:1> ) * <NUM_LIT:3> ) ) : <EOL> ans = _dec_from_triple ( <NUM_LIT:0> , '<STR_LIT:1>' , context . Etiny ( ) - <NUM_LIT:1> ) <EOL> elif self . _sign == <NUM_LIT:0> and adj < - p : <EOL> ans = _dec_from_triple ( <NUM_LIT:0> , '<STR_LIT:1>' + '<STR_LIT:0>' * ( p - <NUM_LIT:1> ) + '<STR_LIT:1>' , - p ) <EOL> elif self . _sign == <NUM_LIT:1> and adj < - p - <NUM_LIT:1> : <EOL> ans = _dec_from_triple ( <NUM_LIT:0> , '<STR_LIT>' * ( p + <NUM_LIT:1> ) , - p - <NUM_LIT:1> ) <EOL> else : <EOL> op = _WorkRep ( self ) <EOL> c , e = op . int , op . exp <EOL> if op . sign == <NUM_LIT:1> : <EOL> c = - c <EOL> extra = <NUM_LIT:3> <EOL> while True : <EOL> coeff , exp = _dexp ( c , e , p + extra ) <EOL> if coeff % ( <NUM_LIT:5> * <NUM_LIT:10> ** ( len ( str ( coeff ) ) - p - <NUM_LIT:1> ) ) : <EOL> break <EOL> extra += <NUM_LIT:3> <EOL> ans = _dec_from_triple ( <NUM_LIT:0> , str ( coeff ) , exp ) <EOL> context = context . _shallow_copy ( ) <EOL> rounding = context . _set_rounding ( ROUND_HALF_EVEN ) <EOL> ans = ans . _fix ( context ) <EOL> context . rounding = rounding <EOL> return ans <EOL> def is_canonical ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def is_finite ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . _is_special <EOL> def is_infinite ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _exp == '<STR_LIT:F>' <EOL> def is_nan ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _exp in ( '<STR_LIT:n>' , '<STR_LIT:N>' ) <EOL> def is_normal ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special or not self : <EOL> return False <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> return context . Emin <= self . adjusted ( ) <EOL> def is_qnan ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _exp == '<STR_LIT:n>' <EOL> def is_signed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _sign == <NUM_LIT:1> <EOL> def is_snan ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _exp == '<STR_LIT:N>' <EOL> def is_subnormal ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_special or not self : <EOL> return False <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> return self . adjusted ( ) < context . Emin <EOL> def is_zero ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . _is_special and self . _int == '<STR_LIT:0>' <EOL> def _ln_exp_bound ( self ) : <EOL> """<STR_LIT>""" <EOL> adj = self . _exp + len ( self . _int ) - <NUM_LIT:1> <EOL> if adj >= <NUM_LIT:1> : <EOL> return len ( str ( adj * <NUM_LIT> // <NUM_LIT:10> ) ) - <NUM_LIT:1> <EOL> if adj <= - <NUM_LIT:2> : <EOL> return len ( str ( ( - <NUM_LIT:1> - adj ) * <NUM_LIT> // <NUM_LIT:10> ) ) - <NUM_LIT:1> <EOL> op = _WorkRep ( self ) <EOL> c , e = op . int , op . exp <EOL> if adj == <NUM_LIT:0> : <EOL> num = str ( c - <NUM_LIT:10> ** - e ) <EOL> den = str ( c ) <EOL> return len ( num ) - len ( den ) - ( num < den ) <EOL> return e + len ( str ( <NUM_LIT:10> ** - e - c ) ) - <NUM_LIT:1> <EOL> def ln ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> if not self : <EOL> return _NegativeInfinity <EOL> if self . _isinfinity ( ) == <NUM_LIT:1> : <EOL> return _Infinity <EOL> if self == _One : <EOL> return _Zero <EOL> if self . _sign == <NUM_LIT:1> : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> op = _WorkRep ( self ) <EOL> c , e = op . int , op . exp <EOL> p = context . prec <EOL> places = p - self . _ln_exp_bound ( ) + <NUM_LIT:2> <EOL> while True : <EOL> coeff = _dlog ( c , e , places ) <EOL> if coeff % ( <NUM_LIT:5> * <NUM_LIT:10> ** ( len ( str ( abs ( coeff ) ) ) - p - <NUM_LIT:1> ) ) : <EOL> break <EOL> places += <NUM_LIT:3> <EOL> ans = _dec_from_triple ( int ( coeff < <NUM_LIT:0> ) , str ( abs ( coeff ) ) , - places ) <EOL> context = context . _shallow_copy ( ) <EOL> rounding = context . _set_rounding ( ROUND_HALF_EVEN ) <EOL> ans = ans . _fix ( context ) <EOL> context . rounding = rounding <EOL> return ans <EOL> def _log10_exp_bound ( self ) : <EOL> """<STR_LIT>""" <EOL> adj = self . _exp + len ( self . _int ) - <NUM_LIT:1> <EOL> if adj >= <NUM_LIT:1> : <EOL> return len ( str ( adj ) ) - <NUM_LIT:1> <EOL> if adj <= - <NUM_LIT:2> : <EOL> return len ( str ( - <NUM_LIT:1> - adj ) ) - <NUM_LIT:1> <EOL> op = _WorkRep ( self ) <EOL> c , e = op . int , op . exp <EOL> if adj == <NUM_LIT:0> : <EOL> num = str ( c - <NUM_LIT:10> ** - e ) <EOL> den = str ( <NUM_LIT> * c ) <EOL> return len ( num ) - len ( den ) - ( num < den ) + <NUM_LIT:2> <EOL> num = str ( <NUM_LIT:10> ** - e - c ) <EOL> return len ( num ) + e - ( num < "<STR_LIT>" ) - <NUM_LIT:1> <EOL> def log10 ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> if not self : <EOL> return _NegativeInfinity <EOL> if self . _isinfinity ( ) == <NUM_LIT:1> : <EOL> return _Infinity <EOL> if self . _sign == <NUM_LIT:1> : <EOL> return context . _raise_error ( InvalidOperation , <EOL> '<STR_LIT>' ) <EOL> if self . _int [ <NUM_LIT:0> ] == '<STR_LIT:1>' and self . _int [ <NUM_LIT:1> : ] == '<STR_LIT:0>' * ( len ( self . _int ) - <NUM_LIT:1> ) : <EOL> ans = Decimal ( self . _exp + len ( self . _int ) - <NUM_LIT:1> ) <EOL> else : <EOL> op = _WorkRep ( self ) <EOL> c , e = op . int , op . exp <EOL> p = context . prec <EOL> places = p - self . _log10_exp_bound ( ) + <NUM_LIT:2> <EOL> while True : <EOL> coeff = _dlog10 ( c , e , places ) <EOL> if coeff % ( <NUM_LIT:5> * <NUM_LIT:10> ** ( len ( str ( abs ( coeff ) ) ) - p - <NUM_LIT:1> ) ) : <EOL> break <EOL> places += <NUM_LIT:3> <EOL> ans = _dec_from_triple ( int ( coeff < <NUM_LIT:0> ) , str ( abs ( coeff ) ) , - places ) <EOL> context = context . _shallow_copy ( ) <EOL> rounding = context . _set_rounding ( ROUND_HALF_EVEN ) <EOL> ans = ans . _fix ( context ) <EOL> context . rounding = rounding <EOL> return ans <EOL> def logb ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . _isinfinity ( ) : <EOL> return _Infinity <EOL> if not self : <EOL> return context . _raise_error ( DivisionByZero , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> ans = Decimal ( self . adjusted ( ) ) <EOL> return ans . _fix ( context ) <EOL> def _islogical ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _sign != <NUM_LIT:0> or self . _exp != <NUM_LIT:0> : <EOL> return False <EOL> for dig in self . _int : <EOL> if dig not in '<STR_LIT>' : <EOL> return False <EOL> return True <EOL> def _fill_logical ( self , context , opa , opb ) : <EOL> dif = context . prec - len ( opa ) <EOL> if dif > <NUM_LIT:0> : <EOL> opa = '<STR_LIT:0>' * dif + opa <EOL> elif dif < <NUM_LIT:0> : <EOL> opa = opa [ - context . prec : ] <EOL> dif = context . prec - len ( opb ) <EOL> if dif > <NUM_LIT:0> : <EOL> opb = '<STR_LIT:0>' * dif + opb <EOL> elif dif < <NUM_LIT:0> : <EOL> opb = opb [ - context . prec : ] <EOL> return opa , opb <EOL> def logical_and ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if not self . _islogical ( ) or not other . _islogical ( ) : <EOL> return context . _raise_error ( InvalidOperation ) <EOL> ( opa , opb ) = self . _fill_logical ( context , self . _int , other . _int ) <EOL> result = "<STR_LIT>" . join ( [ str ( int ( a ) & int ( b ) ) for a , b in zip ( opa , opb ) ] ) <EOL> return _dec_from_triple ( <NUM_LIT:0> , result . lstrip ( '<STR_LIT:0>' ) or '<STR_LIT:0>' , <NUM_LIT:0> ) <EOL> def logical_invert ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> return self . logical_xor ( _dec_from_triple ( <NUM_LIT:0> , '<STR_LIT:1>' * context . prec , <NUM_LIT:0> ) , <EOL> context ) <EOL> def logical_or ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if not self . _islogical ( ) or not other . _islogical ( ) : <EOL> return context . _raise_error ( InvalidOperation ) <EOL> ( opa , opb ) = self . _fill_logical ( context , self . _int , other . _int ) <EOL> result = "<STR_LIT>" . join ( [ str ( int ( a ) | int ( b ) ) for a , b in zip ( opa , opb ) ] ) <EOL> return _dec_from_triple ( <NUM_LIT:0> , result . lstrip ( '<STR_LIT:0>' ) or '<STR_LIT:0>' , <NUM_LIT:0> ) <EOL> def logical_xor ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if not self . _islogical ( ) or not other . _islogical ( ) : <EOL> return context . _raise_error ( InvalidOperation ) <EOL> ( opa , opb ) = self . _fill_logical ( context , self . _int , other . _int ) <EOL> result = "<STR_LIT>" . join ( [ str ( int ( a ) ^ int ( b ) ) for a , b in zip ( opa , opb ) ] ) <EOL> return _dec_from_triple ( <NUM_LIT:0> , result . lstrip ( '<STR_LIT:0>' ) or '<STR_LIT:0>' , <NUM_LIT:0> ) <EOL> def max_mag ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . _is_special or other . _is_special : <EOL> sn = self . _isnan ( ) <EOL> on = other . _isnan ( ) <EOL> if sn or on : <EOL> if on == <NUM_LIT:1> and sn == <NUM_LIT:0> : <EOL> return self . _fix ( context ) <EOL> if sn == <NUM_LIT:1> and on == <NUM_LIT:0> : <EOL> return other . _fix ( context ) <EOL> return self . _check_nans ( other , context ) <EOL> c = self . copy_abs ( ) . _cmp ( other . copy_abs ( ) ) <EOL> if c == <NUM_LIT:0> : <EOL> c = self . compare_total ( other ) <EOL> if c == - <NUM_LIT:1> : <EOL> ans = other <EOL> else : <EOL> ans = self <EOL> return ans . _fix ( context ) <EOL> def min_mag ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . _is_special or other . _is_special : <EOL> sn = self . _isnan ( ) <EOL> on = other . _isnan ( ) <EOL> if sn or on : <EOL> if on == <NUM_LIT:1> and sn == <NUM_LIT:0> : <EOL> return self . _fix ( context ) <EOL> if sn == <NUM_LIT:1> and on == <NUM_LIT:0> : <EOL> return other . _fix ( context ) <EOL> return self . _check_nans ( other , context ) <EOL> c = self . copy_abs ( ) . _cmp ( other . copy_abs ( ) ) <EOL> if c == <NUM_LIT:0> : <EOL> c = self . compare_total ( other ) <EOL> if c == - <NUM_LIT:1> : <EOL> ans = self <EOL> else : <EOL> ans = other <EOL> return ans . _fix ( context ) <EOL> def next_minus ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> if self . _isinfinity ( ) == - <NUM_LIT:1> : <EOL> return _NegativeInfinity <EOL> if self . _isinfinity ( ) == <NUM_LIT:1> : <EOL> return _dec_from_triple ( <NUM_LIT:0> , '<STR_LIT>' * context . prec , context . Etop ( ) ) <EOL> context = context . copy ( ) <EOL> context . _set_rounding ( ROUND_FLOOR ) <EOL> context . _ignore_all_flags ( ) <EOL> new_self = self . _fix ( context ) <EOL> if new_self != self : <EOL> return new_self <EOL> return self . __sub__ ( _dec_from_triple ( <NUM_LIT:0> , '<STR_LIT:1>' , context . Etiny ( ) - <NUM_LIT:1> ) , <EOL> context ) <EOL> def next_plus ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> ans = self . _check_nans ( context = context ) <EOL> if ans : <EOL> return ans <EOL> if self . _isinfinity ( ) == <NUM_LIT:1> : <EOL> return _Infinity <EOL> if self . _isinfinity ( ) == - <NUM_LIT:1> : <EOL> return _dec_from_triple ( <NUM_LIT:1> , '<STR_LIT>' * context . prec , context . Etop ( ) ) <EOL> context = context . copy ( ) <EOL> context . _set_rounding ( ROUND_CEILING ) <EOL> context . _ignore_all_flags ( ) <EOL> new_self = self . _fix ( context ) <EOL> if new_self != self : <EOL> return new_self <EOL> return self . __add__ ( _dec_from_triple ( <NUM_LIT:0> , '<STR_LIT:1>' , context . Etiny ( ) - <NUM_LIT:1> ) , <EOL> context ) <EOL> def next_toward ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> other = _convert_other ( other , raiseit = True ) <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> comparison = self . _cmp ( other ) <EOL> if comparison == <NUM_LIT:0> : <EOL> return self . copy_sign ( other ) <EOL> if comparison == - <NUM_LIT:1> : <EOL> ans = self . next_plus ( context ) <EOL> else : <EOL> ans = self . next_minus ( context ) <EOL> if ans . _isinfinity ( ) : <EOL> context . _raise_error ( Overflow , <EOL> '<STR_LIT>' , <EOL> ans . _sign ) <EOL> context . _raise_error ( Inexact ) <EOL> context . _raise_error ( Rounded ) <EOL> elif ans . adjusted ( ) < context . Emin : <EOL> context . _raise_error ( Underflow ) <EOL> context . _raise_error ( Subnormal ) <EOL> context . _raise_error ( Inexact ) <EOL> context . _raise_error ( Rounded ) <EOL> if not ans : <EOL> context . _raise_error ( Clamped ) <EOL> return ans <EOL> def number_class ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if self . is_snan ( ) : <EOL> return "<STR_LIT>" <EOL> if self . is_qnan ( ) : <EOL> return "<STR_LIT>" <EOL> inf = self . _isinfinity ( ) <EOL> if inf == <NUM_LIT:1> : <EOL> return "<STR_LIT>" <EOL> if inf == - <NUM_LIT:1> : <EOL> return "<STR_LIT>" <EOL> if self . is_zero ( ) : <EOL> if self . _sign : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> if self . is_subnormal ( context = context ) : <EOL> if self . _sign : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> if self . _sign : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> def radix ( self ) : <EOL> """<STR_LIT>""" <EOL> return Decimal ( <NUM_LIT:10> ) <EOL> def rotate ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> other = _convert_other ( other , raiseit = True ) <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> if other . _exp != <NUM_LIT:0> : <EOL> return context . _raise_error ( InvalidOperation ) <EOL> if not ( - context . prec <= int ( other ) <= context . prec ) : <EOL> return context . _raise_error ( InvalidOperation ) <EOL> if self . _isinfinity ( ) : <EOL> return Decimal ( self ) <EOL> torot = int ( other ) <EOL> rotdig = self . _int <EOL> topad = context . prec - len ( rotdig ) <EOL> if topad > <NUM_LIT:0> : <EOL> rotdig = '<STR_LIT:0>' * topad + rotdig <EOL> elif topad < <NUM_LIT:0> : <EOL> rotdig = rotdig [ - topad : ] <EOL> rotated = rotdig [ torot : ] + rotdig [ : torot ] <EOL> return _dec_from_triple ( self . _sign , <EOL> rotated . lstrip ( '<STR_LIT:0>' ) or '<STR_LIT:0>' , self . _exp ) <EOL> def scaleb ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> other = _convert_other ( other , raiseit = True ) <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> if other . _exp != <NUM_LIT:0> : <EOL> return context . _raise_error ( InvalidOperation ) <EOL> liminf = - <NUM_LIT:2> * ( context . Emax + context . prec ) <EOL> limsup = <NUM_LIT:2> * ( context . Emax + context . prec ) <EOL> if not ( liminf <= int ( other ) <= limsup ) : <EOL> return context . _raise_error ( InvalidOperation ) <EOL> if self . _isinfinity ( ) : <EOL> return Decimal ( self ) <EOL> d = _dec_from_triple ( self . _sign , self . _int , self . _exp + int ( other ) ) <EOL> d = d . _fix ( context ) <EOL> return d <EOL> def shift ( self , other , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> other = _convert_other ( other , raiseit = True ) <EOL> ans = self . _check_nans ( other , context ) <EOL> if ans : <EOL> return ans <EOL> if other . _exp != <NUM_LIT:0> : <EOL> return context . _raise_error ( InvalidOperation ) <EOL> if not ( - context . prec <= int ( other ) <= context . prec ) : <EOL> return context . _raise_error ( InvalidOperation ) <EOL> if self . _isinfinity ( ) : <EOL> return Decimal ( self ) <EOL> torot = int ( other ) <EOL> rotdig = self . _int <EOL> topad = context . prec - len ( rotdig ) <EOL> if topad > <NUM_LIT:0> : <EOL> rotdig = '<STR_LIT:0>' * topad + rotdig <EOL> elif topad < <NUM_LIT:0> : <EOL> rotdig = rotdig [ - topad : ] <EOL> if torot < <NUM_LIT:0> : <EOL> shifted = rotdig [ : torot ] <EOL> else : <EOL> shifted = rotdig + '<STR_LIT:0>' * torot <EOL> shifted = shifted [ - context . prec : ] <EOL> return _dec_from_triple ( self . _sign , <EOL> shifted . lstrip ( '<STR_LIT:0>' ) or '<STR_LIT:0>' , self . _exp ) <EOL> def __reduce__ ( self ) : <EOL> return ( self . __class__ , ( str ( self ) , ) ) <EOL> def __copy__ ( self ) : <EOL> if type ( self ) is Decimal : <EOL> return self <EOL> return self . __class__ ( str ( self ) ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> if type ( self ) is Decimal : <EOL> return self <EOL> return self . __class__ ( str ( self ) ) <EOL> def __format__ ( self , specifier , context = None , _localeconv = None ) : <EOL> """<STR_LIT>""" <EOL> if context is None : <EOL> context = getcontext ( ) <EOL> spec = _parse_format_specifier ( specifier , _localeconv = _localeconv ) <EOL> if self . _is_special : <EOL> sign = _format_sign ( self . _sign , spec ) <EOL> body = str ( self . copy_abs ( ) ) <EOL> return _format_align ( sign , body , spec ) <EOL> if spec [ '<STR_LIT:type>' ] is None : <EOL> spec [ '<STR_LIT:type>' ] = [ '<STR_LIT:g>' , '<STR_LIT>' ] [ context . capitals ] <EOL> if spec [ '<STR_LIT:type>' ] == '<STR_LIT:%>' : <EOL> self = _dec_from_triple ( self . _sign , self . _int , self . _exp + <NUM_LIT:2> ) <EOL> rounding = context . rounding <EOL> precision = spec [ '<STR_LIT>' ] <EOL> if precision is not None : <EOL> if spec [ '<STR_LIT:type>' ] in '<STR_LIT>' : <EOL> self = self . _round ( precision + <NUM_LIT:1> , rounding ) <EOL> elif spec [ '<STR_LIT:type>' ] in '<STR_LIT>' : <EOL> self = self . _rescale ( - precision , rounding ) <EOL> elif spec [ '<STR_LIT:type>' ] in '<STR_LIT>' and len ( self . _int ) > precision : <EOL> self = self . _round ( precision , rounding ) <EOL> if not self and self . _exp > <NUM_LIT:0> and spec [ '<STR_LIT:type>' ] in '<STR_LIT>' : <EOL> self = self . _rescale ( <NUM_LIT:0> , rounding ) <EOL> leftdigits = self . _exp + len ( self . _int ) <EOL> if spec [ '<STR_LIT:type>' ] in '<STR_LIT>' : <EOL> if not self and precision is not None : <EOL> dotplace = <NUM_LIT:1> - precision <EOL> else : <EOL> dotplace = <NUM_LIT:1> <EOL> elif spec [ '<STR_LIT:type>' ] in '<STR_LIT>' : <EOL> dotplace = leftdigits <EOL> elif spec [ '<STR_LIT:type>' ] in '<STR_LIT>' : <EOL> if self . _exp <= <NUM_LIT:0> and leftdigits > - <NUM_LIT:6> : <EOL> dotplace = leftdigits <EOL> else : <EOL> dotplace = <NUM_LIT:1> <EOL> if dotplace < <NUM_LIT:0> : <EOL> intpart = '<STR_LIT:0>' <EOL> fracpart = '<STR_LIT:0>' * ( - dotplace ) + self . _int <EOL> elif dotplace > len ( self . _int ) : <EOL> intpart = self . _int + '<STR_LIT:0>' * ( dotplace - len ( self . _int ) ) <EOL> fracpart = '<STR_LIT>' <EOL> else : <EOL> intpart = self . _int [ : dotplace ] or '<STR_LIT:0>' <EOL> fracpart = self . _int [ dotplace : ] <EOL> exp = leftdigits - dotplace <EOL> return _format_number ( self . _sign , intpart , fracpart , exp , spec ) <EOL> def _dec_from_triple ( sign , coefficient , exponent , special = False ) : <EOL> """<STR_LIT>""" <EOL> self = object . __new__ ( Decimal ) <EOL> self . _sign = sign <EOL> self . _int = coefficient <EOL> self . _exp = exponent <EOL> self . _is_special = special <EOL> return self <EOL> _numbers . Number . register ( Decimal ) <EOL> class _ContextManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , new_context ) : <EOL> self . new_context = new_context . copy ( ) <EOL> def __enter__ ( self ) : <EOL> self . saved_context = getcontext ( ) <EOL> setcontext ( self . new_context ) <EOL> return self . new_context <EOL> def __exit__ ( self , t , v , tb ) : <EOL> setcontext ( self . saved_context ) <EOL> class Context ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , prec = None , rounding = None , <EOL> traps = None , flags = None , <EOL> Emin = None , Emax = None , <EOL> capitals = None , _clamp = <NUM_LIT:0> , <EOL> _ignored_flags = None ) : <EOL> try : <EOL> dc = DefaultContext <EOL> except NameError : <EOL> pass <EOL> self . prec = prec if prec is not None else dc . prec <EOL> self . rounding = rounding if rounding is not None else dc . rounding <EOL> self . Emin = Emin if Emin is not None else dc . Emin <EOL> self . Emax = Emax if Emax is not None else dc . Emax <EOL> self . capitals = capitals if capitals is not None else dc . capitals <EOL> self . _clamp = _clamp if _clamp is not None else dc . _clamp <EOL> if _ignored_flags is None : <EOL> self . _ignored_flags = [ ] <EOL> else : <EOL> self . _ignored_flags = _ignored_flags <EOL> if traps is None : <EOL> self . traps = dc . traps . copy ( ) <EOL> elif not isinstance ( traps , dict ) : <EOL> self . traps = dict ( ( s , int ( s in traps ) ) for s in _signals ) <EOL> else : <EOL> self . traps = traps <EOL> if flags is None : <EOL> self . flags = dict . fromkeys ( _signals , <NUM_LIT:0> ) <EOL> elif not isinstance ( flags , dict ) : <EOL> self . flags = dict ( ( s , int ( s in flags ) ) for s in _signals ) <EOL> else : <EOL> self . flags = flags <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> s = [ ] <EOL> s . append ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % vars ( self ) ) <EOL> names = [ f . __name__ for f , v in self . flags . items ( ) if v ] <EOL> s . append ( '<STR_LIT>' + '<STR_LIT:U+002CU+0020>' . join ( names ) + '<STR_LIT:]>' ) <EOL> names = [ t . __name__ for t , v in self . traps . items ( ) if v ] <EOL> s . append ( '<STR_LIT>' + '<STR_LIT:U+002CU+0020>' . join ( names ) + '<STR_LIT:]>' ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( s ) + '<STR_LIT:)>' <EOL> def clear_flags ( self ) : <EOL> """<STR_LIT>""" <EOL> for flag in self . flags : <EOL> self . flags [ flag ] = <NUM_LIT:0> <EOL> def _shallow_copy ( self ) : <EOL> """<STR_LIT>""" <EOL> nc = Context ( self . prec , self . rounding , self . traps , <EOL> self . flags , self . Emin , self . Emax , <EOL> self . capitals , self . _clamp , self . _ignored_flags ) <EOL> return nc <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> nc = Context ( self . prec , self . rounding , self . traps . copy ( ) , <EOL> self . flags . copy ( ) , self . Emin , self . Emax , <EOL> self . capitals , self . _clamp , self . _ignored_flags ) <EOL> return nc <EOL> __copy__ = copy <EOL> def _raise_error ( self , condition , explanation = None , * args ) : <EOL> """<STR_LIT>""" <EOL> error = _condition_map . get ( condition , condition ) <EOL> if error in self . _ignored_flags : <EOL> return error ( ) . handle ( self , * args ) <EOL> self . flags [ error ] = <NUM_LIT:1> <EOL> if not self . traps [ error ] : <EOL> return condition ( ) . handle ( self , * args ) <EOL> raise error ( explanation ) <EOL> def _ignore_all_flags ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ignore_flags ( * _signals ) <EOL> def _ignore_flags ( self , * flags ) : <EOL> """<STR_LIT>""" <EOL> self . _ignored_flags = ( self . _ignored_flags + list ( flags ) ) <EOL> return list ( flags ) <EOL> def _regard_flags ( self , * flags ) : <EOL> """<STR_LIT>""" <EOL> if flags and isinstance ( flags [ <NUM_LIT:0> ] , ( tuple , list ) ) : <EOL> flags = flags [ <NUM_LIT:0> ] <EOL> for flag in flags : <EOL> self . _ignored_flags . remove ( flag ) <EOL> __hash__ = None <EOL> def Etiny ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . Emin - self . prec + <NUM_LIT:1> ) <EOL> def Etop ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . Emax - self . prec + <NUM_LIT:1> ) <EOL> def _set_rounding ( self , type ) : <EOL> """<STR_LIT>""" <EOL> rounding = self . rounding <EOL> self . rounding = type <EOL> return rounding <EOL> def create_decimal ( self , num = '<STR_LIT:0>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( num , basestring ) and num != num . strip ( ) : <EOL> return self . _raise_error ( ConversionSyntax , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> d = Decimal ( num , context = self ) <EOL> if d . _isnan ( ) and len ( d . _int ) > self . prec - self . _clamp : <EOL> return self . _raise_error ( ConversionSyntax , <EOL> "<STR_LIT>" ) <EOL> return d . _fix ( self ) <EOL> def create_decimal_from_float ( self , f ) : <EOL> """<STR_LIT>""" <EOL> d = Decimal . from_float ( f ) <EOL> return d . _fix ( self ) <EOL> def abs ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . __abs__ ( context = self ) <EOL> def add ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> r = a . __add__ ( b , context = self ) <EOL> if r is NotImplemented : <EOL> raise TypeError ( "<STR_LIT>" % b ) <EOL> else : <EOL> return r <EOL> def _apply ( self , a ) : <EOL> return str ( a . _fix ( self ) ) <EOL> def canonical ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return a . canonical ( context = self ) <EOL> def compare ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . compare ( b , context = self ) <EOL> def compare_signal ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . compare_signal ( b , context = self ) <EOL> def compare_total ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . compare_total ( b ) <EOL> def compare_total_mag ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . compare_total_mag ( b ) <EOL> def copy_abs ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . copy_abs ( ) <EOL> def copy_decimal ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return Decimal ( a ) <EOL> def copy_negate ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . copy_negate ( ) <EOL> def copy_sign ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . copy_sign ( b ) <EOL> def divide ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> r = a . __div__ ( b , context = self ) <EOL> if r is NotImplemented : <EOL> raise TypeError ( "<STR_LIT>" % b ) <EOL> else : <EOL> return r <EOL> def divide_int ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> r = a . __floordiv__ ( b , context = self ) <EOL> if r is NotImplemented : <EOL> raise TypeError ( "<STR_LIT>" % b ) <EOL> else : <EOL> return r <EOL> def divmod ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> r = a . __divmod__ ( b , context = self ) <EOL> if r is NotImplemented : <EOL> raise TypeError ( "<STR_LIT>" % b ) <EOL> else : <EOL> return r <EOL> def exp ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . exp ( context = self ) <EOL> def fma ( self , a , b , c ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . fma ( b , c , context = self ) <EOL> def is_canonical ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return a . is_canonical ( ) <EOL> def is_finite ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . is_finite ( ) <EOL> def is_infinite ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . is_infinite ( ) <EOL> def is_nan ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . is_nan ( ) <EOL> def is_normal ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . is_normal ( context = self ) <EOL> def is_qnan ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . is_qnan ( ) <EOL> def is_signed ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . is_signed ( ) <EOL> def is_snan ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . is_snan ( ) <EOL> def is_subnormal ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . is_subnormal ( context = self ) <EOL> def is_zero ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . is_zero ( ) <EOL> def ln ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . ln ( context = self ) <EOL> def log10 ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . log10 ( context = self ) <EOL> def logb ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . logb ( context = self ) <EOL> def logical_and ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . logical_and ( b , context = self ) <EOL> def logical_invert ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . logical_invert ( context = self ) <EOL> def logical_or ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . logical_or ( b , context = self ) <EOL> def logical_xor ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . logical_xor ( b , context = self ) <EOL> def max ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . max ( b , context = self ) <EOL> def max_mag ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . max_mag ( b , context = self ) <EOL> def min ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . min ( b , context = self ) <EOL> def min_mag ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . min_mag ( b , context = self ) <EOL> def minus ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . __neg__ ( context = self ) <EOL> def multiply ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> r = a . __mul__ ( b , context = self ) <EOL> if r is NotImplemented : <EOL> raise TypeError ( "<STR_LIT>" % b ) <EOL> else : <EOL> return r <EOL> def next_minus ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . next_minus ( context = self ) <EOL> def next_plus ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . next_plus ( context = self ) <EOL> def next_toward ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . next_toward ( b , context = self ) <EOL> def normalize ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . normalize ( context = self ) <EOL> def number_class ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . number_class ( context = self ) <EOL> def plus ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . __pos__ ( context = self ) <EOL> def power ( self , a , b , modulo = None ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> r = a . __pow__ ( b , modulo , context = self ) <EOL> if r is NotImplemented : <EOL> raise TypeError ( "<STR_LIT>" % b ) <EOL> else : <EOL> return r <EOL> def quantize ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . quantize ( b , context = self ) <EOL> def radix ( self ) : <EOL> """<STR_LIT>""" <EOL> return Decimal ( <NUM_LIT:10> ) <EOL> def remainder ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> r = a . __mod__ ( b , context = self ) <EOL> if r is NotImplemented : <EOL> raise TypeError ( "<STR_LIT>" % b ) <EOL> else : <EOL> return r <EOL> def remainder_near ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . remainder_near ( b , context = self ) <EOL> def rotate ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . rotate ( b , context = self ) <EOL> def same_quantum ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . same_quantum ( b ) <EOL> def scaleb ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . scaleb ( b , context = self ) <EOL> def shift ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . shift ( b , context = self ) <EOL> def sqrt ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . sqrt ( context = self ) <EOL> def subtract ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> r = a . __sub__ ( b , context = self ) <EOL> if r is NotImplemented : <EOL> raise TypeError ( "<STR_LIT>" % b ) <EOL> else : <EOL> return r <EOL> def to_eng_string ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . to_eng_string ( context = self ) <EOL> def to_sci_string ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . __str__ ( context = self ) <EOL> def to_integral_exact ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . to_integral_exact ( context = self ) <EOL> def to_integral_value ( self , a ) : <EOL> """<STR_LIT>""" <EOL> a = _convert_other ( a , raiseit = True ) <EOL> return a . to_integral_value ( context = self ) <EOL> to_integral = to_integral_value <EOL> class _WorkRep ( object ) : <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT:int>' , '<STR_LIT>' ) <EOL> def __init__ ( self , value = None ) : <EOL> if value is None : <EOL> self . sign = None <EOL> self . int = <NUM_LIT:0> <EOL> self . exp = None <EOL> elif isinstance ( value , Decimal ) : <EOL> self . sign = value . _sign <EOL> self . int = int ( value . _int ) <EOL> self . exp = value . _exp <EOL> else : <EOL> self . sign = value [ <NUM_LIT:0> ] <EOL> self . int = value [ <NUM_LIT:1> ] <EOL> self . exp = value [ <NUM_LIT:2> ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . sign , self . int , self . exp ) <EOL> __str__ = __repr__ <EOL> def _normalize ( op1 , op2 , prec = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if op1 . exp < op2 . exp : <EOL> tmp = op2 <EOL> other = op1 <EOL> else : <EOL> tmp = op1 <EOL> other = op2 <EOL> tmp_len = len ( str ( tmp . int ) ) <EOL> other_len = len ( str ( other . int ) ) <EOL> exp = tmp . exp + min ( - <NUM_LIT:1> , tmp_len - prec - <NUM_LIT:2> ) <EOL> if other_len + other . exp - <NUM_LIT:1> < exp : <EOL> other . int = <NUM_LIT:1> <EOL> other . exp = exp <EOL> tmp . int *= <NUM_LIT:10> ** ( tmp . exp - other . exp ) <EOL> tmp . exp = other . exp <EOL> return op1 , op2 <EOL> def _nbits ( n , correction = { <EOL> '<STR_LIT:0>' : <NUM_LIT:4> , '<STR_LIT:1>' : <NUM_LIT:3> , '<STR_LIT:2>' : <NUM_LIT:2> , '<STR_LIT:3>' : <NUM_LIT:2> , <EOL> '<STR_LIT:4>' : <NUM_LIT:1> , '<STR_LIT:5>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:a>' : <NUM_LIT:0> , '<STR_LIT:b>' : <NUM_LIT:0> , <EOL> '<STR_LIT:c>' : <NUM_LIT:0> , '<STR_LIT:d>' : <NUM_LIT:0> , '<STR_LIT:e>' : <NUM_LIT:0> , '<STR_LIT:f>' : <NUM_LIT:0> } ) : <EOL> """<STR_LIT>""" <EOL> if n < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> hex_n = "<STR_LIT>" % n <EOL> return <NUM_LIT:4> * len ( hex_n ) - correction [ hex_n [ <NUM_LIT:0> ] ] <EOL> def _decimal_lshift_exact ( n , e ) : <EOL> """<STR_LIT>""" <EOL> if n == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> elif e >= <NUM_LIT:0> : <EOL> return n * <NUM_LIT:10> ** e <EOL> else : <EOL> str_n = str ( abs ( n ) ) <EOL> val_n = len ( str_n ) - len ( str_n . rstrip ( '<STR_LIT:0>' ) ) <EOL> return None if val_n < - e else n // <NUM_LIT:10> ** - e <EOL> def _sqrt_nearest ( n , a ) : <EOL> """<STR_LIT>""" <EOL> if n <= <NUM_LIT:0> or a <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> b = <NUM_LIT:0> <EOL> while a != b : <EOL> b , a = a , a - - n // a >> <NUM_LIT:1> <EOL> return a <EOL> def _rshift_nearest ( x , shift ) : <EOL> """<STR_LIT>""" <EOL> b , q = <NUM_LIT:1> L << shift , x >> shift <EOL> return q + ( <NUM_LIT:2> * ( x & ( b - <NUM_LIT:1> ) ) + ( q & <NUM_LIT:1> ) > b ) <EOL> def _div_nearest ( a , b ) : <EOL> """<STR_LIT>""" <EOL> q , r = divmod ( a , b ) <EOL> return q + ( <NUM_LIT:2> * r + ( q & <NUM_LIT:1> ) > b ) <EOL> def _ilog ( x , M , L = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> y = x - M <EOL> R = <NUM_LIT:0> <EOL> while ( R <= L and long ( abs ( y ) ) << L - R >= M or <EOL> R > L and abs ( y ) >> R - L >= M ) : <EOL> y = _div_nearest ( long ( M * y ) << <NUM_LIT:1> , <EOL> M + _sqrt_nearest ( M * ( M + _rshift_nearest ( y , R ) ) , M ) ) <EOL> R += <NUM_LIT:1> <EOL> T = - int ( - <NUM_LIT:10> * len ( str ( M ) ) // ( <NUM_LIT:3> * L ) ) <EOL> yshift = _rshift_nearest ( y , R ) <EOL> w = _div_nearest ( M , T ) <EOL> for k in xrange ( T - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> w = _div_nearest ( M , k ) - _div_nearest ( yshift * w , M ) <EOL> return _div_nearest ( w * y , M ) <EOL> def _dlog10 ( c , e , p ) : <EOL> """<STR_LIT>""" <EOL> p += <NUM_LIT:2> <EOL> l = len ( str ( c ) ) <EOL> f = e + l - ( e + l >= <NUM_LIT:1> ) <EOL> if p > <NUM_LIT:0> : <EOL> M = <NUM_LIT:10> ** p <EOL> k = e + p - f <EOL> if k >= <NUM_LIT:0> : <EOL> c *= <NUM_LIT:10> ** k <EOL> else : <EOL> c = _div_nearest ( c , <NUM_LIT:10> ** - k ) <EOL> log_d = _ilog ( c , M ) <EOL> log_10 = _log10_digits ( p ) <EOL> log_d = _div_nearest ( log_d * M , log_10 ) <EOL> log_tenpower = f * M <EOL> else : <EOL> log_d = <NUM_LIT:0> <EOL> log_tenpower = _div_nearest ( f , <NUM_LIT:10> ** - p ) <EOL> return _div_nearest ( log_tenpower + log_d , <NUM_LIT:100> ) <EOL> def _dlog ( c , e , p ) : <EOL> """<STR_LIT>""" <EOL> p += <NUM_LIT:2> <EOL> l = len ( str ( c ) ) <EOL> f = e + l - ( e + l >= <NUM_LIT:1> ) <EOL> if p > <NUM_LIT:0> : <EOL> k = e + p - f <EOL> if k >= <NUM_LIT:0> : <EOL> c *= <NUM_LIT:10> ** k <EOL> else : <EOL> c = _div_nearest ( c , <NUM_LIT:10> ** - k ) <EOL> log_d = _ilog ( c , <NUM_LIT:10> ** p ) <EOL> else : <EOL> log_d = <NUM_LIT:0> <EOL> if f : <EOL> extra = len ( str ( abs ( f ) ) ) - <NUM_LIT:1> <EOL> if p + extra >= <NUM_LIT:0> : <EOL> f_log_ten = _div_nearest ( f * _log10_digits ( p + extra ) , <NUM_LIT:10> ** extra ) <EOL> else : <EOL> f_log_ten = <NUM_LIT:0> <EOL> else : <EOL> f_log_ten = <NUM_LIT:0> <EOL> return _div_nearest ( f_log_ten + log_d , <NUM_LIT:100> ) <EOL> class _Log10Memoize ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . digits = "<STR_LIT>" <EOL> def getdigits ( self , p ) : <EOL> """<STR_LIT>""" <EOL> if p < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if p >= len ( self . digits ) : <EOL> extra = <NUM_LIT:3> <EOL> while True : <EOL> M = <NUM_LIT:10> ** ( p + extra + <NUM_LIT:2> ) <EOL> digits = str ( _div_nearest ( _ilog ( <NUM_LIT:10> * M , M ) , <NUM_LIT:100> ) ) <EOL> if digits [ - extra : ] != '<STR_LIT:0>' * extra : <EOL> break <EOL> extra += <NUM_LIT:3> <EOL> self . digits = digits . rstrip ( '<STR_LIT:0>' ) [ : - <NUM_LIT:1> ] <EOL> return int ( self . digits [ : p + <NUM_LIT:1> ] ) <EOL> _log10_digits = _Log10Memoize ( ) . getdigits <EOL> def _iexp ( x , M , L = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> R = _nbits ( ( long ( x ) << L ) // M ) <EOL> T = - int ( - <NUM_LIT:10> * len ( str ( M ) ) // ( <NUM_LIT:3> * L ) ) <EOL> y = _div_nearest ( x , T ) <EOL> Mshift = long ( M ) << R <EOL> for i in xrange ( T - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> y = _div_nearest ( x * ( Mshift + y ) , Mshift * i ) <EOL> for k in xrange ( R - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> Mshift = long ( M ) << ( k + <NUM_LIT:2> ) <EOL> y = _div_nearest ( y * ( y + Mshift ) , Mshift ) <EOL> return M + y <EOL> def _dexp ( c , e , p ) : <EOL> """<STR_LIT>""" <EOL> p += <NUM_LIT:2> <EOL> extra = max ( <NUM_LIT:0> , e + len ( str ( c ) ) - <NUM_LIT:1> ) <EOL> q = p + extra <EOL> shift = e + q <EOL> if shift >= <NUM_LIT:0> : <EOL> cshift = c * <NUM_LIT:10> ** shift <EOL> else : <EOL> cshift = c // <NUM_LIT:10> ** - shift <EOL> quot , rem = divmod ( cshift , _log10_digits ( q ) ) <EOL> rem = _div_nearest ( rem , <NUM_LIT:10> ** extra ) <EOL> return _div_nearest ( _iexp ( rem , <NUM_LIT:10> ** p ) , <NUM_LIT:1000> ) , quot - p + <NUM_LIT:3> <EOL> def _dpower ( xc , xe , yc , ye , p ) : <EOL> """<STR_LIT>""" <EOL> b = len ( str ( abs ( yc ) ) ) + ye <EOL> lxc = _dlog ( xc , xe , p + b + <NUM_LIT:1> ) <EOL> shift = ye - b <EOL> if shift >= <NUM_LIT:0> : <EOL> pc = lxc * yc * <NUM_LIT:10> ** shift <EOL> else : <EOL> pc = _div_nearest ( lxc * yc , <NUM_LIT:10> ** - shift ) <EOL> if pc == <NUM_LIT:0> : <EOL> if ( ( len ( str ( xc ) ) + xe >= <NUM_LIT:1> ) == ( yc > <NUM_LIT:0> ) ) : <EOL> coeff , exp = <NUM_LIT:10> ** ( p - <NUM_LIT:1> ) + <NUM_LIT:1> , <NUM_LIT:1> - p <EOL> else : <EOL> coeff , exp = <NUM_LIT:10> ** p - <NUM_LIT:1> , - p <EOL> else : <EOL> coeff , exp = _dexp ( pc , - ( p + <NUM_LIT:1> ) , p + <NUM_LIT:1> ) <EOL> coeff = _div_nearest ( coeff , <NUM_LIT:10> ) <EOL> exp += <NUM_LIT:1> <EOL> return coeff , exp <EOL> def _log10_lb ( c , correction = { <EOL> '<STR_LIT:1>' : <NUM_LIT:100> , '<STR_LIT:2>' : <NUM_LIT> , '<STR_LIT:3>' : <NUM_LIT> , '<STR_LIT:4>' : <NUM_LIT> , '<STR_LIT:5>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:16> , '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : <NUM_LIT:5> } ) : <EOL> """<STR_LIT>""" <EOL> if c <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> str_c = str ( c ) <EOL> return <NUM_LIT:100> * len ( str_c ) - correction [ str_c [ <NUM_LIT:0> ] ] <EOL> def _convert_other ( other , raiseit = False , allow_float = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , Decimal ) : <EOL> return other <EOL> if isinstance ( other , ( int , long ) ) : <EOL> return Decimal ( other ) <EOL> if allow_float and isinstance ( other , float ) : <EOL> return Decimal . from_float ( other ) <EOL> if raiseit : <EOL> raise TypeError ( "<STR_LIT>" % other ) <EOL> return NotImplemented <EOL> DefaultContext = Context ( <EOL> prec = <NUM_LIT> , rounding = ROUND_HALF_EVEN , <EOL> traps = [ DivisionByZero , Overflow , InvalidOperation ] , <EOL> flags = [ ] , <EOL> Emax = <NUM_LIT> , <EOL> Emin = - <NUM_LIT> , <EOL> capitals = <NUM_LIT:1> <EOL> ) <EOL> BasicContext = Context ( <EOL> prec = <NUM_LIT:9> , rounding = ROUND_HALF_UP , <EOL> traps = [ DivisionByZero , Overflow , InvalidOperation , Clamped , Underflow ] , <EOL> flags = [ ] , <EOL> ) <EOL> ExtendedContext = Context ( <EOL> prec = <NUM_LIT:9> , rounding = ROUND_HALF_EVEN , <EOL> traps = [ ] , <EOL> flags = [ ] , <EOL> ) <EOL> import re <EOL> _parser = re . compile ( r"""<STR_LIT>""" , re . VERBOSE | re . IGNORECASE | re . UNICODE ) . match <EOL> _all_zeros = re . compile ( '<STR_LIT>' ) . match <EOL> _exact_half = re . compile ( '<STR_LIT>' ) . match <EOL> _parse_format_specifier_regex = re . compile ( r"""<STR_LIT>""" , re . VERBOSE ) <EOL> del re <EOL> try : <EOL> import locale as _locale <EOL> except ImportError : <EOL> pass <EOL> def _parse_format_specifier ( format_spec , _localeconv = None ) : <EOL> """<STR_LIT>""" <EOL> m = _parse_format_specifier_regex . match ( format_spec ) <EOL> if m is None : <EOL> raise ValueError ( "<STR_LIT>" + format_spec ) <EOL> format_dict = m . groupdict ( ) <EOL> fill = format_dict [ '<STR_LIT>' ] <EOL> align = format_dict [ '<STR_LIT>' ] <EOL> format_dict [ '<STR_LIT>' ] = ( format_dict [ '<STR_LIT>' ] is not None ) <EOL> if format_dict [ '<STR_LIT>' ] : <EOL> if fill is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" + format_spec ) <EOL> if align is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" + format_spec ) <EOL> format_dict [ '<STR_LIT>' ] = fill or '<STR_LIT:U+0020>' <EOL> format_dict [ '<STR_LIT>' ] = align or '<STR_LIT:>>' <EOL> if format_dict [ '<STR_LIT>' ] is None : <EOL> format_dict [ '<STR_LIT>' ] = '<STR_LIT:->' <EOL> format_dict [ '<STR_LIT>' ] = int ( format_dict [ '<STR_LIT>' ] or '<STR_LIT:0>' ) <EOL> if format_dict [ '<STR_LIT>' ] is not None : <EOL> format_dict [ '<STR_LIT>' ] = int ( format_dict [ '<STR_LIT>' ] ) <EOL> if format_dict [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> if format_dict [ '<STR_LIT:type>' ] is None or format_dict [ '<STR_LIT:type>' ] in '<STR_LIT>' : <EOL> format_dict [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> if format_dict [ '<STR_LIT:type>' ] == '<STR_LIT:n>' : <EOL> format_dict [ '<STR_LIT:type>' ] = '<STR_LIT:g>' <EOL> if _localeconv is None : <EOL> _localeconv = _locale . localeconv ( ) <EOL> if format_dict [ '<STR_LIT>' ] is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" + format_spec ) <EOL> format_dict [ '<STR_LIT>' ] = _localeconv [ '<STR_LIT>' ] <EOL> format_dict [ '<STR_LIT>' ] = _localeconv [ '<STR_LIT>' ] <EOL> format_dict [ '<STR_LIT>' ] = _localeconv [ '<STR_LIT>' ] <EOL> else : <EOL> if format_dict [ '<STR_LIT>' ] is None : <EOL> format_dict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> format_dict [ '<STR_LIT>' ] = [ <NUM_LIT:3> , <NUM_LIT:0> ] <EOL> format_dict [ '<STR_LIT>' ] = '<STR_LIT:.>' <EOL> format_dict [ '<STR_LIT>' ] = isinstance ( format_spec , unicode ) <EOL> return format_dict <EOL> def _format_align ( sign , body , spec ) : <EOL> """<STR_LIT>""" <EOL> minimumwidth = spec [ '<STR_LIT>' ] <EOL> fill = spec [ '<STR_LIT>' ] <EOL> padding = fill * ( minimumwidth - len ( sign ) - len ( body ) ) <EOL> align = spec [ '<STR_LIT>' ] <EOL> if align == '<STR_LIT:<>' : <EOL> result = sign + body + padding <EOL> elif align == '<STR_LIT:>>' : <EOL> result = padding + sign + body <EOL> elif align == '<STR_LIT:=>' : <EOL> result = sign + padding + body <EOL> elif align == '<STR_LIT>' : <EOL> half = len ( padding ) // <NUM_LIT:2> <EOL> result = padding [ : half ] + sign + body + padding [ half : ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if spec [ '<STR_LIT>' ] : <EOL> result = unicode ( result ) <EOL> return result <EOL> def _group_lengths ( grouping ) : <EOL> """<STR_LIT>""" <EOL> from itertools import chain , repeat <EOL> if not grouping : <EOL> return [ ] <EOL> elif grouping [ - <NUM_LIT:1> ] == <NUM_LIT:0> and len ( grouping ) >= <NUM_LIT:2> : <EOL> return chain ( grouping [ : - <NUM_LIT:1> ] , repeat ( grouping [ - <NUM_LIT:2> ] ) ) <EOL> elif grouping [ - <NUM_LIT:1> ] == _locale . CHAR_MAX : <EOL> return grouping [ : - <NUM_LIT:1> ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def _insert_thousands_sep ( digits , spec , min_width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> sep = spec [ '<STR_LIT>' ] <EOL> grouping = spec [ '<STR_LIT>' ] <EOL> groups = [ ] <EOL> for l in _group_lengths ( grouping ) : <EOL> if l <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> l = min ( max ( len ( digits ) , min_width , <NUM_LIT:1> ) , l ) <EOL> groups . append ( '<STR_LIT:0>' * ( l - len ( digits ) ) + digits [ - l : ] ) <EOL> digits = digits [ : - l ] <EOL> min_width -= l <EOL> if not digits and min_width <= <NUM_LIT:0> : <EOL> break <EOL> min_width -= len ( sep ) <EOL> else : <EOL> l = max ( len ( digits ) , min_width , <NUM_LIT:1> ) <EOL> groups . append ( '<STR_LIT:0>' * ( l - len ( digits ) ) + digits [ - l : ] ) <EOL> return sep . join ( reversed ( groups ) ) <EOL> def _format_sign ( is_negative , spec ) : <EOL> """<STR_LIT>""" <EOL> if is_negative : <EOL> return '<STR_LIT:->' <EOL> elif spec [ '<STR_LIT>' ] in '<STR_LIT>' : <EOL> return spec [ '<STR_LIT>' ] <EOL> else : <EOL> return '<STR_LIT>' <EOL> def _format_number ( is_negative , intpart , fracpart , exp , spec ) : <EOL> """<STR_LIT>""" <EOL> sign = _format_sign ( is_negative , spec ) <EOL> if fracpart : <EOL> fracpart = spec [ '<STR_LIT>' ] + fracpart <EOL> if exp != <NUM_LIT:0> or spec [ '<STR_LIT:type>' ] in '<STR_LIT>' : <EOL> echar = { '<STR_LIT:E>' : '<STR_LIT:E>' , '<STR_LIT:e>' : '<STR_LIT:e>' , '<STR_LIT>' : '<STR_LIT:E>' , '<STR_LIT:g>' : '<STR_LIT:e>' } [ spec [ '<STR_LIT:type>' ] ] <EOL> fracpart += "<STR_LIT>" . format ( echar , exp ) <EOL> if spec [ '<STR_LIT:type>' ] == '<STR_LIT:%>' : <EOL> fracpart += '<STR_LIT:%>' <EOL> if spec [ '<STR_LIT>' ] : <EOL> min_width = spec [ '<STR_LIT>' ] - len ( fracpart ) - len ( sign ) <EOL> else : <EOL> min_width = <NUM_LIT:0> <EOL> intpart = _insert_thousands_sep ( intpart , spec , min_width ) <EOL> return _format_align ( sign , intpart + fracpart , spec ) <EOL> _Infinity = Decimal ( '<STR_LIT>' ) <EOL> _NegativeInfinity = Decimal ( '<STR_LIT>' ) <EOL> _NaN = Decimal ( '<STR_LIT>' ) <EOL> _Zero = Decimal ( <NUM_LIT:0> ) <EOL> _One = Decimal ( <NUM_LIT:1> ) <EOL> _NegativeOne = Decimal ( - <NUM_LIT:1> ) <EOL> _SignedInfinity = ( _Infinity , _NegativeInfinity ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import doctest , sys <EOL> doctest . testmod ( sys . modules [ __name__ ] ) </s>
