Qfw dZgdZeZdZdZdZddlZddlZ ddl Z ddl m Z e dd d Zd Zd ZdZdZdZdZdZdZdZdZe j4dk(rdZdZdZndZdZdZeedz z ZGddeZ Gdde Z!Gdd e Z"Gd!d"e"Z#Gd#d$e e$Z%Gd%d&e"Z&Gd'd(e"e$Z'Gd)d*e Z(Gd+d,e"Z)Gd-d.e Z*Gd/d0e Z+Gd1d2e(e*Z,Gd3d4e(e*e+Z-Gd5d6e e.Z/e!e%e(e,e*e-e"e+e/g Z0e#e"e&e"e'e"e)e"iZ1eeeeeeeefZ2ddl3Z3e3jhd7Z5e6gd8Z7d9Z8d:Z9[3dtd;Z:Gd<d=e;ZZ=e j|je<Gd?d@e;Z@GdAdBe;ZAGdCdDe;ZBdvdEZCeDjZFdFZGdGZHdHZIdIZJdwdJZKdKZLdLZMGdMdNe;ZNeNjZPdwdOZQdPZRdQZSdRdSdTdUdVdWdXdYdZd[ fd\ZTdxd]ZUdud^ZVeAd_ee%e,e"ggd`daddbZWeAdcee%e,e"e!e-ggdZXeAdceggdZYddlZZZeZjdeeZjeZjzjZ_eZjdfjZ`eZjdgjZaeZjdheZjeZjzZc[Z ddldZedtdiZfdjZgdkZhdydlZidmZjdnZker0CsrrrrrrrrTllNZolNZoi@TiceZdZdZdZy)ra1Base exception class. Used exceptions derive from this. If an exception derives from another exception besides this (such as Underflow (Inexact, Rounded, Subnormal) that indicates that it is only called if the others are present. This isn't actually used for anything, though. handle -- Called when context._raise_error is called and the trap_enabler is not set. First argument is self, second is the context. More arguments can be given, those being after the explanation in _raise_error (For example, context._raise_error(NewError, '(-x)!', self._sign) would call NewError().handle(context, self._sign).) To define a new exception, it should be sufficient to have it derive from DecimalException. cyr,r-selfcontextr.s r/handlezDecimalException.handlers r1N__name__ __module__ __qualname____doc__r8r-r1r/rr_s $ r1rceZdZdZy)r a)Exponent of a 0 changed to fit bounds. This occurs and signals clamped if the exponent of a result has been altered in order to fit the constraints of a specific concrete representation. This may occur when the exponent of a zero result would be outside the bounds of a representation, or when a large normal number would have an encoded exponent that cannot be represented. In this latter case, the exponent is reduced to fit and the corresponding number of zero digits are appended to the coefficient ("fold-down"). Nr:r;r<r=r-r1r/r r v r1r ceZdZdZdZy)r a0An invalid operation was performed. Various bad things cause this: Something creates a signaling NaN -INF + INF 0 * (+-)INF (+-)INF / (+-)INF x % 0 (+-)INF % x x._rescale( non-integer ) sqrt(-x) , x > 0 0 ** 0 x ** (non-integer) x ** (+-)INF An operand is invalid The result of the operation after these is a quiet positive NaN, except when the cause is a signaling NaN, in which case the result is also a quiet NaN, but with the original sign, and an optional diagnostic information. c|r9t|dj|djdd}|j|StS)Nr(nT)_dec_from_triple_sign_int_fix_nan_NaN)r6r7r.anss r/r8zInvalidOperation.handles9 "47==$q',,TJC<<( ( r1Nr9r-r1r/r r s ,r1r ceZdZdZdZy)rzTrying to convert badly formed string. This occurs and signals invalid-operation if a string is being converted to a number and it does not conform to the numeric string syntax. The result is [0,qNaN]. ctSr,rHr5s r/r8zConversionSyntax.handle r1Nr9r-r1r/rrs  r1rceZdZdZdZy)r aDivision by 0. This occurs and signals division-by-zero if division of a finite number by zero was attempted (during a divide-integer or divide operation, or a power operation with negative right-hand operand), and the dividend was not zero. The result of the operation is [sign,inf], where sign is the exclusive or of the signs of the operands for divide, or is 1 for an odd power of -0, for power. ct|Sr,)_SignedInfinityr6r7signr.s r/r8zDivisionByZero.handles t$$r1Nr9r-r1r/r r s  %r1r ceZdZdZdZy)rzCannot perform the division adequately. This occurs and signals invalid-operation if the integer result of a divide-integer or remainder operation had too many digits (would be longer than precision). The result is [0,qNaN]. ctSr,rLr5s r/r8zDivisionImpossible.handlerMr1Nr9r-r1r/rr r1rceZdZdZdZy)rzUndefined result of division. This occurs and signals invalid-operation if division by zero was attempted (during a divide-integer, divide, or remainder operation), and the dividend is also zero. The result is [0,qNaN]. ctSr,rLr5s r/r8zDivisionUndefined.handlerMr1Nr9r-r1r/rrrUr1rceZdZdZy)r aHad to round, losing information. This occurs and signals inexact whenever the result of an operation is not exact (that is, it needed to be rounded and any discarded digits were non-zero), or if an overflow or underflow condition occurs. The result in all cases is unchanged. The inexact signal may be tested (or trapped) to determine if a given operation (or sequence of operations) was inexact. Nr?r-r1r/r r r@r1r ceZdZdZdZy)raInvalid context. Unknown rounding, for example. This occurs and signals invalid-operation if an invalid context was detected during an operation. This can occur if contexts are not checked on creation and either the precision exceeds the capability of the underlying concrete representation or an unknown or unsupported rounding was specified. These aspects of the context need only be checked when the values are required to be used. The result is [0,qNaN]. ctSr,rLr5s r/r8zInvalidContext.handlerMr1Nr9r-r1r/rrs r1rceZdZdZy)r aNumber got rounded (not necessarily changed during rounding). This occurs and signals rounded whenever the result of an operation is rounded (that is, some zero or non-zero digits were discarded from the coefficient), or if an overflow or underflow condition occurs. The result in all cases is unchanged. The rounded signal may be tested (or trapped) to determine if a given operation (or sequence of operations) caused a loss of precision. Nr?r-r1r/r r r@r1r ceZdZdZy)raExponent < Emin before rounding. This occurs and signals subnormal whenever the result of a conversion or operation is subnormal (that is, its adjusted exponent is less than Emin, before any rounding). The result in all cases is unchanged. The subnormal signal may be tested (or trapped) to determine if a given or operation (or sequence of operations) yielded a subnormal result. Nr?r-r1r/rrsr1rceZdZdZdZy)raNumerical overflow. This occurs and signals overflow if the adjusted exponent of a result (from a conversion or from an operation that is not an attempt to divide by zero), after rounding, would be greater than the largest value that can be handled by the implementation (the value Emax). The result depends on the rounding mode: For round-half-up and round-half-even (and for round-half-down and round-up, if implemented), the result of the operation is [sign,inf], where sign is the sign of the intermediate result. For round-down, the result is the largest finite number that can be represented in the current precision, with the sign of the intermediate result. For round-ceiling, the result is the same as for round-down if the sign of the intermediate result is 1, or is [0,inf] otherwise. For round-floor, the result is the same as for round-down if the sign of the intermediate result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded will also be raised. c|jttttfvr t |S|dk(rP|jt k(r t |St|d|jz|j|jz dzS|dk(rP|jtk(r t |St|d|jz|j|jz dzSy)Nr(9r2) roundingrrrrrPrrDprecEmaxrrQs r/r8zOverflow.handles     / ; ;"4( ( 19=0&t,,#D#gll*:#LL5a79 9 19;.&t,,#D#gll*:$\\',,6q8: : r1Nr9r-r1r/rrs * :r1rceZdZdZy)raxNumerical underflow with result rounded to 0. This occurs and signals underflow if a result is inexact and the adjusted exponent of the result would be smaller (more negative) than the smallest value that can be handled by the implementation (the value Emin). That is, the result is both inexact and subnormal. The result after an underflow will be a subnormal number rounded, if necessary, so that its exponent is not less than Etiny. This may result in 0 with the sign of the intermediate result and an exponent of Etiny. In all cases, Inexact, Rounded, and Subnormal will also be raised. Nr?r-r1r/rr& r1rceZdZdZy)raEnable stricter semantics for mixing floats and Decimals. If the signal is not trapped (default), mixing floats and Decimals is permitted in the Decimal() constructor, context.create_decimal() and all comparison operators. Both conversion and comparisons are exact. Any occurrence of a mixed operation is silently recorded by setting FloatOperation in the context flags. Explicit conversions with Decimal.from_float() or context.create_decimal_from_float() do not set the flag. Otherwise (the signal is trapped), only equality comparisons and explicit conversions are silent. All other mixed operations raise FloatOperation. Nr?r-r1r/rr5rdr1rdecimal_context)raEminrbcapitalsclampr`flagstrapsc tjS#t$r$t}tj ||cYSwxYw)zReturns this thread's context. If this thread does not yet have a context, returns a new context and sets this thread's context. New contexts are copies of DefaultContext. )_current_context_varget LookupErrorrsetr7s r/rr_s?#'')) )  )s*AAc|tttfvr |j}|j t j |y)z%Set this thread's context to context.N)rrrcopy clear_flagsrmrprqs r/rrms6><AA,,.W%r1c | t}t|}|jD]3\}}|tvrt d|dt |j ||5|S)abReturn a context manager for a copy of the supplied context Uses a copy of the current context if no context is specified The returned context manager creates a local decimal context in a with statement: def sin(x): with localcontext() as ctx: ctx.prec += 2 # Rest of sin calculation algorithm # uses a precision 2 greater than normal return +s # Convert result to normal precision def sin(x): with localcontext(ExtendedContext): # Rest of sin calculation algorithm # uses the Extended Context from the # General Decimal Arithmetic Specification return +s # Convert result to normal context >>> setcontext(DefaultContext) >>> print(getcontext().prec) 28 >>> with localcontext(): ... ctx = getcontext() ... ctx.prec += 2 ... print(ctx.prec) ... 30 >>> with localcontext(ExtendedContext): ... print(getcontext().prec) ... 9 >>> print(getcontext().prec) 28 'z2' is an invalid keyword argument for this function)r_ContextManageritems_context_attributes TypeErrorsetattr new_context)ctxkwargs ctx_managerkeyvalues r/r r vsgH {l!#&Klln U ) )au$VWX X ''e4% r1c eZdZdZdZdzdZedZdZdZ d{dZ d Z d Z d Z d|d Zd|d Zd|dZd|dZd|dZd|dZdZdZdZdZd}dZd|dZd|dZd|dZd~dZd|dZeZd|dZd|dZ d|dZ!e!Z"d|dZ#d Z$d|d!Z%d|d"Z&d|d#Z'd|d$Z(d|d%Z)d|d&Z*d|d'Z+d|d(Z,d)Z-d*Z.e.Z/e0d+Z1e0d,Z2d-Z3d.Z4d/Z5d0Z6d1Z7d2Z8d3Z9d4Z:d5Z;d6Ze?e7e8e9e:e;e9Z@d|d:ZAd;ZBd<ZCd|d=ZDd|d>ZEd?ZFd{d@ZGd|dAZHd|dBZId{dCZJd|dDZKdEZLdFZMd{dGZNd{dHZOeOZPd|dIZQd|dJZRd|dKZSdLZTdMZUdNZVdOZWd|dPZXd|dQZYd|dRZZdSZ[dTZ\d|dUZ]d|dVZ^dWZ_dXZ`dYZadZZbd|d[Zcd\Zdd]Zed^Zfd|d_Zgd`ZhdaZid|dbZjdcZkd|ddZld|deZmdfZndgZod|dhZpd|diZqd|djZrd|dkZsd|dlZtd|dmZud|dnZvd|doZwd|dpZxd|dqZydrZzd|dsZ{d|dtZ|d|duZ}dvZ~dwZdxZd{dyZy)rz,Floating-point class for decimal arithmetic.)_exprFrE _is_specialNc8 tj|}t|trht |j j dd}|%| t}|jtd|zS|jddk(rd|_ nd|_ |jd}|k|jd xsd}t|jd xsd }tt||z|_ |t|z |_d |_|S|jd }|Mtt|xsd j#d |_ |jdrd|_nd|_nd |_ d|_d|_|St|tr=|dk\rd|_ nd|_ d|_tt%||_ d |_|St|t&rF|j|_|j|_ |j|_ |j |_|St|t(rN|j*|_ t|j|_ t|j,|_d |_|St|t.t0frZt|dk7r t3dt|dtr|ddvs t3d|d|_ |ddk(rd |_ |d|_d|_|Sg} |dD]N} t| tr2d| cxkrdkr'n t3d| s| dk7s3| j5| Et3d|ddvr7dj7t9t| |_ |d|_d|_|St|dtr>> Decimal('3.14') # string input Decimal('3.14') >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent) Decimal('3.14') >>> Decimal(314) # int Decimal('314') >>> Decimal(Decimal(314)) # another decimal instance Decimal('314') >>> Decimal(' 3.14 \n') # leading and trailing whitespace okay Decimal('3.14') _zInvalid literal for Decimal: %rrR-r2r(intfracexp0FdiagsignalNrCFTztInvalid tuple size in creation of Decimal from list or tuple. The list or tuple should have exactly three elements.r(r2z|Invalid sign. The first value in the tuple should be an integer; either 0 for a positive number or 1 for a negative number. zTThe second value in the tuple must be composed of integers in the range 0 through 9.rCrzUThe third value in the tuple must be an integer, or one of the strings 'F', 'n', 'N'.;strict semantics for mixing floats and Decimals are enabledzCannot convert %r to Decimal)!object__new__ isinstancestr_parserstripreplacer _raise_errorrgrouprErrFlenrrlstripabsr_WorkReprRrlisttuple ValueErrorappendjoinmapfloatr from_floatrz) clsrr7r6mintpartfracpartrrdigitsdigits r/rzDecimal.__new__sL.~~c" eS ! --c267Ay?(lG++,< AE IKKwwv#%  ggenG"776?0b!''%./C0GH$4 56 #h-/ #( Kwwv# #C $4 5 < ?? eU #$,   &&u-EDIDJDI % 1 1D K6>??r1c t|tr |dk\rdnd}d}tt|}nt|trt j |st j|r|t|St jd|dk(rd}nd}t|j\}}|jdz }t|d|zz}n tdt||| }|tur|S||S)a.Converts a float to a decimal number, exactly. Note that Decimal.from_float(0.1) is not the same as Decimal('0.1'). Since 0.1 is not exactly representable in binary floating point, the value is stored as the nearest representable value which is 0x1.999999999999ap-4. The exact equivalent of the value in decimal is 0.1000000000000000055511151231257827021181583404541015625. >>> Decimal.from_float(0.1) Decimal('0.1000000000000000055511151231257827021181583404541015625') >>> Decimal.from_float(float('nan')) Decimal('NaN') >>> Decimal.from_float(float('inf')) Decimal('Infinity') >>> Decimal.from_float(-float('inf')) Decimal('-Infinity') >>> Decimal.from_float(-0.0) Decimal('-0') r(r2g?zargument must be int or float.)rrrrr_mathisinfisnanreprcopysignas_integer_ratio bit_lengthrzrDr)rfrRkcoeffrCdresults r/rzDecimal.from_floatIs, a Q1ADAAKE 5 !{{1~Q47|#~~c1%,q6**,DAq "A!Q$KE<= =!$r2 '>Mv; r1cL|jr|j}|dk(ry|dk(ryy)zrReturns whether the number is not actually one. 0 if a number 1 if NaN 2 if sNaN rCr2rrr()rr)r6rs r/_isnanzDecimal._isnanvs-   ))Cczr1c>|jdk(r|jryyy)zyReturns whether the number is infinite 0 if finite or not a number 1 if +INF -1 if -INF rr2r()rrEr6s r/ _isinfinityzDecimal._isinfinitys  99 zzr1c&|j}|d}n|j}|s|rh| t}|dk(r|jtd|S|dk(r|jtd|S|r|j |S|j |Sy)zReturns whether the number is not actually one. if self, other are sNaN, signal if self, other are NaN return nan return 0 Done before operations. FrsNaNr()rrrr rG)r6otherr7 self_is_nan other_is_nans r/ _check_nanszDecimal._check_nansskkm = L <<>L ,$,a++,>'* *r1c| t}|js |jr|jr|jtd|S|jr|jtd|S|j r|jtd|S|j r|jtd|Sy)aCVersion of _check_nans used for the signaling comparisons compare_signal, __le__, __lt__, __ge__, __gt__. Signal InvalidOperation if either self or other is a (quiet or signaling) NaN. Signaling NaNs take precedence over quiet NaNs. Return 0 if neither operand is a NaN. zcomparison involving sNaNzcomparison involving NaNr()rris_snanrr is_qnanr6rr7s r/_compare_check_nanszDecimal._compare_check_nanss ? lG   u00||~++,<,G,022++,<,G,133++,<,F,022++,<,F,133r1c<|jxs|jdk7S)zuReturn True if self is nonzero; otherwise return False. NaNs and infinities are considered nonzero. rrrFrs r/__bool__zDecimal.__bool__s 3499#33r1c|js |jr-|j}|j}||k(ry||kryy|s|syd|jz S|sd|jzS|j|jkry|j|jkry|j}|j}||k(r||jd|j |j z zz}|jd|j |j z zz}||k(ry||krd|jz Sd|jzS||kDrd|jzSd|jz S)zCompare the two non-NaN decimal instances self and other. Returns -1 if self < other, 0 if self == other and 1 if self > other. This routine is for internal use only.r(rr2r)rrrEadjustedrFr)r6rself_inf other_inf self_adjustedother_adjusted self_padded other_paddeds r/_cmpz Decimal._cmps]   u00'')H))+I9$I%u{{*++# # ;; # :: #  ) N *))c499uzz+A&BBK ::UZZ$))-C(DDLl*|+djj(((TZZ'' ^ +# #4::%& &r1ct||d\}}|tur|S|j||ry|j|dk(S)NT) equality_opFr()_convert_for_comparisonNotImplementedrrrs r/__eq__zDecimal.__eq__sH-dEtL e N "L   E7 +yy1$$r1ct||\}}|tur|S|j||}|ry|j|dkSNFr(rrrrr6rr7rIs r/__lt__zDecimal.__lt__"K-dE: e N "L&&ug6 yy!##r1ct||\}}|tur|S|j||}|ry|j|dkSrrrs r/__le__zDecimal.__le__+K-dE: e N "L&&ug6 yy1$$r1ct||\}}|tur|S|j||}|ry|j|dkDSrrrs r/__gt__zDecimal.__gt__4rr1ct||\}}|tur|S|j||}|ry|j|dk\Srrrs r/__ge__zDecimal.__ge__=rr1ct|d}|js|r"|jr|j||}|r|St|j |S)zCompare self to other. Return a decimal value: a or b is a NaN ==> Decimal('NaN') a < b ==> Decimal('-1') a == b ==> Decimal('0') a > b ==> Decimal('1') Traiseit)_convert_otherrrrrrs r/comparezDecimal.compareFsSud3   %*;*;""5'2C tyy'((r1c|jrY|jr td|jrtj |S|j rt StS|jdk\rtd|jt}n tt|j t}t|j|ztz}|dk\r|n| }|dk(rdS|S)zx.__hash__() <==> hash(x)z"Cannot hash a signaling NaN value.r( r)rrrzis_nanr__hash__rE _PyHASH_INFrpow_PyHASH_MODULUS _PyHASH_10INVrrF)r6exp_hashhash_rIs r/rzDecimal.__hash__Xs   ||~ DEEt,,::'<'&& 99>2tyy/:H=499*oFHDII)O;qyeufBYr'C'r1c t|jttt|j |j S)zeRepresents the number as a triple tuple. To show the internals exactly as they are. )rrErrrrFrrs r/as_tuplezDecimal.as_tuplers+ DJJc#tyy.A(BDIINNr1c|jr&|jr tdtd|syt |j }|j dk\r|d|j zzd}}nt|j }|dkDr |dzdk(r|dz}|dz}|dkDr |dzdk(r|j }t|| zjdz |}|r ||z}||z}d|z|z}|jr| }||fS)aExpress a finite Decimal instance in the form n / d. Returns a pair (n, d) of integers. When called on an infinity or NaN, raises OverflowError or ValueError respectively. >>> Decimal('3.14').as_integer_ratio() (157, 50) >>> Decimal('-123e5').as_integer_ratio() (-12300000, 1) >>> Decimal('0.00').as_integer_ratio() (0, 1) z#cannot convert NaN to integer ratioz(cannot convert Infinity to integer ratiorr(rr2r) rrr OverflowErrorrrFrminrrE)r6rCrd5d2shift2s r/rzDecimal.as_integer_ratioys    {{} !FGG#$NOO  N 99>r499}$aqA))Bq&QUaZaaq&QUaZ ))B!qb&,,.2B7Ff f 2 A ::A!t r1cdt|zS)z0Represents the number as an instance of Decimal.z Decimal('%s'))rrs r/__repr__zDecimal.__repr__sT**r1cddg|j}|jrG|jdk(r|dzS|jdk(r|dz|jzS|dz|jzS|jt |jz}|jdkr|d kDr|}n+|sd }n&|jd k(r |d zd zd z }n |d z d zd z}|dkrd }d d | zz|jz}nd|t |jk\r+|jd |t |jz zz}d}n!|jd|}d |j|dz}||k(rd}n&| t }ddg|j d||z zz}||z|z|zS)zReturn string representation of the number in scientific notation. Captures all of the information in the underlying representation. rrrInfinityrCNaNrr(r2rr.NeEz%+d)rErrrFrrrh) r6engr7rR leftdigitsdotplacerrrs r/__str__zDecimal.__str__s Cy$   yyCj((c!e|dii//f}tyy00YYTYY/ 99>j2o!HH YY# "Q!+a/H#Q!+a/H q=GS8)_,tyy8H TYY 'iiXc$))n%< ==GHii *GTYYxy11H  !C$,*W--.*X:M1NNCg~(3..r1c(|jd|S)a,Convert to a string, using engineering notation if an exponent is needed. Engineering notation has an exponent which is a multiple of 3. This can leave up to 3 digits to the left of the decimal place and may require the addition of either one or two trailing zeros. T)rr7)rr6r7s r/ to_eng_stringzDecimal.to_eng_strings||g|66r1c|jr|j|}|r|S| t}|s$|jtk7r|j }n|j }|j|S)zRReturns a copy with the sign switched. Rounds, if it has reason. rq)rrrr`rcopy_abs copy_negate_fixr6r7rIs r/__neg__zDecimal.__neg__sn   ""7"3C ? lG((K7--/C""$Cxx  r1c|jr|j|}|r|S| t}|s$|jtk7r|j }n t |}|j|S)zhReturns a copy, unless it is a sNaN. Rounds the number (if more than precision digits) rq)rrrr`rrrrrs r/__pos__zDecimal.__pos__sg   ""7"3C ? lG((K7--/C$-Cxx  r1c|s|jS|jr|j|}|r|S|jr|j |}|S|j |}|S)zReturns the absolute value of self. If the keyword argument 'round' is false, do not round. The expression self.__abs__(round=False) is equivalent to self.copy_abs(). rq)rrrrErr)r6roundr7rIs r/__abs__zDecimal.__abs__sl==? "   ""7"3C ::,,w,/C ,,w,/C r1c<t|}|tur|S| t}|js |jr|j ||}|r|S|j rJ|j |j k7r&|j r|jtdSt|S|j r t|St|j|j}d}|jtk(r|j |j k7rd}|sF|sDt|j |j }|rd}t|d|}|j|}|S|sUt!||j|j"z dz }|j%||j}|j|}|S|sUt!||j|j"z dz }|j%||j}|j|}|St'|}t'|}t)|||j"\}}t'} |j*|j*k7r|j,|j,k(r t|d|}|j|}|S|j,|j,kr||}}|j*dk(r+d| _|j*|j*c|_|_n5d| _n-|j*dk(rd| _d\|_|_nd| _|j*dk(r|j,|j,z| _n|j,|j,z | _|j.| _t| }|j|}|S)zbReturns self + other. -INF + INF (or the reverse) cause InvalidOperation errors. z -INF + INFr(r2r)r(r()rrrrrrrErr rrrr`rrDrmaxra_rescaler _normalizerRrr) r6rr7rIr negativezerorRop1op2rs r/__add__zDecimal.__add__.s u% N "L ? lG   u00""5'2C !::,1B1B1D"//0@,OOt}$  "u~%$))UZZ(   { *tzzU[[/HLEtzz5;;/D"4c2C((7#CJc5:: 4Q67C..g&6&67C((7#CJc499w||3A56C--W%5%56C((7#CJtnuoc3 5S 88sxx ww#''!&|S#>hhw' ww Sxx1} %(XXsxx"#( XX]FK!' CHchFK 88q=377*FJ377*FJWW fohhw r1ct|}|tur|S|js |jr|j||}|r|S|j |j |S)zReturn self - otherrq)rrrrr)rrs r/__sub__zDecimal.__sub__seu% N "L   u00""5'":C ||E--/|AAr1cRt|}|tur|S|j||S)zReturn other - selfrq)rrr+rs r/__rsub__zDecimal.__rsub__s,u% N "L}}T7}33r1ct|}|tur|S| t}|j|jz }|js |jrx|j ||}|r|S|j r!|s|jtdSt|S|j r!|s|jtdSt|S|j|jz}|r|s t|d|}|j|}|S|jdk(r*t||j|}|j|}|S|jdk(r*t||j|}|j|}|St|}t|}t|t|j |j z|}|j|}|S)z\Return self * other. (+-) INF * 0 (or its reverse) raise InvalidOperation. z (+-)INF * 0z 0 * (+-)INFr1)rrrrErrrrr rPrrDrrFrrr)r6rr7 resultsignrI resultexpr'r(s r/__mul__zDecimal.__mul__s u% N "L ? lGZZ%++-   u00""5'2C !"//0@-PP&z22  ""//0@-PP&z22II * 5":sI>C((7#CJ 99 ":uzz9EC((7#CJ :: ":tyy)DC((7#CJtnuoz3sww/@+A9Mhhw r1ct|}|turtS| t}|j|jz }|js |jr|j ||}|r|S|j r&|j r|jtdS|j r t|S|j r1|jtdt|d|jS|s/|s|jtdS|jtd|S|s|j|jz }d}nt!|j"t!|j"z |j$zdz}|j|jz |z }t'|}t'|} |dk\r*t)|j*d|zz| j*\}} n*t)|j*| j*d| zz\}} | r|d zdk(rD|dz }n>|j|jz } || kr |dzdk(r|dz}|dz }|| kr |dzdk(rt|t-||}|j/|S) zReturn self / other.z(+-)INF/(+-)INFzDivision by infinityrz0 / 0zx / 0r(r2rr)rrrrErrrrr rPr rDEtinyrr rrrFrardivmodrrr) r6rr7rRrIrrshiftr'r( remainder ideal_exps r/ __truediv__zDecimal.__truediv__sGu% N "! ! ? lGzzEKK'   u00""5'2C !e&7&7&9++,<>OPP!&t,,  "$$W.DE'c7==?CC++,=wGG''F F))ejj(CE Oc$))n4w||CaGE))ejj(50C4.C5/Cz#)#''BI*=sww#G y#)#''377R%Z3G#H y19>QJE!II 2 Io%"*/bLE1HCIo%"*/tSZ5xx  r1c|j|jz }|jr |j}n t|j|j}|j |j z }|r|js|dkr)t |dd|j ||jfS||jkrt|}t|}|j|jk\r0|xjd|j|jz zzc_ n/|xjd|j|jz zzc_ t|j|j\}} |d|jzkr6t |t|dt |jt| |fS|jtd} | | fS)zReturn (self // other, self % other), to context.prec precision. Assumes that neither self nor other is a NaN, that self is not infinite and that other is nonzero. rrr(rz%quotient too large in //, % or divmod)rErrrrrDr$r`rarrrr5rrr) r6rr7rRr8expdiffr'r(qrrIs r/_dividezDecimal._divides zzEKK'     IDIIuzz2I--/ENN$44u((*gm$T32MM)W-=-=>@ @ gll "4.C5/Cww#''!2#'' 1222#'' 122#''377+DAq2w||##(s1vq9(SVYGII""#5#JLCxr1cRt|}|tur|S|j||S)z)Swaps self/other and returns __truediv__.rq)rrr9rs r/ __rtruediv__zDecimal.__rtruediv__1s/u% N "L  w 77r1cBt|}|tur|S| t}|j||}|r||fS|j|jz }|j rI|j r|j td}||fSt||j tdfS|sI|s|j td}||fS|j td||j tdfS|j||\}}|j|}||fS)z6 Return (self // other, self % other) zdivmod(INF, INF)INF % xz divmod(0, 0)x // 0x % 0) rrrrrErrr rPrr r>r)r6rr7rIrRquotientr7s r/ __divmod__zDecimal.__divmod__8s5u% N "L ? lGug. : zzEKK'      "**+;=OPCx'-,,-=yIKK**+r)r6rr7rIr7s r/__mod__zDecimal.__mod__csu% N "L ? lGug. J    ''(8)D D++,rs r/ __floordiv__zDecimal.__floordiv__su% N "L ? lGug. J      "++,S^#B M1 1S:DII.5#66 6r1c|Sr,r-rs r/realz Decimal.real s r1ctdS)Nr(rrs r/imagz Decimal.imags qzr1c|Sr,r-rs r/ conjugatezDecimal.conjugates r1c*tt|Sr,)complexrrs r/ __complex__zDecimal.__complex__suT{##r1c|j}|j|jz }t||kDrB|t||z dj d}t |j ||jdSt|S)z2Decapitate the payload of a NaN to fit the contextNrT) rFrarirrrDrErr)r6r7payloadmax_payload_lens r/rGzDecimal._fix_nanso))",,6 w&&xtzzJC   )   )J#eO G 99w ^dii/'9Fz' CC"::7;K;KLO%dF3GIIgv&-#E{CJqL)u: ,!#2JEqLG~**8\4::N&tzz5'B,$$Y/ $$Y/$$W-   )$$W-J    + ==A $))d"2   )))c499t+;&< DIId1f%T1##D) )$$T** *r1)rrrrrrrrc|:t|ts tdtdd| }|j |S|j r&|j r tdtdt|jdtS)aRound self to the nearest integer, or to a given precision. If only one argument is supplied, round a finite Decimal instance self to the nearest integer. If self is infinite or a NaN then a Python exception is raised. If self is finite and lies exactly halfway between two integers then it is rounded to the integer with even last digit. >>> round(Decimal('123.456')) 123 >>> round(Decimal('-456.789')) -457 >>> round(Decimal('-3.0')) -3 >>> round(Decimal('2.5')) 2 >>> round(Decimal('3.5')) 4 >>> round(Decimal('Inf')) Traceback (most recent call last): ... OverflowError: cannot round an infinity >>> round(Decimal('NaN')) Traceback (most recent call last): ... ValueError: cannot round a NaN If a second argument n is supplied, self is rounded to n decimal places using the rounding mode for the current context. For an integer n, round(self, -n) is exactly equivalent to self.quantize(Decimal('1En')). >>> round(Decimal('123.456'), 0) Decimal('123') >>> round(Decimal('123.456'), 2) Decimal('123.46') >>> round(Decimal('123.456'), -2) Decimal('1E+2') >>> round(Decimal('-Infinity'), 37) Decimal('NaN') >>> round(Decimal('sNaN123'), 0) Decimal('NaN123') z+Second argument to round should be integralr(r/cannot round a NaNcannot round an infinity) rrrzrDquantizerrrrr$r)r6rCrs r/ __round__zDecimal.__round__s^ =a% MNN"1cA2.C==% %   {{} !566#$>??4==O455r1c|jr&|jr tdtdt |j dt S)zReturn the floor of self, as an integer. For a finite Decimal instance self, return the greatest integer n such that n <= self. If self is infinite or a NaN then a Python exception is raised. rrr()rrrrrr$rrs r/ __floor__zDecimal.__floor__ sD   {{} !566#$>??4==K011r1c|jr&|jr tdtdt |j dt S)zReturn the ceiling of self, as an integer. For a finite Decimal instance self, return the least integer n such that n >= self. If self is infinite or a NaN then a Python exception is raised. rrr()rrrrrr$rrs r/__ceil__zDecimal.__ceil__sD   {{} !566#$>??4==M233r1c t|d}t|d}|js |jr | t}|jdk(r|j t d|S|jdk(r|j t d|S|jdk(r|}n|jdk(r|}n|jdk(r9|s|j t dSt |j|jz }n|jdk(r|s|j t dSt |j|jz }nmt|j|jz tt|jt|jz|j|jz}j||S) a:Fused multiply-add. Returns self*other+third with no rounding of the intermediate product self*other. self and other are multiplied together, with no rounding of the result. The third operand is then added to the result, and a single final rounding is performed. TrrrrCrzINF * 0 in fmaz0 * INF in fma) rrrrrr rPrErDrrrFr))r6rthirdr7products r/fmaz Decimal.fma+s|ud3ud3   u00$,yyC++,C O+K'L'+yy5::'=?Gug..r1cBt|}|tur|St|}|tur|S| t}|j}|j}|j}|s|s|r|dk(r|j t d|S|dk(r|j t d|S|dk(r|j t d|S|r|j |S|r|j |S|j |S|jr |jr|js|j t dS|dkr|j t dS|s|j t dS|j|jk\r|j t dS|s|s|j t dS|jrd}n |j}tt|}t|j}t|j} |j|zt!d |j"|z|z}t%| j"D]} t!|d |}t!|| j|}t'|t)|dS) z!Three argument version of __pow__rrz@pow() 3rd argument not allowed unless all arguments are integersr(zApow() 2nd argument cannot be negative when 3rd argument specifiedzpow() 3rd argument cannot be 0zSinsufficient precision: pow() 3rd argument must not have more than precision digitszXat least one of pow() 1st argument and 2nd argument must be nonzero; 0**0 is not definedr)rrrrrr rG _isintegerrra_isevenrErrrto_integral_valuerrrangerDr) r6rmodulor7rr modulo_is_nanrRbaseexponentis r/ _power_modulozDecimal._power_moduloWsu% N "L' ^ #M ? lGkkm ||~   ,-a++,? ? ==?D::DS[!..01E33566!CDHHf$==Gx||$AtR(D%4v.c$i33r1ct|}|j|j}}|dzdk(r|dz}|dz }|dzdk(rt|}|j|j}}|dzdk(r|dz}|dz }|dzdk(r|dk(r||z}|dzdk(r|dz}|dz }|dzdk(r|dkry|d|zz} |jdk(r| } |j r:|j dk(r+|j t|z} t| | z |dz } nd} tddd| zz| | z S|jdk(r3|dz} | dvrg|| z|k7ryt|dz } |dzd z}|tt|k\ryt| |z|} t||z|}| |y| |kDryd | z}n| d k(rt|d zd z} td | z|\}}|ry|d zdk(r|d z}| dz} |d zdk(r|dzd z}|tt|k\ryt| |z|} t||z|}| |y| |kDryd | z}nyt|}t||kDry| |z }td||S|dk\r |d|zzd}}n|dk7r%ttt||z| kryt|}ttt||z| kry|d| z}}|d z|d zcxk(rdk(r!nn|d z}|d z}|d z|d zcxk(rdk(rn|d z|d zcxk(rdk(r!nn|d z}|d z}|d z|d zcxk(rdk(rn|dkDrf|kryt||\}}|dk7rydt| |z z} t|||dz z\}}||krn||dz z|z|z}*||k(r|dk(sy|}|dkDr||dzt|zkDry||z}||z}t|}t||kDry|j rC|j dk(r4|j t|z} t|| z |t|z } nd} td|d| zz|| z S)ahAttempt to compute self**other exactly. Given Decimals self and other and an integer p, attempt to compute an exact result for the power self**other, with p digits of precision. Return None if self**other is not exactly representable in p digits. Assumes that elimination of special cases has already been performed: self and other must both be nonspecial; self must be positive and not numerically equal to 1; other must be nonzero. For efficiency, other._exp should not be too large, so that 10**abs(other._exp) is a feasible calculation.rr(r2Nr/r)r]Arrrd)rrrrRrrErrrD_nbitsrr_decimal_lshift_exactr5r _log10_lb)r6rpxxcxeyycyerrOzeros last_digitremaxr7strxcrrCxc_bitsremar<r=str_xcs r/ _power_exactzDecimal._power_exacts't TNB2gl 2IB !GB2gl UOB2gl 2IB !GB2gl 7 "HBr'Q,r ar'Q,AvBF{Hvv{$9!ekkQ&6!%3u:!5H^3QqS9#AsSYG G 66Q;bJY&8r>2JqL6tRxSY'*!b&"5*27B79 t8Tq2JrM2% &q!tR 0 I1fk1HBFA1fktQwSY')!b&"5*27B79 t8TGE5zA~BB#Aub1 1 7b"f9aqAQw3s3r"u:/B36RjG3s2ww'(RC/rRCyqAa%1q5%A%aaa%1q5%A%a%1q5%A%aaa%1q5%A% q5!|RmGBaxr {A~&&Ab!ac(+16AaC1q(A  FqAvB 6a!C%2.. U a R v;?    %++"2!YYs5z1N>)1S[=9EE6#e)#3RX>>r1cZ ||j|||St|}|tur|S| t}|j ||}|r|S|s|s|j t dStSd}|jdk(rK|jr|jsd}n|r|j t dS|j}|s%|jdk(r t|ddSt|S|jr%|jdk(r t|St|ddS|tk(r|jr|jdk(rd}n'||jkDr |j}n t!|}|j"|z}|d|jz kr^d|jz }|j t$n9|j t&|j t$d|jz }t|dd| zz|S|j)}|jr+|jdk(|dkk(r t|ddSt|Sd}d} |j+|j)z} |dk\|jdk(k(r<| t-t/|j0k\rSt|d|j0dz}n8|j3} | t-t/| k\rt|d| dz }|I|j5||jdz}|(|dk(r!td|j6|j"}d } ||j} t9|} | j | j:}}t9|}|j |j:}}|j<dk(r| }d } t?||||| |z\}}|d d t-t/|| z dz zzzrn|d z }?t|t/||}| r|jszt-|j6|jkrY|jdzt-|j6z }t|j|j6d|zz|j"|z }|jA}|jCtDD]}d|jF|<|jI|}|j t&|jJtLr|j tN|jJtPr!|j tPd |jtNtLt&t$tRfD]#}|jJ|s|j |%|S|jI|}|S)aHReturn self ** other [ % modulo]. With two arguments, compute self**other. With three arguments, compute (self**other) % modulo. For the three argument form, the following restrictions on the arguments hold: - all three arguments must be integral - other must be nonnegative - either self or other (or both) must be nonzero - modulo must be nonzero and must have at most p digits, where p is the context precision. If any of these restrictions is violated the InvalidOperation flag is raised. The result of pow(self, other, modulo) is identical to the result that would be obtained by computing (self**other) % modulo with unbounded precision, but is computed more efficiently. It is always exact. Nz0 ** 0r(r2z+x ** y with x negative and y not an integerrr/FTrrrrj)*rrrrrrr _OnerErrrrDrPrrarrr r r_log10_exp_boundrrrbr4rrFrrrR_dpowerrsrt_signalsrkrrjrrrr )r6rrr7rI result_sign multiplierrself_adjexactboundr4rrrrrrrextrarr; newcontext exceptions r/__pow__zDecimal.__pow__s>0  %%eVW= =u% N "L ? lGug. J++,3??r1c`t|d}| t}| |j}|js |jrw|j ||}|r|S|j s|j rA|j r|j r t |S|jtdS|j|jcxkr|jksn|jtdS|s2t|jd|j}|j|S|j}||jkDr|jtdS||jz dz|j kDr|jtdS|j#|j|}|j|jkDr|jtdSt%|j&|j kDr|jtdS|r2|j|j(kr|jt*|j|jkDr/||k7r|jt,|jt.|j|}|S) zQuantize self so its exponent is the same as that of exp. Similar to self._rescale(exp._exp) but with error checking. Trzquantize with one INFz)target exponent out of bounds in quantizerz9exponent of quantize result too large for current contextr2z7quantize result has too many digits for current context)rrr`rrrrrr r4rrbrDrErrrar$rrFrgrr r )r6rr`r7rIrs r/rzDecimal.quantize sL S$/ ? lG  ''H   s""30C  D$4$4$6??$)9)9);"4=(++,<(?AA 388;w||;''(8>@ @"4::sCHH=C88G$ $  7<< '''(8(ce e 388 #a '',, 6''(8(ac cmmCHHh/ <<>GLL (''(8(ce e sxx=7<< '''(8(ac c 3<<>GLL0   + 88dii d{$$W-   )hhw r1c t|d}|js |jrF|jxr|jxs"|jxr|jS|j|jk(S)a=Return True if self and other have the same exponent; otherwise return False. If either operand is a special value, the following rules are used: * return True if both operands are infinities * return True if both operands are NaNs * otherwise, return False. Tr)rrr is_infiniterrs r/ same_quantumzDecimal.same_quantum smud3   u00KKM4elln?$$&>5+<+<+> @yyEJJ&&r1c0|jr t|S|st|jd|S|j|k\r4t|j|j d|j|z zz|St |j |jz|z }|dkrt|jd|dz }d}|j|}|||}|j d|xsd}|dk(rtt|dz}t|j||S)asRescale self so that the exponent is exp, either by padding with zeros or by truncating digits, using the given rounding mode. Specials are returned without change. This operation is quiet: it raises no flags, and uses no information from the context. exp = exp to scale to (an integer) rounding = rounding mode rr(r/r2N) rrrDrErrFrrlrr)r6rr`r this_functionrrrs r/r$zDecimal._rescale s   4= #DJJS9 9 99 #DJJ(, CS4I(I3P P TYY$))+c1 A:#DJJSU;DF44X> f- '6")c a<E 1 %E E377r1c2|dkr td|js|s t|S|j|j dz|z |}|j |j k7r&|j|j dz|z |}|S)a"Round a nonzero, nonspecial Decimal to a fixed number of significant figures, using the given rounding mode. Infinities, NaNs and zeros are returned unaltered. This operation is quiet: it raises no flags, and uses no information from the context. r(z'argument should be at least 1 in _roundr2)rrrr$r)r6placesr`rIs r/_roundzDecimal._round s Q;FG G   44= mmDMMOA-f4h? <<>T]]_ ,,,s||~a/6AC r1c||jr!|j|}|r|St|S|jdk\r t|S|st |j ddS| t }| |j}|jd|}||k7r|jt|jt|S)aVRounds to a nearby integer. If no rounding mode is specified, take the rounding mode from the context. This method raises the Rounded and Inexact flags when appropriate. See also: to_integral_value, which does exactly the same as this method except that it doesn't raise Inexact or Rounded. rqr(r) rrrrrDrErr`r$rr r r6r`r7rIs r/to_integral_exactzDecimal.to_integral_exact! s   ""7"3C 4= 99>4= #DJJQ7 7 ? lG  ''HmmAx( $;   )W% r1c| t}| |j}|jr!|j|}|r|St |S|j dk\r t |S|j d|S)z@Rounds to the nearest integer, without raising inexact, rounded.rqr()rr`rrrrr$rs r/rzDecimal.to_integral_value> ss ? lG  ''H   ""7"3C 4= 99>4= ==H- -r1c,| t}|jr@|j|}|r|S|jr|jdk(r t |S|s5t |jd|jdz}|j|S|jdk(r|jtdS|jdz}t|}|jdz }|jdzr+|jdz}t|j dz dz}n'|j}t|j dzdz }||z }|dk\r |d|zz}d } nt#|d| z\}} | } ||z}d|z} || z} | | krn | | zdz } | xr| | z|k(} | r|dk\r | d|zz} n | d| zz} ||z }n | d zdk(r| dz } t dt%| |}|j'}|j)t*} |j|}| |_|S) zReturn the square root of self.rqr(rrr2zsqrt(-x), x > 0rrTr)rrrrrErrDrrrr rarrrrrFr5r _shallow_copy _set_roundingrr`)r6r7rIraoprclr6rr7rCr<r`s r/sqrtz Decimal.sqrtQ s7 ? lG   ""7"3C !djjAot}$"4::sDIINCC88G$ $ ::?''(8:KL L,||A~ d^ FFaK 66A: ATYY1$)AADIIq A%AQ A: eOAE!!S5&[1LAy!ME U  H1AAvEQJ  "!A#( zb%iR%Z JA1uzQq#a&!,'')((9hhw# r1ct|d}| t}|js |jrl|j}|j}|s|rH|dk(r|dk(r|j |S|dk(r|dk(r|j |S|j ||S|j |}|dk(r|j|}|dk(r|}n|}|j |S)zReturns the larger value. Like max(self, other) except if one is not a number, returns NaN (and signals if one is sNaN). Also rounds. Trr2r(rrrrrrrr compare_totalr6rr7snonrrIs r/r#z Decimal.max s ud3 ? lG   u00BBR7rQw99W--7rQw ::g..''w77 IIe  6""5)A 7CCxx  r1ct|d}| t}|js |jrl|j}|j}|s|rH|dk(r|dk(r|j |S|dk(r|dk(r|j |S|j ||S|j |}|dk(r|j|}|dk(r|}n|}|j |S)zReturns the smaller value. Like min(self, other) except if one is not a number, returns NaN (and signals if one is sNaN). Also rounds. Trr2r(rrrs r/rz Decimal.min s ud3 ? lG   u00BBR7rQw99W--7rQw ::g..''w77 IIe  6""5)A 7CCxx  r1c|jry|jdk\ry|j|jd}|dt|zk(S)z"Returns whether self is an integerFr(TNr)rrrFr)r6rests r/rzDecimal._isinteger sC    99>yy$s3t9}$$r1cb|r|jdkDry|jd|jzdvS)z:Returns True if self is even. Assumes self is an integer.r(Trr)rrFrs r/rzDecimal._iseven s.tyy1}yyDII&'11r1cl |jt|jzdz S#t$rYywxYw)z$Return the adjusted exponent of selfr2r()rrrFrzrs r/rzDecimal.adjusted s5 99s499~-1 1  s $' 33c|S)zReturns the same Decimal object. As we do not have different encodings for the same number, the received object already is in its canonical form. r-rs r/ canonicalzDecimal.canonical s  r1cnt|d}|j||}|r|S|j||S)zCompares self to the other operand numerically. It's pretty much like compare(), but all NaNs signal, with signaling NaNs taking precedence over quiet NaNs. Trrq)rrrrs r/compare_signalzDecimal.compare_signal s= u5&&ug6 J||E7|33r1cbt|d}|jr|jstS|js|jrtS|j}|j }|j }|s|r||k(rnt |j |j f}t |j |j f}||kr|rtStS||kDr|rtStStS|r,|dk(rtS|dk(rtS|dk(rtS|dk(r2tS|dk(rtS|dk(rtS|dk(rtS|dk(rtS||krtS||kDrtS|j|jkr|rtStS|j|jkDr|rtStStS)zCompares self to other using the abstract representations. This is not like the standard compare, which use their numerical value. Note that a total ordering is defined for all possible abstract representations. Trr2r) rrE _NegativeOnerrrrF_Zeror)r6rr7rRself_nan other_nanself_key other_keys r/rzDecimal.compare_total+ sud3 ::ekk zzekkKzz;;=LLN y9$tyy>4994 OUZZ7 i'# ++i'++#  q=''>Kq=''>Kq=K>''q=K>'' %<  %<K 99uzz ! ## 99uzz !##  r1c~t|d}|j}|j}|j|S)zCompares self to other using abstract repr., ignoring sign. Like compare_total, but with operand's sign ignored and assumed to be 0. Tr)rrr)r6rr7rXos r/compare_total_magzDecimal.compare_total_magt s6 ud3 MMO NN q!!r1cZtd|j|j|jS)z'Returns a copy with the sign set to 0. r()rDrFrrrs r/rzDecimal.copy_abs s!499dii9I9IJJr1c|jr,td|j|j|jStd|j|j|jS)z&Returns a copy with the sign inverted.r(r2)rErDrFrrrs r/rzDecimal.copy_negate sG ::#Atyy$))T=M=MN N#Atyy$))T=M=MN Nr1ct|d}t|j|j|j|j S)z$Returns self with the sign of other.Tr)rrDrErFrrrs r/ copy_signzDecimal.copy_sign s6ud3 TYY $ 4+;+;= =r1c| t}|j|}|r|S|jdk(rtS|stS|jdk(r t |S|j }|j}|jdk(rC|tt|jdzdzkDrtdd|jdz}nE|jdk(rK|tt|j dzdzkDrtdd|jdz }n|jdk(r!|| krtddd|dz zzdz| }n|jdk(r!|| dz krtdd|dzz| dz }nt|}|j|j }}|j"dk(r| }d} t%||||z\} } | d d tt| |z dz zzzrn|dz }=tdt| | }|j'}|j)t*} |j-|}| |_|S) zReturns e ** self.rqrr2r(rr/rr_rr)rrrrrrrarrErrrbrDr4rrrrR_dexprrrrr`) r6r7rIradjrrrrrrr`s r/rz Decimal.exp sD ? lGw/ J     #LK     "4= LLmmo ::?sSgll1na-?)@%AA"1c7<<>:C ZZ1_s30@0BA/E+F'G!G"1c7==?1+<=C ZZ1_r"1cC1Io&;aR@C ZZ1_r!t"1c1Q3i!A6C$B66266qAww!|B E"1a51 sAb3s5z?1#4Q#6778   #1c%j#6C'')((9hhw# r1cy)zReturn True if self is canonical; otherwise return False. Currently, the encoding of a Decimal instance is always canonical, so this method returns True for any Decimal. Tr-rs r/ is_canonicalzDecimal.is_canonical s r1c|j S)zReturn True if self is finite; otherwise return False. A Decimal instance is considered finite if it is neither infinite nor a NaN. )rrs r/ is_finitezDecimal.is_finite s ####r1c |jdk(S)z8Return True if self is infinite; otherwise return False.rrrs r/rzDecimal.is_infinite yyCr1c|jdvS)z>Return True if self is a qNaN or sNaN; otherwise return False.rr rs r/rzDecimal.is_nan syyJ&&r1cr|js|sy| t}|j|jkS)z?Return True if self is a normal number; otherwise return False.F)rrrgrrs r/ is_normalzDecimal.is_normal s1   4 ? lG||t}}..r1c |jdk(S)z;Return True if self is a quiet NaN; otherwise return False.rCr rs r/rzDecimal.is_qnan r r1c |jdk(S)z8Return True if self is negative; otherwise return False.r2)rErs r/ is_signedzDecimal.is_signed szzQr1c |jdk(S)z?Return True if self is a signaling NaN; otherwise return False.rr rs r/rzDecimal.is_snan r r1cr|js|sy| t}|j|jkS)z9Return True if self is subnormal; otherwise return False.F)rrrrgrs r/ is_subnormalzDecimal.is_subnormal s1   4 ? lG}}--r1c>|j xr|jdk(S)z6Return True if self is a zero; otherwise return False.rrrs r/is_zerozDecimal.is_zero s###8 S(88r1c|jt|jzdz }|dk\rtt|dzdzdz S|dkr ttd|z dzdzdz St |}|j |j }}|dk(r:t|d| zz }t|}t|t|z ||kz S|ttd| z|z zdz S)zCompute a lower bound for the adjusted exponent of self.ln(). In other words, compute r such that self.ln() >= 10**r. Assumes that self is finite and positive and that self != 1. r2rrrr(rrrFrrrrr6rrrrnumdens r/ _ln_exp_boundzDecimal._ln_exp_bound sii#dii.(1, !8s3r62:'!+ + "9sBsFB;?+,q0 0 d^vvrvv1 !8aQBh-Ca&Cs8c#h&#)4 43s2r6A:''!++r1c | t}|j|}|r|S|stS|jdk(rtS|t k(rt S|jdk(r|jtdSt|}|j|j}}|j}||jz dz} t|||}|ddt!t#t%||z dz zzzrn|dz }@t't|dkt#t%|| }|j)}|j+t,} |j/|}| |_|S) z/Returns the natural (base e) logarithm of self.rqr2zln of a negative valuerrrrr()rr_NegativeInfinityr _InfinityrrrErr rrrrar_dlogrrrrDrrrrr` r6r7rIrrrrrrr`s r/lnz Decimal.ln, sl ? lGw/ J$ $     "  4<L ::?''(8(@B Bd^vvrvv1 LLT''))A-!Q'E"s3s5z?3A5a7889 aKF  s57|SU_vgF'')((9hhw# r1c|jt|jzdz }|dk\rtt|dz S|dkrttd|z dz St |}|j |j }}|dk(r@t|d| zz }td|z}t|t|z ||kz dzStd| z|z }t||z|dkz dz S) zCompute a lower bound for the adjusted exponent of self.log10(). In other words, find r such that self.log10() >= 10**r. Assumes that self is finite and positive and that self != 1. r2rrr(rr231rrs r/rzDecimal._log10_exp_bound^ sii#dii.(1, !8s3x=? " "9s2c6{#A% % d^vvrvv1 !8aQBh-Cc!e*Cs8c#h&#)4q8 8"qb&(m3x!|sU{+a//r1c | t}|j|}|r|S|stS|jdk(rtS|j dk(r|j tdS|jddk(rZ|jdddt|jdz zk(r/t|jt|jzdz }nt|}|j|j}}|j}||j!z dz} t#|||}|d d tt%t'||z dz zzzrn|d z }@t)t|dkt%t'|| }|j+}|j-t.} |j1|}| |_|S) z&Returns the base 10 logarithm of self.Nrqr2zlog10 of a negative valuer(r/rrrrr)rrr rr!rErr rFrrrrrrrar_dlog10rrrDrrrrr`r#s r/log10z Decimal.log10| s ? lGw/ J$ $     "  ::?''(8(CE E 99Q<3 499QR=CTYY!9K4L#L$))c$))n4q89C$B66266qA At,,..q0F1f-Ab3s3u:#7#9!#;<<=!   #3uQw<SZ6'JC'')((9hhw# r1c|j|}|r|S| t}|jrtS|s|j t ddSt |j}|j|S)aM Returns the exponent of the magnitude of self's MSD. The result is the integer which is the exponent of the magnitude of the most significant digit of self (as though it were truncated to a single digit while maintaining the value of that digit and without limiting the resulting exponent). rqzlogb(0)r2) rrrr!rr rrrrs r/logbz Decimal.logb szw/ J ? lG     '' 1E E dmmo&xx  r1cp|jdk7s|jdk7ry|jD]}|dvsyy)zReturn True if self is a logical operand. For being logical, it must be a finite number with a sign of 0, an exponent of 0, and a coefficient whose digits must all be either 0 or 1. r(F01T)rErrF)r6digs r/ _islogicalzDecimal._islogical s9 ::?dii1n99C$r1c|jt|z }|dkDr d|z|z}n|dkr||j d}|jt|z }|dkDr d|z|z}||fS|dkr||j d}||fS)Nr(r)rar)r6r7opaopbdifs r/ _fill_logicalzDecimal._fill_logical sllSX% 7c'C-C 1Ww||mn%CllSX% 7c'C-CCx1Ww||mn%CCxr1c | t}t|d}|jr|js|jtS|j ||j |j \}}djt||Dcgc]%\}}tt|t|z'c}}}td|jdxsddScc}}w)z;Applies an 'and' operation between self and other's digits.Trrr(r rrr0rr r5rFrziprrrDrr6rr7r2r3rbrs r/ logical_andzDecimal.logical_and ? lGud3 (8(8(:''(89 9''EJJG cC E 1#c!fSVm, EF6==#5#<a@@F*C% cn| t}|jtdd|jzd|S)zInvert all its digits.r(r/)r logical_xorrDrars r/logical_invertzDecimal.logical_invert s9 ? lG 03w||3CA F ') )r1c | t}t|d}|jr|js|jtS|j ||j |j \}}djt||Dcgc]%\}}tt|t|z'c}}}td|jdxsddScc}}w)z:Applies an 'or' operation between self and other's digits.Trrr(rr7r9s r/ logical_orzDecimal.logical_or r<r=c | t}t|d}|jr|js|jtS|j ||j |j \}}djt||Dcgc]%\}}tt|t|z 'c}}}td|jdxsddScc}}w)z;Applies an 'xor' operation between self and other's digits.Trrr(rr7r9s r/r?zDecimal.logical_xor r<r=ct|d}| t}|js |jrl|j}|j}|s|rH|dk(r|dk(r|j |S|dk(r|dk(r|j |S|j ||S|j j|j }|dk(r|j|}|dk(r|}n|}|j |Sz8Compares the values numerically with their sign ignored.Trr2r(r rrrrrrrrrrs r/max_magzDecimal.max_mag" sud3 ? lG   u00BBR7rQw99W--7rQw ::g..''w77 MMO !1 2 6""5)A 7CCxx  r1ct|d}| t}|js |jrl|j}|j}|s|rH|dk(r|dk(r|j |S|dk(r|dk(r|j |S|j ||S|j j|j }|dk(r|j|}|dk(r|}n|}|j |SrErFrs r/min_magzDecimal.min_mag@ sud3 ? lG   u00BBR7rQw99W--7rQw ::g..''w77 MMO !1 2 6""5)A 7CCxx  r1c| t}|j|}|r|S|jdk(rtS|jdk(r(t dd|j z|j S|j}|jt|j|j|}||k7r|S|jt dd|jdz |S)z=Returns the largest representable number smaller than itself.rqrr2r(r_r/)rrrr rDrarkrsrr_ignore_all_flagsrr+r4r6r7rInew_selfs r/ next_minuszDecimal.next_minus^ s ? lGw/ J     #$ $     "#As7<<'7H H,,.k*!!#99W% t O||,QW]]_Q5FG#% %r1c| t}|j|}|r|S|jdk(rtS|jdk(r(t dd|j z|j S|j}|jt|j|j|}||k7r|S|jt dd|jdz |S)z=Returns the smallest representable number larger than itself.rqr2rr_r(r/)rrrr!rDrarkrsrrrKrr)r4rLs r/ next_pluszDecimal.next_plusu s ? lGw/ J     "      ##As7<<'7H H,,.m,!!#99W% t O||,QW]]_Q5FG#% %r1ct|d}| t}|j||}|r|S|j|}|dk(r|j |S|dk(r|j |}n|j |}|jrM|jtd|j|jt|jt|S|j|jkrk|jt|jt |jt|jt|s|jt"|S)aReturns the number closest to self, in the direction towards other. The result is the closest representable number to self (excluding self) that is in the direction towards other, unless both have the same value. If the two operands are numerically equal, then the result is a copy of self with the sign set to be the same as the sign of other. Trr(rz Infinite result from next_toward)rrrrrrPrNrrrrEr r rrgrrr )r6rr7rI comparisons r/ next_towardzDecimal.next_toward s-ud3 ? lGug. JYYu% ?>>%( (  ..)C//'*C ??   !C!$ ,   )   ) \\^gll *   +   +   )   )$$W- r1c.|jry|jry|j}|dk(ry|dk(ry|jr|jryy| t }|j | r|jry y |jry y )aReturns an indication of the class of self. The class is one of the following strings: sNaN NaN -Infinity -Normal -Subnormal -Zero +Zero +Subnormal +Normal +Infinity rr r2z +Infinityrz -Infinityz-Zeroz+Zerorqz -Subnormalz +Subnormalz-Normalz+Normal)rrrrrErr)r6r7infs r/ number_classzDecimal.number_class s <<> <<>  !8 "9 <<>zz ? lG   W  -zz## ::r1ctdS)z'Just returns 10, as this is Decimal, :)rr_rs r/radixz Decimal.radix s r{r1cl| t}t|d}|j||}|r|S|jdk7r|j t S|j t|cxkr|j ksn|j t S|jr t|St|}|j}|j t|z }|dkDr d|z|z}n |dkr|| d}||d|d|z}t|j|jdxsd|jS)z5Returns a rotated copy of self, value-of-other times.NTrr(rrrrrrr rarrrrFrrDrEr)r6rr7rItorotrotdigtopadrotateds r/rotatezDecimal.rotate s' ? lGud3ug. J ::?''(89 9 U;w||;''(89 9    4= E  s6{* 19Y'F QYUFG_F.6&5>1 's 3 :sDIIG Gr1c8| t}t|d}|j||}|r|S|jdk7r|j t Sd|j |jzz}d|j |jzz}|t|cxkr|ksn|j t S|jr t|St|j|j|jt|z}|j|}|S)z>Returns self operand after adding the second value to its exp.Trr(rr)rrrrrr rbrarrrrDrErFr)r6rr7rIliminflimsuprs r/scalebzDecimal.scaleb s ? lGud3ug. J ::?''(89 9w||gll23w||gll23#e*..''(89 9    4= TZZDIIE 4J K FF7Or1c| t}t|d}|j||}|r|S|jdk7r|j t S|j t|cxkr|j ksn|j t S|jr t|St|}|j}|j t|z }|dkDr d|z|z}n |dkr|| d}|dkr|d|}n|d|zz}||j d}t|j|jdxsd|jS)z5Returns a shifted copy of self, value-of-other times.NTrr(rrZ)r6rr7rIr[r\r]shifteds r/r6z Decimal.shift"sC ? lGud3ug. J ::?''(89 9 U;w||;''(89 9    4= E  s6{* 19Y'F QYUFG_F 19VenGs5y(Gw||mn-G $+NN3$7$>3 K Kr1c2|jt|ffSr,) __class__rrs r/ __reduce__zDecimal.__reduce__IsT --r1c\t|tur|S|jt|Sr,typerrgrrs r/__copy__zDecimal.__copy__L& : K~~c$i((r1c\t|tur|S|jt|Sr,rj)r6memos r/ __deepcopy__zDecimal.__deepcopy__Qrmr1c| t}t||}|jrIt|j|}t |j }|ddk(r|dz }t|||S|dddg|j|d<|ddk(r.t|j|j|jdz}|j}|d}|i|dd vr|j|d z|}nL|dd vr|j| |}n1|dd vr*t|j|kDr|j||}|s(|jd kDr|dd vr|jd |}|s|dr|jrd } n |j} |jt|jz} |dd vr |s|d |z } n-d } n*|dd vr| } n |dd vr|jd kr| dkDr| } nd }  d krd} d| z|jz} ne| t|jkDr+|jd| t|jz zz} d} n"|jd| xsd} |j| d} | | z }t!| | | ||S)a|Format a Decimal instance according to the given specifier. The specifier should be a standard format specifier, with the form described in PEP 3101. Formatting types 'e', 'E', 'f', 'F', 'g', 'G', 'n' and '%' are supported. If the formatting type is omitted it defaults to 'g' or 'G', depending on the value of context.capitals. N) _localeconvrk%gGr precisioneEr2zfF%gGr(no_neg_0r rr)r_parse_format_specifierr _format_signrErr _format_alignrhrDrFrr`rr$r_format_number)r6 specifierr7rrspecrRbodyr`rv adjusted_signrrrrrs r/ __format__zDecimal.__format__Xs ? lG&ykJ    D1Dt}}'DF|s"  tT2 2 < :g&6&67DL <3 #DJJ 499Q;GD##%  F|t#{{9Q;9f&}}iZ:f%#dii.9*D{{9h7 A $v,%*?==H-DZ(TZZM JJMYYTYY/ <4 I1y= &\U "!H &\T !yyA~*r/% a<GXI2H DII &ii#xDII'>"??GHii *1cGyy+H!mWhTJJr1)rN)NNr,)FN)TN)r:r;r<r= __slots__r classmethodrrrrrrrrrrrrrrrrrrrrrr!r)__radd__r+r-r2__rmul__r9r>r@rFrHrJrLrPrRrTrYr[ __trunc__propertyr]r`rbrerGrrvrxr{rrrrrdictrlrrrrrrrrrrrr$rrr to_integralrr#rrrrrrrrrrrrrr rrrrrrrrrr$rr*r,r0r5r;r@rBr?rGrIrNrPrSrVrXr_rcr6rhrlrprr-r1r/rrs66I T@l**X  @B4-'@%$%$%)$(4O0d+ 2/h7!,!*,TlH B46nH9!vB8"#H764I!V/89 7I $ ZL'--+++# &**&"  <6| 2 4*/XS4jv?pVp4@2;z ' 8D.:."$KaF(!T !D%2  4FR "KO= IV$ '/  .9,20d0<1f!<  A")A"A"!<!<%.%.,\(TGB2$KN.) )TKr1rcptjt}||_||_||_||_|S)zCreate a decimal instance directly, without any validation, normalization (e.g. removal of leading zeros) or argument conversion. This function is for *internal use only*. )rrrrErFrr)rR coefficientrspecialr6s r/rDrDs4 >>' "DDJDIDID Kr1c"eZdZdZdZdZdZy)rwzContext manager class to support localcontext(). Sets a copy of the supplied context in __enter__() and restores the previous decimal context in __exit__() c.|j|_yr,)rsr|)r6r|s r/__init__z_ContextManager.__init__s&++-r1cbt|_t|j|jSr,)r saved_contextrr|rs r/ __enter__z_ContextManager.__enter__s&'\4##$r1c.t|jyr,)rr)r6tvtbs r/__exit__z_ContextManager.__exit__s4%%&r1N)r:r;r<r=rrrr-r1r/rwrws . 'r1rwceZdZdZ dTdZdZdZdZdZdZ d Z d Z d Z d Z d ZeZdUdZdZdZdZdZdZdZdZdVdZdZdZdZdZdZdZdZdZ dZ!dZ"d Z#d!Z$d"Z%d#Z&d$Z'd%Z(d&Z)d'Z*d(Z+d)Z,d*Z-d+Z.d,Z/d-Z0d.Z1d/Z2d0Z3d1Z4d2Z5d3Z6d4Z7d5Z8d6Z9d7Z:d8Z;d9Zd<Z?d=Z@d>ZAd?ZBd@ZCdAZDdBZEdCZFdDZGdUdEZHdFZIdGZJdHZKdIZLdJZMdKZNdLZOdMZPdNZQdOZRdPZSdQZTdRZUdSZVeVZWy)WraContains the context for a Decimal instance. Contains: prec - precision (for use in rounding, division, square roots..) rounding - rounding type (how you round) traps - If traps[exception] = 1, then the exception is raised when it is caused. Otherwise, a value is substituted in. flags - When an exception is caused, flags[exception] is set. (Whether or not the trap_enabler is set) Should be reset by user of Decimal instance. Emin - Minimum exponent Emax - Maximum exponent capitals - If 1, 1*10^1 is printed as 1E+1. If 0, printed as 1e1 clamp - If 1, change exponents if too high (Default 0) Nc  t} ||n  j|_||n  j|_||n  j|_||n  j |_||n  j |_||n  j|_| g|_n| |_  jj|_ n8tts!tfdtzD|_ n|_  tjtd|_ytts!tfdtzD|_y|_y#t$rYPwxYw)Nc3<K|]}|t|vfywr,r).0rXrks r/ z#Context.__init__..!M.rr)r NameErrorrar`rgrbrhri_ignored_flagsrkrsrrrfromkeysrj) r6rar`rgrbrhrirjrkrdcs `` r/rzContext.__init__s"  B!,D"'' $,$8bkk  ,D"''  ,D"'' $,$8bkk #/URXX  !"$D "0D  =DJE4(MHut| !BdDRVX]E^!^__ U]t| !BdDRVX]E^!^__t|ut| !AT4QUW\D]!]^^!!$e44r1ct|tstd|z|D]}|tvs t d|ztD]}||vst d|zt j |||S)Nz%s must be a signal dictz%s is not a valid signal dict)rrrzrKeyErrorrr)r6rrrs r/_set_signal_dictzContext._set_signal_dictsz!T"6:; ;C(?>BCCC!8>BCC!!$a00r1c|dk(r|j||ddS|dk(r|j||ddS|dk(r|j||ddS|dk(r|j||ddS|d k(r|j||ddS|d k(r-|tvrtd |ztj |||S|d k(s|d k(r|j ||S|dk(rtj |||St d|z)Nrar2rUrgrr(rbrhrir`z%s: invalid rounding moderjrkrz.'decimal.Context' object has no attribute '%s')r_rounding_modesrzrrrAttributeError)r6rrs r/rzContext.__setattr__%s& 6>**45A A V^**4B B V^**45A A Z **41= = W_**41= = Z O+ ;e CDD%%dD%8 8 W_((u5 5 % %%%dD%8 8 @4GI Ir1ctd|z)Nz%s cannot be deleted)r)r6rs r/ __delattr__zContext.__delattr__>s3d:;;r1c z|jjDcgc] \}}|s | }}}|jjDcgc] \}}|s | }}}|j|j|j |j |j|j|j||ffScc}}wcc}}wr,) rjrxrkrgrar`rgrbrhri)r6sigrrjrks r/rhzContext.__reduce__Bs#'::#3#3#5;#5a#5;#'::#3#3#5;#5a#5;DMM499dii E5:; ;<;s B1B1 B7B7cg}|jdt|z|jjDcgc]\}}|s |j}}}|jddj |zdz|j jDcgc]\}}|s |j}}}|jddj |zdzdj |dzScc}}wcc}}w)zShow the current context.zrContext(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d, clamp=%(clamp)dzflags=[, ]ztraps=[))rvarsrjrxr:rrk)r6rXrrnamesrs r/rzContext.__repr__Is  #: )- (8(8(:@(:1a(:@ TYYu--34(, (8(8(:@(:1a(:@ TYYu--34yy|c!! A@s C2C2 C8'C8cD|jD]}d|j|<y)zReset all flags to zeror(N)rjr6flags r/rtzContext.clear_flagsVJJD DJJt r1cD|jD]}d|j|<y)zReset all traps to zeror(N)rkrs r/ clear_trapszContext.clear_traps[rr1c t|j|j|j|j|j |j |j|j|j }|S)z!Returns a shallow copy from self.) rrar`rgrbrhrirjrkrr6ncs r/rzContext._shallow_copy`sM TYY tyy$))]]DJJ DJJ((* r1c t|j|j|j|j|j |j |jj|jj|j }|S)zReturns a deep copy from self.) rrar`rgrbrhrirjrsrkrrs r/rsz Context.copygs\ TYY tyy$))]]DJJZZ__& (9((* r1ctj||}||jvr|j|g|Sd|j|<|j |s|j|g|S||)a#Handles an error If the flag is in _ignored_flags, returns the default response. Otherwise, it sets the flag, then, if the corresponding trap_enabler is set, it reraises the exception. Otherwise, it returns the default value after setting the flag. r2)_condition_maprnrr8rjrk)r6 condition explanationr.errors r/rzContext._raise_errorps}""9i8 D'' '!57>>$.. . 5zz% %9;%%d2T2 2K  r1c(|jtS)z$Ignore all flags, if they are raised) _ignore_flagsrrs r/rKzContext._ignore_all_flagss!t!!8,,r1cR|jt|z|_t|S)z$Ignore the flags, if they are raised)rr)r6rjs r/rzContext._ignore_flagss% $22T%[@E{r1c|rt|dttfr|d}|D]}|jj |y)z+Stop ignoring the flags, if they are raisedr(N)rrrrremove)r6rjrs r/ _regard_flagszContext._regard_flagss= Za5,7!HED    & &t ,r1cLt|j|jz dzS)z!Returns Etiny (= Emin - prec + 1)r2)rrgrars r/r4z Context.Etiny499tyy(1,--r1cLt|j|jz dzS)z,Returns maximum exponent (= Emax - prec + 1)r2)rrbrars r/rkz Context.Etoprr1c,|j}||_|S)aSets the rounding type. Sets the rounding type, and returns the current (previous) rounding type. Often used like: context = context.copy() # so you don't change the calling context # if an error occurs in the middle. rounding = context._set_rounding(ROUND_UP) val = self.__sub__(other, context=context) context._set_rounding(rounding) This will make it round up for that operation. )r`)r6rkr`s r/rzContext._set_roundings== r1cbt|tr-||jk7sd|vr|jtdSt ||}|j rEt|j|j|jz kDr|jtdS|j|S)zCreates a new Decimal instance but using self as context. This method implements the to-number operation of the IBM Decimal specification.rzAtrailing or leading whitespace and underscores are not permitted.rqzdiagnostic info too long in NaN) rrrrrrrrrFrarir)r6rrs r/create_decimalzContext.create_decimals c3 SCIIK%73#:$$%5&FG G C & 88:#aff+ DJJ(>>$$%5%FH Hvvd|r1cNtj|}|j|S)aCreates a new Decimal instance from a float but rounding using self as the context. >>> context = Context(prec=5, rounding=ROUND_DOWN) >>> context.create_decimal_from_float(3.1415926535897932) Decimal('3.1415') >>> context = Context(prec=5, traps=[Inexact]) >>> context.create_decimal_from_float(3.1415926535897932) Traceback (most recent call last): ... decimal.Inexact: None )rrr)r6rrs r/create_decimal_from_floatz!Context.create_decimal_from_floats"   q !vvd|r1c@t|d}|j|S)a[Returns the absolute value of the operand. If the operand is negative, the result is the same as using the minus operation on the operand. Otherwise, the result is the same as using the plus operation on the operand. >>> ExtendedContext.abs(Decimal('2.1')) Decimal('2.1') >>> ExtendedContext.abs(Decimal('-100')) Decimal('100') >>> ExtendedContext.abs(Decimal('101.5')) Decimal('101.5') >>> ExtendedContext.abs(Decimal('-101.5')) Decimal('101.5') >>> ExtendedContext.abs(-1) Decimal('1') Trrq)rr!r6rs r/rz Context.abss!$ 1d +yyy&&r1crt|d}|j||}|turtd|z|S)aReturn the sum of the two operands. >>> ExtendedContext.add(Decimal('12'), Decimal('7.00')) Decimal('19.00') >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4')) Decimal('1.02E+4') >>> ExtendedContext.add(1, Decimal(2)) Decimal('3') >>> ExtendedContext.add(Decimal(8), 5) Decimal('13') >>> ExtendedContext.add(5, 5) Decimal('10') TrrqUnable to convert %s to Decimal)rr)rrzr6rr:r=s r/addz Context.adds@ 1d + IIaI &  =AB BHr1c6t|j|Sr,)rrrs r/_applyzContext._applys166$<  r1cXt|ts td|jS)zReturns the same Decimal object. As we do not have different encodings for the same number, the received object already is in its canonical form. >>> ExtendedContext.canonical(Decimal('2.50')) Decimal('2.50') z,canonical requires a Decimal as an argument.)rrrzrrs r/rzContext.canonicals&!W%JK K{{}r1cBt|d}|j||S)aCompares values numerically. If the signs of the operands differ, a value representing each operand ('-1' if the operand is less than zero, '0' if the operand is zero or negative zero, or '1' if the operand is greater than zero) is used in place of that operand for the comparison instead of the actual operand. The comparison is then effected by subtracting the second operand from the first and then returning a value according to the result of the subtraction: '-1' if the result is less than zero, '0' if the result is zero or negative zero, or '1' if the result is greater than zero. >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3')) Decimal('-1') >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1')) Decimal('0') >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10')) Decimal('0') >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1')) Decimal('1') >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3')) Decimal('1') >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1')) Decimal('-1') >>> ExtendedContext.compare(1, 2) Decimal('-1') >>> ExtendedContext.compare(Decimal(1), 2) Decimal('-1') >>> ExtendedContext.compare(1, Decimal(2)) Decimal('-1') Trrq)rrr6rr:s r/rzContext.compares$B 1d +yyDy))r1cBt|d}|j||S)aCompares the values of the two operands numerically. It's pretty much like compare(), but all NaNs signal, with signaling NaNs taking precedence over quiet NaNs. >>> c = ExtendedContext >>> c.compare_signal(Decimal('2.1'), Decimal('3')) Decimal('-1') >>> c.compare_signal(Decimal('2.1'), Decimal('2.1')) Decimal('0') >>> c.flags[InvalidOperation] = 0 >>> print(c.flags[InvalidOperation]) 0 >>> c.compare_signal(Decimal('NaN'), Decimal('2.1')) Decimal('NaN') >>> print(c.flags[InvalidOperation]) 1 >>> c.flags[InvalidOperation] = 0 >>> print(c.flags[InvalidOperation]) 0 >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1')) Decimal('NaN') >>> print(c.flags[InvalidOperation]) 1 >>> c.compare_signal(-1, 2) Decimal('-1') >>> c.compare_signal(Decimal(-1), 2) Decimal('-1') >>> c.compare_signal(-1, Decimal(2)) Decimal('-1') Trrq)rrrs r/rzContext.compare_signal7s'@ 1d +400r1c>t|d}|j|S)a+Compares two operands using their abstract representation. This is not like the standard compare, which use their numerical value. Note that a total ordering is defined for all possible abstract representations. >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9')) Decimal('-1') >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12')) Decimal('-1') >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3')) Decimal('-1') >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30')) Decimal('0') >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300')) Decimal('1') >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN')) Decimal('-1') >>> ExtendedContext.compare_total(1, 2) Decimal('-1') >>> ExtendedContext.compare_total(Decimal(1), 2) Decimal('-1') >>> ExtendedContext.compare_total(1, Decimal(2)) Decimal('-1') Tr)rrrs r/rzContext.compare_totalZs4 1d +q!!r1c>t|d}|j|S)zCompares two operands using their abstract representation ignoring sign. Like compare_total, but with operand's sign ignored and assumed to be 0. Tr)rrrs r/rzContext.compare_total_magws! 1d +""1%%r1c<t|d}|jS)aReturns a copy of the operand with the sign set to 0. >>> ExtendedContext.copy_abs(Decimal('2.1')) Decimal('2.1') >>> ExtendedContext.copy_abs(Decimal('-100')) Decimal('100') >>> ExtendedContext.copy_abs(-1) Decimal('1') Tr)rrrs r/rzContext.copy_abss 1d +zz|r1c2t|d}t|S)aReturns a copy of the decimal object. >>> ExtendedContext.copy_decimal(Decimal('2.1')) Decimal('2.1') >>> ExtendedContext.copy_decimal(Decimal('-1.00')) Decimal('-1.00') >>> ExtendedContext.copy_decimal(1) Decimal('1') Tr)rrrs r/ copy_decimalzContext.copy_decimals 1d +qzr1c<t|d}|jS)a(Returns a copy of the operand with the sign inverted. >>> ExtendedContext.copy_negate(Decimal('101.5')) Decimal('-101.5') >>> ExtendedContext.copy_negate(Decimal('-101.5')) Decimal('101.5') >>> ExtendedContext.copy_negate(1) Decimal('-1') Tr)rrrs r/rzContext.copy_negates 1d +}}r1c>t|d}|j|S)aCopies the second operand's sign to the first one. In detail, it returns a copy of the first operand with the sign equal to the sign of the second operand. >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33')) Decimal('1.50') >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33')) Decimal('1.50') >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33')) Decimal('-1.50') >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33')) Decimal('-1.50') >>> ExtendedContext.copy_sign(1, -2) Decimal('-1') >>> ExtendedContext.copy_sign(Decimal(1), -2) Decimal('-1') >>> ExtendedContext.copy_sign(1, Decimal(-2)) Decimal('-1') Tr)rrrs r/rzContext.copy_signs* 1d +{{1~r1crt|d}|j||}|turtd|z|S)aDecimal division in a specified context. >>> ExtendedContext.divide(Decimal('1'), Decimal('3')) Decimal('0.333333333') >>> ExtendedContext.divide(Decimal('2'), Decimal('3')) Decimal('0.666666667') >>> ExtendedContext.divide(Decimal('5'), Decimal('2')) Decimal('2.5') >>> ExtendedContext.divide(Decimal('1'), Decimal('10')) Decimal('0.1') >>> ExtendedContext.divide(Decimal('12'), Decimal('12')) Decimal('1') >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2')) Decimal('4.00') >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0')) Decimal('1.20') >>> ExtendedContext.divide(Decimal('1000'), Decimal('100')) Decimal('10') >>> ExtendedContext.divide(Decimal('1000'), Decimal('1')) Decimal('1000') >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2')) Decimal('1.20E+6') >>> ExtendedContext.divide(5, 5) Decimal('1') >>> ExtendedContext.divide(Decimal(5), 5) Decimal('1') >>> ExtendedContext.divide(5, Decimal(5)) Decimal('1') Trrqr)rr9rrzrs r/dividezContext.divides@< 1d + MM!TM *  =AB BHr1crt|d}|j||}|turtd|z|S)a/Divides two numbers and returns the integer part of the result. >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3')) Decimal('0') >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3')) Decimal('3') >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3')) Decimal('3') >>> ExtendedContext.divide_int(10, 3) Decimal('3') >>> ExtendedContext.divide_int(Decimal(10), 3) Decimal('3') >>> ExtendedContext.divide_int(10, Decimal(3)) Decimal('3') Trrqr)rrRrrzrs r/ divide_intzContext.divide_ints@ 1d + NN1dN +  =AB BHr1crt|d}|j||}|turtd|z|S)aReturn (a // b, a % b). >>> ExtendedContext.divmod(Decimal(8), Decimal(3)) (Decimal('2'), Decimal('2')) >>> ExtendedContext.divmod(Decimal(8), Decimal(4)) (Decimal('2'), Decimal('0')) >>> ExtendedContext.divmod(8, 4) (Decimal('2'), Decimal('0')) >>> ExtendedContext.divmod(Decimal(8), 4) (Decimal('2'), Decimal('0')) >>> ExtendedContext.divmod(8, Decimal(4)) (Decimal('2'), Decimal('0')) Trrqr)rrFrrzrs r/r5zContext.divmods@ 1d + LLDL )  =AB BHr1c@t|d}|j|S)a#Returns e ** a. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.exp(Decimal('-Infinity')) Decimal('0') >>> c.exp(Decimal('-1')) Decimal('0.367879441') >>> c.exp(Decimal('0')) Decimal('1') >>> c.exp(Decimal('1')) Decimal('2.71828183') >>> c.exp(Decimal('0.693147181')) Decimal('2.00000000') >>> c.exp(Decimal('+Infinity')) Decimal('Infinity') >>> c.exp(10) Decimal('22026.4658') Trrq)rrrs r/rz Context.exps!* !T *uuTu""r1cDt|d}|j|||S)a Returns a multiplied by b, plus c. The first two operands are multiplied together, using multiply, the third operand is then added to the result of that multiplication, using add, all with only one final rounding. >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7')) Decimal('22') >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7')) Decimal('-8') >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578')) Decimal('1.38435736E+12') >>> ExtendedContext.fma(1, 3, 4) Decimal('7') >>> ExtendedContext.fma(1, Decimal(3), 4) Decimal('7') >>> ExtendedContext.fma(1, 3, Decimal(4)) Decimal('7') Trrq)rr)r6rr:rs r/rz Context.fma's%( 1d +uuQ4u((r1cXt|ts td|jS)aReturn True if the operand is canonical; otherwise return False. Currently, the encoding of a Decimal instance is always canonical, so this method returns True for any Decimal. >>> ExtendedContext.is_canonical(Decimal('2.50')) True z/is_canonical requires a Decimal as an argument.)rrrzrrs r/rzContext.is_canonical>s'!W%MN N~~r1c<t|d}|jS)a,Return True if the operand is finite; otherwise return False. A Decimal instance is considered finite if it is neither infinite nor a NaN. >>> ExtendedContext.is_finite(Decimal('2.50')) True >>> ExtendedContext.is_finite(Decimal('-0.3')) True >>> ExtendedContext.is_finite(Decimal('0')) True >>> ExtendedContext.is_finite(Decimal('Inf')) False >>> ExtendedContext.is_finite(Decimal('NaN')) False >>> ExtendedContext.is_finite(1) True Tr)rr rs r/r zContext.is_finiteKs& 1d +{{}r1c<t|d}|jS)aUReturn True if the operand is infinite; otherwise return False. >>> ExtendedContext.is_infinite(Decimal('2.50')) False >>> ExtendedContext.is_infinite(Decimal('-Inf')) True >>> ExtendedContext.is_infinite(Decimal('NaN')) False >>> ExtendedContext.is_infinite(1) False Tr)rrrs r/rzContext.is_infiniteas 1d +}}r1c<t|d}|jS)aOReturn True if the operand is a qNaN or sNaN; otherwise return False. >>> ExtendedContext.is_nan(Decimal('2.50')) False >>> ExtendedContext.is_nan(Decimal('NaN')) True >>> ExtendedContext.is_nan(Decimal('-sNaN')) True >>> ExtendedContext.is_nan(1) False Tr)rrrs r/rzContext.is_nanps 1d +xxzr1c@t|d}|j|S)aReturn True if the operand is a normal number; otherwise return False. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.is_normal(Decimal('2.50')) True >>> c.is_normal(Decimal('0.1E-999')) False >>> c.is_normal(Decimal('0.00')) False >>> c.is_normal(Decimal('-Inf')) False >>> c.is_normal(Decimal('NaN')) False >>> c.is_normal(1) True Trrq)rrrs r/rzContext.is_normals!( 1d +{{4{((r1c<t|d}|jS)aHReturn True if the operand is a quiet NaN; otherwise return False. >>> ExtendedContext.is_qnan(Decimal('2.50')) False >>> ExtendedContext.is_qnan(Decimal('NaN')) True >>> ExtendedContext.is_qnan(Decimal('sNaN')) False >>> ExtendedContext.is_qnan(1) False Tr)rrrs r/rzContext.is_qnans 1d +yy{r1c<t|d}|jS)aReturn True if the operand is negative; otherwise return False. >>> ExtendedContext.is_signed(Decimal('2.50')) False >>> ExtendedContext.is_signed(Decimal('-12')) True >>> ExtendedContext.is_signed(Decimal('-0')) True >>> ExtendedContext.is_signed(8) False >>> ExtendedContext.is_signed(-8) True Tr)rrrs r/rzContext.is_signeds 1d +{{}r1c<t|d}|jS)aTReturn True if the operand is a signaling NaN; otherwise return False. >>> ExtendedContext.is_snan(Decimal('2.50')) False >>> ExtendedContext.is_snan(Decimal('NaN')) False >>> ExtendedContext.is_snan(Decimal('sNaN')) True >>> ExtendedContext.is_snan(1) False Tr)rrrs r/rzContext.is_snans 1d +yy{r1c@t|d}|j|S)aReturn True if the operand is subnormal; otherwise return False. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.is_subnormal(Decimal('2.50')) False >>> c.is_subnormal(Decimal('0.1E-999')) True >>> c.is_subnormal(Decimal('0.00')) False >>> c.is_subnormal(Decimal('-Inf')) False >>> c.is_subnormal(Decimal('NaN')) False >>> c.is_subnormal(1) False Trrq)rrrs r/rzContext.is_subnormals!& 1d +~~d~++r1c<t|d}|jS)auReturn True if the operand is a zero; otherwise return False. >>> ExtendedContext.is_zero(Decimal('0')) True >>> ExtendedContext.is_zero(Decimal('2.50')) False >>> ExtendedContext.is_zero(Decimal('-0E+2')) True >>> ExtendedContext.is_zero(1) False >>> ExtendedContext.is_zero(0) True Tr)rrrs r/rzContext.is_zeros 1d +yy{r1c@t|d}|j|S)aReturns the natural (base e) logarithm of the operand. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.ln(Decimal('0')) Decimal('-Infinity') >>> c.ln(Decimal('1.000')) Decimal('0') >>> c.ln(Decimal('2.71828183')) Decimal('1.00000000') >>> c.ln(Decimal('10')) Decimal('2.30258509') >>> c.ln(Decimal('+Infinity')) Decimal('Infinity') >>> c.ln(1) Decimal('0') Trrq)rr$rs r/r$z Context.lns!& 1d +ttDt!!r1c@t|d}|j|S)aReturns the base 10 logarithm of the operand. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.log10(Decimal('0')) Decimal('-Infinity') >>> c.log10(Decimal('0.001')) Decimal('-3') >>> c.log10(Decimal('1.000')) Decimal('0') >>> c.log10(Decimal('2')) Decimal('0.301029996') >>> c.log10(Decimal('10')) Decimal('1') >>> c.log10(Decimal('70')) Decimal('1.84509804') >>> c.log10(Decimal('+Infinity')) Decimal('Infinity') >>> c.log10(0) Decimal('-Infinity') >>> c.log10(1) Decimal('0') Trrq)rr*rs r/r*z Context.log10s!2 1d +wwtw$$r1c@t|d}|j|S)a4 Returns the exponent of the magnitude of the operand's MSD. The result is the integer which is the exponent of the magnitude of the most significant digit of the operand (as though the operand were truncated to a single digit while maintaining the value of that digit and without limiting the resulting exponent). >>> ExtendedContext.logb(Decimal('250')) Decimal('2') >>> ExtendedContext.logb(Decimal('2.50')) Decimal('0') >>> ExtendedContext.logb(Decimal('0.03')) Decimal('-2') >>> ExtendedContext.logb(Decimal('0')) Decimal('-Infinity') >>> ExtendedContext.logb(1) Decimal('0') >>> ExtendedContext.logb(10) Decimal('1') >>> ExtendedContext.logb(100) Decimal('2') Trrq)rr,rs r/r,z Context.logb s!. 1d +vvdv##r1cBt|d}|j||S)aApplies the logical operation 'and' between each operand's digits. The operands must be both logical numbers. >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0')) Decimal('0') >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1')) Decimal('0') >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0')) Decimal('0') >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1')) Decimal('1') >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010')) Decimal('1000') >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10')) Decimal('10') >>> ExtendedContext.logical_and(110, 1101) Decimal('100') >>> ExtendedContext.logical_and(Decimal(110), 1101) Decimal('100') >>> ExtendedContext.logical_and(110, Decimal(1101)) Decimal('100') Trrq)rr;rs r/r;zContext.logical_and:#0 1d +}}Q}--r1c@t|d}|j|S)a Invert all the digits in the operand. The operand must be a logical number. >>> ExtendedContext.logical_invert(Decimal('0')) Decimal('111111111') >>> ExtendedContext.logical_invert(Decimal('1')) Decimal('111111110') >>> ExtendedContext.logical_invert(Decimal('111111111')) Decimal('0') >>> ExtendedContext.logical_invert(Decimal('101010101')) Decimal('10101010') >>> ExtendedContext.logical_invert(1101) Decimal('111110010') Trrq)rr@rs r/r@zContext.logical_invertUs$ 1d +--r1cBt|d}|j||S)aApplies the logical operation 'or' between each operand's digits. The operands must be both logical numbers. >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0')) Decimal('0') >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1')) Decimal('1') >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0')) Decimal('1') >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1')) Decimal('1') >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010')) Decimal('1110') >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10')) Decimal('1110') >>> ExtendedContext.logical_or(110, 1101) Decimal('1111') >>> ExtendedContext.logical_or(Decimal(110), 1101) Decimal('1111') >>> ExtendedContext.logical_or(110, Decimal(1101)) Decimal('1111') Trrq)rrBrs r/rBzContext.logical_orhs#0 1d +||At|,,r1cBt|d}|j||S)aApplies the logical operation 'xor' between each operand's digits. The operands must be both logical numbers. >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0')) Decimal('0') >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1')) Decimal('1') >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0')) Decimal('1') >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1')) Decimal('0') >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010')) Decimal('110') >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10')) Decimal('1101') >>> ExtendedContext.logical_xor(110, 1101) Decimal('1011') >>> ExtendedContext.logical_xor(Decimal(110), 1101) Decimal('1011') >>> ExtendedContext.logical_xor(110, Decimal(1101)) Decimal('1011') Trrq)rr?rs r/r?zContext.logical_xorrr1cBt|d}|j||S)amax compares two values numerically and returns the maximum. If either operand is a NaN then the general rules apply. Otherwise, the operands are compared as though by the compare operation. If they are numerically equal then the left-hand operand is chosen as the result. Otherwise the maximum (closer to positive infinity) of the two operands is chosen as the result. >>> ExtendedContext.max(Decimal('3'), Decimal('2')) Decimal('3') >>> ExtendedContext.max(Decimal('-10'), Decimal('3')) Decimal('3') >>> ExtendedContext.max(Decimal('1.0'), Decimal('1')) Decimal('1') >>> ExtendedContext.max(Decimal('7'), Decimal('NaN')) Decimal('7') >>> ExtendedContext.max(1, 2) Decimal('2') >>> ExtendedContext.max(Decimal(1), 2) Decimal('2') >>> ExtendedContext.max(1, Decimal(2)) Decimal('2') Trrq)rr#rs r/r#z Context.max#0 1d +uuQu%%r1cBt|d}|j||S)aCompares the values numerically with their sign ignored. >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN')) Decimal('7') >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10')) Decimal('-10') >>> ExtendedContext.max_mag(1, -2) Decimal('-2') >>> ExtendedContext.max_mag(Decimal(1), -2) Decimal('-2') >>> ExtendedContext.max_mag(1, Decimal(-2)) Decimal('-2') Trrq)rrGrs r/rGzContext.max_mag# 1d +yyDy))r1cBt|d}|j||S)amin compares two values numerically and returns the minimum. If either operand is a NaN then the general rules apply. Otherwise, the operands are compared as though by the compare operation. If they are numerically equal then the left-hand operand is chosen as the result. Otherwise the minimum (closer to negative infinity) of the two operands is chosen as the result. >>> ExtendedContext.min(Decimal('3'), Decimal('2')) Decimal('2') >>> ExtendedContext.min(Decimal('-10'), Decimal('3')) Decimal('-10') >>> ExtendedContext.min(Decimal('1.0'), Decimal('1')) Decimal('1.0') >>> ExtendedContext.min(Decimal('7'), Decimal('NaN')) Decimal('7') >>> ExtendedContext.min(1, 2) Decimal('1') >>> ExtendedContext.min(Decimal(1), 2) Decimal('1') >>> ExtendedContext.min(1, Decimal(29)) Decimal('1') Trrq)rrrs r/rz Context.minrr1cBt|d}|j||S)aCompares the values numerically with their sign ignored. >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2')) Decimal('-2') >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN')) Decimal('-3') >>> ExtendedContext.min_mag(1, -2) Decimal('1') >>> ExtendedContext.min_mag(Decimal(1), -2) Decimal('1') >>> ExtendedContext.min_mag(1, Decimal(-2)) Decimal('1') Trrq)rrIrs r/rIzContext.min_magr r1c@t|d}|j|S)aMinus corresponds to unary prefix minus in Python. The operation is evaluated using the same rules as subtract; the operation minus(a) is calculated as subtract('0', a) where the '0' has the same exponent as the operand. >>> ExtendedContext.minus(Decimal('1.3')) Decimal('-1.3') >>> ExtendedContext.minus(Decimal('-1.3')) Decimal('1.3') >>> ExtendedContext.minus(1) Decimal('-1') Trrq)rrrs r/minusz Context.minus! 1d +yyy&&r1crt|d}|j||}|turtd|z|S)amultiply multiplies two operands. If either operand is a special value then the general rules apply. Otherwise, the operands are multiplied together ('long multiplication'), resulting in a number which may be as long as the sum of the lengths of the two operands. >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3')) Decimal('3.60') >>> ExtendedContext.multiply(Decimal('7'), Decimal('3')) Decimal('21') >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8')) Decimal('0.72') >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0')) Decimal('-0.0') >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321')) Decimal('4.28135971E+11') >>> ExtendedContext.multiply(7, 7) Decimal('49') >>> ExtendedContext.multiply(Decimal(7), 7) Decimal('49') >>> ExtendedContext.multiply(7, Decimal(7)) Decimal('49') Trrqr)rr2rrzrs r/multiplyzContext.multiplys@2 1d + IIaI &  =AB BHr1c@t|d}|j|S)a"Returns the largest representable number smaller than a. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> ExtendedContext.next_minus(Decimal('1')) Decimal('0.999999999') >>> c.next_minus(Decimal('1E-1007')) Decimal('0E-1007') >>> ExtendedContext.next_minus(Decimal('-1.00000003')) Decimal('-1.00000004') >>> c.next_minus(Decimal('Infinity')) Decimal('9.99999999E+999') >>> c.next_minus(1) Decimal('0.999999999') Trrq)rrNrs r/rNzContext.next_minus's!" 1d +||D|))r1c@t|d}|j|S)aReturns the smallest representable number larger than a. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> ExtendedContext.next_plus(Decimal('1')) Decimal('1.00000001') >>> c.next_plus(Decimal('-1E-1007')) Decimal('-0E-1007') >>> ExtendedContext.next_plus(Decimal('-1.00000003')) Decimal('-1.00000002') >>> c.next_plus(Decimal('-Infinity')) Decimal('-9.99999999E+999') >>> c.next_plus(1) Decimal('1.00000001') Trrq)rrPrs r/rPzContext.next_plus;s!" 1d +{{4{((r1cBt|d}|j||S)aReturns the number closest to a, in direction towards b. The result is the closest representable number from the first operand (but not the first operand) that is in the direction towards the second operand, unless the operands have the same value. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.next_toward(Decimal('1'), Decimal('2')) Decimal('1.00000001') >>> c.next_toward(Decimal('-1E-1007'), Decimal('1')) Decimal('-0E-1007') >>> c.next_toward(Decimal('-1.00000003'), Decimal('0')) Decimal('-1.00000002') >>> c.next_toward(Decimal('1'), Decimal('0')) Decimal('0.999999999') >>> c.next_toward(Decimal('1E-1007'), Decimal('-100')) Decimal('0E-1007') >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10')) Decimal('-1.00000004') >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000')) Decimal('-0.00') >>> c.next_toward(0, 1) Decimal('1E-1007') >>> c.next_toward(Decimal(0), 1) Decimal('1E-1007') >>> c.next_toward(0, Decimal(1)) Decimal('1E-1007') Trrq)rrSrs r/rSzContext.next_towardOs$@ 1d +}}Q}--r1c@t|d}|j|S)anormalize reduces an operand to its simplest form. Essentially a plus operation with all trailing zeros removed from the result. >>> ExtendedContext.normalize(Decimal('2.1')) Decimal('2.1') >>> ExtendedContext.normalize(Decimal('-2.0')) Decimal('-2') >>> ExtendedContext.normalize(Decimal('1.200')) Decimal('1.2') >>> ExtendedContext.normalize(Decimal('-120')) Decimal('-1.2E+2') >>> ExtendedContext.normalize(Decimal('120.00')) Decimal('1.2E+2') >>> ExtendedContext.normalize(Decimal('0.00')) Decimal('0') >>> ExtendedContext.normalize(6) Decimal('6') Trrq)rrrs r/rzContext.normalizers!* 1d +{{4{((r1c@t|d}|j|S)aReturns an indication of the class of the operand. The class is one of the following strings: -sNaN -NaN -Infinity -Normal -Subnormal -Zero +Zero +Subnormal +Normal +Infinity >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.number_class(Decimal('Infinity')) '+Infinity' >>> c.number_class(Decimal('1E-10')) '+Normal' >>> c.number_class(Decimal('2.50')) '+Normal' >>> c.number_class(Decimal('0.1E-999')) '+Subnormal' >>> c.number_class(Decimal('0')) '+Zero' >>> c.number_class(Decimal('-0')) '-Zero' >>> c.number_class(Decimal('-0.1E-999')) '-Subnormal' >>> c.number_class(Decimal('-1E-10')) '-Normal' >>> c.number_class(Decimal('-2.50')) '-Normal' >>> c.number_class(Decimal('-Infinity')) '-Infinity' >>> c.number_class(Decimal('NaN')) 'NaN' >>> c.number_class(Decimal('-NaN')) 'NaN' >>> c.number_class(Decimal('sNaN')) 'sNaN' >>> c.number_class(123) '+Normal' Trrq)rrVrs r/rVzContext.number_classs"^ 1d +~~d~++r1c@t|d}|j|S)aPlus corresponds to unary prefix plus in Python. The operation is evaluated using the same rules as add; the operation plus(a) is calculated as add('0', a) where the '0' has the same exponent as the operand. >>> ExtendedContext.plus(Decimal('1.3')) Decimal('1.3') >>> ExtendedContext.plus(Decimal('-1.3')) Decimal('-1.3') >>> ExtendedContext.plus(-1) Decimal('-1') Trrq)rrrs r/plusz Context.plusrr1ctt|d}|j|||}|turtd|z|S)a Raises a to the power of b, to modulo if given. With two arguments, compute a**b. If a is negative then b must be integral. The result will be inexact unless b is integral and the result is finite and can be expressed exactly in 'precision' digits. With three arguments, compute (a**b) % modulo. For the three argument form, the following restrictions on the arguments hold: - all three arguments must be integral - b must be nonnegative - at least one of a or b must be nonzero - modulo must be nonzero and have at most 'precision' digits The result of pow(a, b, modulo) is identical to the result that would be obtained by computing (a**b) % modulo with unbounded precision, but is computed more efficiently. It is always exact. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.power(Decimal('2'), Decimal('3')) Decimal('8') >>> c.power(Decimal('-2'), Decimal('3')) Decimal('-8') >>> c.power(Decimal('2'), Decimal('-3')) Decimal('0.125') >>> c.power(Decimal('1.7'), Decimal('8')) Decimal('69.7575744') >>> c.power(Decimal('10'), Decimal('0.301029996')) Decimal('2.00000000') >>> c.power(Decimal('Infinity'), Decimal('-1')) Decimal('0') >>> c.power(Decimal('Infinity'), Decimal('0')) Decimal('1') >>> c.power(Decimal('Infinity'), Decimal('1')) Decimal('Infinity') >>> c.power(Decimal('-Infinity'), Decimal('-1')) Decimal('-0') >>> c.power(Decimal('-Infinity'), Decimal('0')) Decimal('1') >>> c.power(Decimal('-Infinity'), Decimal('1')) Decimal('-Infinity') >>> c.power(Decimal('-Infinity'), Decimal('2')) Decimal('Infinity') >>> c.power(Decimal('0'), Decimal('0')) Decimal('NaN') >>> c.power(Decimal('3'), Decimal('7'), Decimal('16')) Decimal('11') >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16')) Decimal('-11') >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16')) Decimal('1') >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16')) Decimal('11') >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789')) Decimal('11729830') >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729')) Decimal('-0') >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537')) Decimal('1') >>> ExtendedContext.power(7, 7) Decimal('823543') >>> ExtendedContext.power(Decimal(7), 7) Decimal('823543') >>> ExtendedContext.power(7, Decimal(7), 2) Decimal('1') Trrqr)rrrrz)r6rr:rr=s r/powerz Context.powersCR 1d + IIaI .  =AB BHr1cBt|d}|j||S)a Returns a value equal to 'a' (rounded), having the exponent of 'b'. The coefficient of the result is derived from that of the left-hand operand. It may be rounded using the current rounding setting (if the exponent is being increased), multiplied by a positive power of ten (if the exponent is being decreased), or is unchanged (if the exponent is already equal to that of the right-hand operand). Unlike other operations, if the length of the coefficient after the quantize operation would be greater than precision then an Invalid operation condition is raised. This guarantees that, unless there is an error condition, the exponent of the result of a quantize is always equal to that of the right-hand operand. Also unlike other operations, quantize will never raise Underflow, even if the result is subnormal and inexact. >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001')) Decimal('2.170') >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01')) Decimal('2.17') >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1')) Decimal('2.2') >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0')) Decimal('2') >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1')) Decimal('0E+1') >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity')) Decimal('-Infinity') >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity')) Decimal('NaN') >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1')) Decimal('-0') >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5')) Decimal('-0E+5') >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2')) Decimal('NaN') >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2')) Decimal('NaN') >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1')) Decimal('217.0') >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0')) Decimal('217') >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1')) Decimal('2.2E+2') >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2')) Decimal('2E+2') >>> ExtendedContext.quantize(1, 2) Decimal('1') >>> ExtendedContext.quantize(Decimal(1), 2) Decimal('1') >>> ExtendedContext.quantize(1, Decimal(2)) Decimal('1') Trrq)rrrs r/rzContext.quantizes$n 1d +zz!Tz**r1ctdS)zkJust returns 10, as this is Decimal, :) >>> ExtendedContext.radix() Decimal('10') rr_rs r/rXz Context.radixWs r{r1crt|d}|j||}|turtd|z|S)aReturns the remainder from integer division. The result is the residue of the dividend after the operation of calculating integer division as described for divide-integer, rounded to precision digits if necessary. The sign of the result, if non-zero, is the same as that of the original dividend. This operation will fail under the same conditions as integer division (that is, if integer division on the same two operands would fail, the remainder cannot be calculated). >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3')) Decimal('2.1') >>> ExtendedContext.remainder(Decimal('10'), Decimal('3')) Decimal('1') >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3')) Decimal('-1') >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1')) Decimal('0.2') >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3')) Decimal('0.1') >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3')) Decimal('1.0') >>> ExtendedContext.remainder(22, 6) Decimal('4') >>> ExtendedContext.remainder(Decimal(22), 6) Decimal('4') >>> ExtendedContext.remainder(22, Decimal(6)) Decimal('4') Trrqr)rrJrrzrs r/r7zContext.remainder_s@> 1d + IIaI &  =AB BHr1cBt|d}|j||S)aGReturns to be "a - b * n", where n is the integer nearest the exact value of "x / b" (if two integers are equally near then the even one is chosen). If the result is equal to 0 then its sign will be the sign of a. This operation will fail under the same conditions as integer division (that is, if integer division on the same two operands would fail, the remainder cannot be calculated). >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3')) Decimal('-0.9') >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6')) Decimal('-2') >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3')) Decimal('1') >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3')) Decimal('-1') >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1')) Decimal('0.2') >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3')) Decimal('0.1') >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3')) Decimal('-0.3') >>> ExtendedContext.remainder_near(3, 11) Decimal('3') >>> ExtendedContext.remainder_near(Decimal(3), 11) Decimal('3') >>> ExtendedContext.remainder_near(3, Decimal(11)) Decimal('3') Trrq)rrPrs r/rPzContext.remainder_nears&> 1d +400r1cBt|d}|j||S)aNReturns a rotated copy of a, b times. The coefficient of the result is a rotated copy of the digits in the coefficient of the first operand. The number of places of rotation is taken from the absolute value of the second operand, with the rotation being to the left if the second operand is positive or to the right otherwise. >>> ExtendedContext.rotate(Decimal('34'), Decimal('8')) Decimal('400000003') >>> ExtendedContext.rotate(Decimal('12'), Decimal('9')) Decimal('12') >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2')) Decimal('891234567') >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0')) Decimal('123456789') >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2')) Decimal('345678912') >>> ExtendedContext.rotate(1333333, 1) Decimal('13333330') >>> ExtendedContext.rotate(Decimal(1333333), 1) Decimal('13333330') >>> ExtendedContext.rotate(1333333, Decimal(1)) Decimal('13333330') Trrq)rr_rs r/r_zContext.rotates#4 1d +xx4x((r1c>t|d}|j|S)aReturns True if the two operands have the same exponent. The result is never affected by either the sign or the coefficient of either operand. >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001')) False >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01')) True >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1')) False >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf')) True >>> ExtendedContext.same_quantum(10000, -1) True >>> ExtendedContext.same_quantum(Decimal(10000), -1) True >>> ExtendedContext.same_quantum(10000, Decimal(-1)) True Tr)rrrs r/rzContext.same_quantums* 1d +~~a  r1cBt|d}|j||S)a3Returns the first operand after adding the second value its exp. >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2')) Decimal('0.0750') >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0')) Decimal('7.50') >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3')) Decimal('7.50E+3') >>> ExtendedContext.scaleb(1, 4) Decimal('1E+4') >>> ExtendedContext.scaleb(Decimal(1), 4) Decimal('1E+4') >>> ExtendedContext.scaleb(1, Decimal(4)) Decimal('1E+4') Trrq)rrcrs r/rczContext.scalebs# 1d +xx4x((r1cBt|d}|j||S)a{Returns a shifted copy of a, b times. The coefficient of the result is a shifted copy of the digits in the coefficient of the first operand. The number of places to shift is taken from the absolute value of the second operand, with the shift being to the left if the second operand is positive or to the right otherwise. Digits shifted into the coefficient are zeros. >>> ExtendedContext.shift(Decimal('34'), Decimal('8')) Decimal('400000000') >>> ExtendedContext.shift(Decimal('12'), Decimal('9')) Decimal('0') >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2')) Decimal('1234567') >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0')) Decimal('123456789') >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2')) Decimal('345678900') >>> ExtendedContext.shift(88888888, 2) Decimal('888888800') >>> ExtendedContext.shift(Decimal(88888888), 2) Decimal('888888800') >>> ExtendedContext.shift(88888888, Decimal(2)) Decimal('888888800') Trrq)rr6rs r/r6z Context.shifts#6 1d +wwq$w''r1c@t|d}|j|S)aSquare root of a non-negative number to context precision. If the result must be inexact, it is rounded using the round-half-even algorithm. >>> ExtendedContext.sqrt(Decimal('0')) Decimal('0') >>> ExtendedContext.sqrt(Decimal('-0')) Decimal('-0') >>> ExtendedContext.sqrt(Decimal('0.39')) Decimal('0.624499800') >>> ExtendedContext.sqrt(Decimal('100')) Decimal('10') >>> ExtendedContext.sqrt(Decimal('1')) Decimal('1') >>> ExtendedContext.sqrt(Decimal('1.0')) Decimal('1.0') >>> ExtendedContext.sqrt(Decimal('1.00')) Decimal('1.0') >>> ExtendedContext.sqrt(Decimal('7')) Decimal('2.64575131') >>> ExtendedContext.sqrt(Decimal('10')) Decimal('3.16227766') >>> ExtendedContext.sqrt(2) Decimal('1.41421356') >>> ExtendedContext.prec 9 Trrq)rrrs r/rz Context.sqrt s!: 1d +vvdv##r1crt|d}|j||}|turtd|z|S)a&Return the difference between the two operands. >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07')) Decimal('0.23') >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30')) Decimal('0.00') >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07')) Decimal('-0.77') >>> ExtendedContext.subtract(8, 5) Decimal('3') >>> ExtendedContext.subtract(Decimal(8), 5) Decimal('3') >>> ExtendedContext.subtract(8, Decimal(5)) Decimal('3') Trrqr)rr+rrzrs r/subtractzContext.subtract-s@ 1d + IIaI &  =AB BHr1c@t|d}|j|S)aConvert to a string, using engineering notation if an exponent is needed. Engineering notation has an exponent which is a multiple of 3. This can leave up to 3 digits to the left of the decimal place and may require the addition of either one or two trailing zeros. The operation is not affected by the context. >>> ExtendedContext.to_eng_string(Decimal('123E+1')) '1.23E+3' >>> ExtendedContext.to_eng_string(Decimal('123E+3')) '123E+3' >>> ExtendedContext.to_eng_string(Decimal('123E-10')) '12.3E-9' >>> ExtendedContext.to_eng_string(Decimal('-123E-12')) '-123E-12' >>> ExtendedContext.to_eng_string(Decimal('7E-7')) '700E-9' >>> ExtendedContext.to_eng_string(Decimal('7E+1')) '70' >>> ExtendedContext.to_eng_string(Decimal('0E+1')) '0.00E+3' Trrq)rrrs r/rzContext.to_eng_stringDs!2 1d +t,,r1c@t|d}|j|S)zyConverts a number to a string, using scientific notation. The operation is not affected by the context. Trrq)rrrs r/ to_sci_stringzContext.to_sci_string`s! 1d +yyy&&r1c@t|d}|j|S)akRounds to an integer. When the operand has a negative exponent, the result is the same as using the quantize() operation using the given operand as the left-hand-operand, 1E+0 as the right-hand-operand, and the precision of the operand as the precision setting; Inexact and Rounded flags are allowed in this operation. The rounding mode is taken from the context. >>> ExtendedContext.to_integral_exact(Decimal('2.1')) Decimal('2') >>> ExtendedContext.to_integral_exact(Decimal('100')) Decimal('100') >>> ExtendedContext.to_integral_exact(Decimal('100.0')) Decimal('100') >>> ExtendedContext.to_integral_exact(Decimal('101.5')) Decimal('102') >>> ExtendedContext.to_integral_exact(Decimal('-101.5')) Decimal('-102') >>> ExtendedContext.to_integral_exact(Decimal('10E+5')) Decimal('1.0E+6') >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77')) Decimal('7.89E+77') >>> ExtendedContext.to_integral_exact(Decimal('-Inf')) Decimal('-Infinity') Trrq)rrrs r/rzContext.to_integral_exacths$6 1d +""4"00r1c@t|d}|j|S)aLRounds to an integer. When the operand has a negative exponent, the result is the same as using the quantize() operation using the given operand as the left-hand-operand, 1E+0 as the right-hand-operand, and the precision of the operand as the precision setting, except that no flags will be set. The rounding mode is taken from the context. >>> ExtendedContext.to_integral_value(Decimal('2.1')) Decimal('2') >>> ExtendedContext.to_integral_value(Decimal('100')) Decimal('100') >>> ExtendedContext.to_integral_value(Decimal('100.0')) Decimal('100') >>> ExtendedContext.to_integral_value(Decimal('101.5')) Decimal('102') >>> ExtendedContext.to_integral_value(Decimal('-101.5')) Decimal('-102') >>> ExtendedContext.to_integral_value(Decimal('10E+5')) Decimal('1.0E+6') >>> ExtendedContext.to_integral_value(Decimal('7.89E+77')) Decimal('7.89E+77') >>> ExtendedContext.to_integral_value(Decimal('-Inf')) Decimal('-Infinity') Trrq)rrrs r/rzContext.to_integral_values$4 1d +""4"00r1) NNNNNNNNNr,)r)Xr:r;r<r=rrrrrrhrrtrrrsrlrrKrrrr4rkrrrrrrrrrrrrrrrrrr5rrrr rrrrrrrrr$r*r,r;r@rBr?r#rGrrIr rrNrPrSrrVrrrrXr7rPr_rrcr6rr$rr'rrrr-r1r/rrs$BFDH&*"H 5 1I2<; "! ! H!,--H..&"$'**! "*H!1F":&   0#J.*#0).  ,  ). " ,,"",%8$4.6.&-6.6&6*"&6*"'"@*()(!.F)00,d'"N`8+t$L 1D):!0)&(<$@.-8'1<1<$Kr1rceZdZdZddZdZy)rrRrrNc |d|_d|_d|_yt|tr=|j |_t|j |_|j|_y|d|_|d|_|d|_y)Nr(r2r)rRrrrrrErFr)r6rs r/rz_WorkRep.__init__sn =DIDHDH w ' DI5::DHzzDHaDIQxDHQxDHr1cVd|jd|jd|jdS)N(rrr+rs r/rz_WorkRep.__repr__s!%DHHdhh??r1r,)r:r;r<rrrr-r1r/rrs$I  @r1rc|j|jkr|}|}n|}|}tt|j}tt|j}|jt d||z dz z}||jzdz |krd|_||_|xjd|j|jz zzc_|j|_||fS)zcNormalizes op1, op2 to have the same exp and length of coefficient. Done during addition. rrr2r)rrrrr)r'r(ratmprtmp_len other_lenrs r/r%r%s  ww#cgg,GC N#I ''CGdNQ./ /C599q 3&  GGrcgg )**GiiCG 8Or1c|dk(ry|dk\r|d|zzStt|}t|t|jdz }|| krdS|d| zzS)a Given integers n and e, return n * 10**e if it's an integer, else None. The computation is designed to avoid computing large powers of 10 unnecessarily. >>> _decimal_lshift_exact(3, 4) 30000 >>> _decimal_lshift_exact(300, -999999999) # returns None r(rrN)rrrrstrip)rCrstr_nval_ns r/rrsk Av a2q5yCF E Sc!233rzt2qBF{2r1cf|dks|dkr tdd}||k7r||| |zz dz }}||k7r|S)zClosest integer to the square root of the positive integer n. a is an initial approximation to the square root. Any positive integer will do for a, but the closer a is to the square root of n the faster convergence will be. r(z3Both arguments to _sqrt_nearest should be positive.r2)r)rCrr:s r/ _sqrt_nearestr8sN AvaNOOA q&!QBE'1*1 q& Hr1cDd|z||z }}|d||dz zz|dzz|kDzS)zGiven an integer x and a nonnegative integer shift, return closest integer to x / 2**shift; use round-to-even in case of a tie. r2rr-)rr6r:r<s r/_rshift_nearestr:s: :qEzqA 1!9 1%) **r1cBt||\}}|d|z|dzz|kDzS)zaClosest integer to a/b, a and b positive integers; rounds to even in the case of a tie. rr2)r5)rr:r<r=s r/ _div_nearestr< s. !Q 0, p >= 0, compute an integer approximation to 10**p * log10(c*10**e), with an absolute error of at most 1. Assumes that c*10**e is not exactly 1.rr2r(rr)rrr<rE _log10_digits) rrrrrr?rlog_dlog_10 log_tenpowers r/r)r)DsFA CF A !qsaxA1u E aCE 6 QJAQQB'Aa q!U1Wf-s #ArA2v.  U*C 00r1c|dz }tt|}||z||zdk\z }|dkDr6||z|z }|dk\r |d|zz}nt|d| z}t|d|z}nd}|rJttt |dz }||zdk\rt|t ||zzd|z}nd}nd}t||zdS)zGiven integers c, e and p with c > 0, compute an integer approximation to 10**p * log(c*10**e), with an absolute error of at most 1. Assumes that c*10**e is not exactly 1.rr2r(rr)rrr<rErrG) rrrrrrrHr f_log_tens r/r"r"fsFA CF A !qsaxA 1u aCE 6 QJAQQB'AaQ CAK " u9>%Q}QuW'=%=r5yIII   E)3 //r1ceZdZdZdZdZy) _Log10MemoizezClass to compute, store, and allow retrieval of, digits of the constant log(10) = 2.302585.... This constant is needed by Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__.cd|_y)N/23025850929940456840179914546843642076011014886)rrs r/rz_Log10Memoize.__init__s G r1c@|dkr td|t|jk\r\d} d||zdzz}tt t d|z|d}|| dd|zk7rn|dz }@|j ddd |_t|jd|d zS) ztGiven an integer p >= 0, return floor(10**p)*log(10). For example, self.getdigits(3) returns 2302. r(zp should be nonnegativerrrrNrrr2)rrrrr<rEr4r)r6rrr?rs r/ getdigitsz_Log10Memoize.getdigitss q567 7 DKK E5O\%1a.#>?5&'?c%i/  !--,Sb1DK4;;t!$%%r1N)r:r;r<r=rrRr-r1r/rNrNsCH&r1rNcPt||z|z}tdtt|zd|zz }t ||}||z}t |dz ddD]}t |||zz||z}t |dz ddD]}||dzz}t |||zz|}||zS)zGiven integers x and M, M > 0, such that x/M is small in absolute value, compute an integer approximation to M*exp(x/M). For 0 <= x/M <= 2.4, the absolute error in the result is bounded by 60 (and is usually much smaller).r>rr2r(rr)rrrrr<r) rr?r@rArBrMshiftrrs r/_iexprUs* 1qyA SSV_qs # $$AQA TF 1Q32  FQJ! 41Q3B QqS AfHv .  Q3Jr1c &|dz }td|tt|zdz }||z}||z}|dk\r |d|zz}n |d| zz}t|t |\}}t |d|z}t t |d|zd||z dzfS)aCompute an approximation to exp(c*10**e), with p decimal places of precision. Returns integers d, f such that: 10**(p-1) <= d <= 10**p, and (d-1)*10**f < exp(c*10**e) < (d+1)*10**f In other words, d*10**f is an approximation to exp(c*10**e) with p digits of precision, and with an error in d of at most 1. This is almost, but not quite, the same as the error being < 1ulp: when d = 10**(p-1) the error could be up to 10 ulp.rr(r2rir)r#rrr5rGr<rU) rrrrr<r6cshiftquotrs r/rrsFA 1s3q6{?Q& 'E E A aCE z2u9BJv}Q/0ID# sBI &C c2q5)4 0$(Q, >>r1cttt||z}t||||zdz}||z }|dk\r ||zd|zz}nt ||zd| z}|dk(rCtt||zdk\|dkDk(rd|dz zdzd|z } } | | fSd|zdz | } } | | fSt ||dz |dz\} } t | d} | dz } | | fS)a5Given integers xc, xe, yc and ye representing Decimals x = xc*10**xe and y = yc*10**ye, compute x**y. Returns a pair of integers (c, e) such that: 10**(p-1) <= c <= 10**p, and (c-1)*10**e < x**y < (c+1)*10**e in other words, c*10**e is an approximation to x**y with p digits of precision, and with an error in c of at most 1. (This is almost, but not quite, the same as the error being < 1ulp: when c == 10**(p-1) we can only guarantee error < 10ulp.) We assume that: x is positive and not equal to 1, and y is nonzero. r2r(r)rrrr"r<r) rrrrrr:lxcr6pcrrs r/rrs CBLBA B!A C qDE z VBI  #b&"uf* - QwR\B ! #a 0ac1ac3E #: Qq1"3E #: 21vqs+ sUB' q #:r1rF5(rrr) r/2345678r_cf|dkr tdt|}dt|z||dz S)z@Compute a lower bound for 100*log10(c) for a positive integer c.r(z0The argument to _log10_lb should be nonnegative.r)rrr)r correctionstr_cs r/rr*s< AvKLL FE s5z>JuQx0 00r1ct|tr|St|tr t|S|r%t|trtj |S|rt d|zt S)zConvert other to Decimal. Verifies that it's ok to use in an implicit construction. If allow_float is true, allow conversion from float; this is used in the comparison methods (__eq__ and friends). r)rrrrrrzr)rr allow_floats r/rr5s^%! %u~z%/!!%((9EABB r1cxt|tr||fSt|tjrm|jsJt |j tt|j|jz|j}|t|jfS|r5t|tjr|jdk(r |j}t|t rMt#}|rd|j$t&<n|j)t&d|tj+|fSt,t,fS)zGiven a Decimal instance self and a Python object other, return a pair (s, o) of Decimal instances such that "s op o" is equivalent to "self op other" for any of the 6 comparison operators "op". r(r2r)rr_numbersRationalrrDrErrrF denominatorr numeratorComplexr`r]rrrjrrrr)r6rrr7s r/rrHs%!U{ %**+#DJJ$'DII9J9J(J$K$(II/DWU__--- z%)9)9:uzzQ %, ,-GMM. )  M OW''... > ))r1ri?Bi)rar`rkrjrbrgrhrir)rar`rkrja # A numeric string consists of: # \s* (?P[-+])? # an optional sign, followed by either... ( (?=\d|\.\d) # ...a number (with at least one digit) (?P\d*) # having a (possibly empty) integer part (\.(?P\d*))? # followed by an optional fractional part (E(?P[-+]?\d+))? # followed by an optional exponent, or... | Inf(inity)? # ...an infinity, or... | (?Ps)? # ...an (optionally signaling) NaN # NaN (?P\d*) # with (possibly empty) diagnostic info. ) # \s* \Z z0*$z50*$z\A (?: (?P.)? (?P[<>=^]) )? (?P[-+ ])? (?Pz)? (?P\#)? (?P0)? (?P(?!0)\d+)? (?P,)? (?:\.(?P0|(?!0)\d+))? (?P[eEfFgGn%])? \Z cntj|}|td|z|j}|d}|d}|ddu|d<|dr |td|z|td|z|xsd|d<|xsd |d<|d d |d <t |d xsd |d <|dt |d|d<|ddk(r|d|ddvrd|d<|ddk(rHd|d<|t j }|dtd|z|d|d<|d|d<|d|d<|S|dd|d<ddg|d<d|d<|S)aParse and validate a format specifier. Turns a standard numeric format specifier into a dict, with the following entries: fill: fill character to pad field to minimum width align: alignment type, either '<', '>', '=' or '^' sign: either '+', '-' or ' ' minimumwidth: nonnegative integer giving minimum width zeropad: boolean, indicating whether to pad with zeros thousands_sep: string to use as thousands separator, or '' grouping: grouping for thousands separators, in format used by localeconv decimal_point: string to use for decimal point precision: nonnegative integer giving precision, or None type: one of the characters 'eEfFgG%', or None NzInvalid format specifier: fillalignzeropadz7Fill character conflicts with '0' in format specifier: z2Alignment conflicts with '0' in format specifier:  >rRr minimumwidthrrvr(rkgGnr2rCrt thousands_sepzJExplicit thousands separator conflicts with 'n' type in format specifier: grouping decimal_pointrrr )_parse_format_specifier_regexmatchr groupdictr_locale localeconv) format_specrrr format_dictrtrus r/rzrzs & &++K8Ay5 CDD++-K v D  E))4D@K 9  68CDE E  24?@A A+#K!@KLM M'2?'C O$"-j"9 J'2?'C O$   ' /+-K (#$a& J'* O$ r1c|d}|d}||t|z t|z z}|d}|dk(r ||z|z}|S|dk(r ||z|z}|S|dk(r ||z|z}|S|dk(r!t|dz}|d ||z|z||d z}|Std ) zGiven an unpadded, non-aligned numeric string 'body' and sign string 'sign', add padding and alignment conforming to the given format specifier dictionary 'spec' (as produced by parse_format_specifier). ryrtru M788r1cddlm}m}|sgS|ddk(r#t|dk\r||dd||dS|dtj k(r|ddSt d)zyConvert a localeconv-style grouping into a (possibly infinite) iterable of integers representing group lengths. r()chainrepeatrrNrz unrecognised format for grouping) itertoolsrrrrCHAR_MAXr)r|rrs r/_group_lengthsrAsl(  " s8}1Xcr]F8B<$899 ")) )};<? ? CKA. 2 c1s6{?+faRSk9:!Q )q. SX & F Y * c1s6{?+faRSk9: 88HV$ %%r1c"|ry|ddvr|dSy)zDetermine sign character.rrRz +rr-) is_negativers r/r{r{}s# f F|r1c2t||}|s|dr|d|z}|dk7s|ddvr"ddddd|d}|d j||z }|dd k(r|d z }|d r|d t|z t|z }nd}t|||}t |||z|S) acFormat a number, given the following data: is_negative: true if the number is negative, else false intpart: string of digits that must appear before the decimal point fracpart: string of digits that must come after the point exp: exponent, as an integer spec: dictionary resulting from parsing the format specifier This function uses the information in spec to: insert separators (decimal separator and thousands separators) format the sign format the exponent add trailing '%' for the '%' type zero-pad if necessary fill and align if necessary altr}r(rkrwrr)rrrurtz{0}{1:+}rsrvry)r{formatrrr|)rrrrrrRecharrs r/r}r}s$  T *D4;(83 ax4<4'#C8fFJ%%eS11 F|sC I(3x=83t9D  #GT9=G wx/ 66r1Infz-Infr rrr,)F)r()r)FF)r2)|r=__all__r: __xname__ __version____libmpdec_version__mathrnumbersrnsys collectionsr) _namedtupler ImportErrorrrrrrrrrr%r&maxsizer!r"r#r$ArithmeticErrorrr r rZeroDivisionErrorr rrr rr rrrrzrrrr contextvars ContextVarrm frozensetryrrr rrrDNumberregisterrwrrr%rrrrr8r:r<rEr)r"rNrRrGrUrrrrrrrrrecompileVERBOSE IGNORECASErrrtr~DOTALLr~localerrzr|rrr{r}r!r rHrrrrP hash_infomodulusrrUrrV _PyHASH_NANrrr-r1r/rs '! F    &5~/EiXL   #  #   ;;'!H!H"HHHH  #   .  ':'%%'8% )  (*;    %     #:w#:L )  %y  ^Wh 'N D##3$%5#$4 !13 }o}/:G-{--.?@O &+hB4KfB4KHh& ! 'f 'O$fO$b6@v@4< 3*  +!. ` 1D*0X!&F!&F)) #J"?H(Vr" br+1&"*T /x)9:   x)97IN  * "**"ZZ"--# !""'#&RZZ  $ $ bjj && !+ ,ZZ !   N`6=.#&J#7R EN FOu~ qzr{ /0--''mm mm B!+_= CD&%L&b|  s#M& M5&M21M25M=<M=