mņ %U²Ic@sQdZddddddddd d d d d ddddddddddgZdkZdZdZdZdZdZdZ dZ dZ dZ de fd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZd eefd „ƒYZd!efd"„ƒYZd#eefd$„ƒYZd efd%„ƒYZd&efd'„ƒYZd efd(„ƒYZd efd)„ƒYZd eefd*„ƒYZdeeefd+„ƒYZeeeeeeeegZhee<ee<ee<eeej o2dk Z d,fd-„ƒYZ!e!ƒZ[ [!nXy ei"WnKe#j o?e$ei%ƒd.ƒoei%ƒ`&nd/„Z'd0„Z(nFXei"ƒZ"e$e"d.ƒo e"`&ne"d1„Z(e"d2„Z'[["de)fd3„ƒYZ*gZ+e*i,i-ƒD]!Z.e.i/d4ƒo e+e.nq[+Z0x8e0D]0Z.e.d5i1ƒZ2e3ƒe2Z4e.e*i5e4d5<d?d5<d@d5<dAdB<dCdBe6dFdQdHedIeeeeegdJgƒZ?e6dFdQdHedIgdJgƒZ@e*dRƒZAe*dSƒZBeAeBfZCe*dTƒZDdkEZEeEiFdUeEiGƒiHZI[EdV„ZJeKdWjo*dkLZLdk Z eLiMe iNeKƒndS(XsČ This is a Py2.3 implementation of decimal floating point arithmetic based on the General Decimal Arithmetic Specification: www2.hursley.ibm.com/decimal/decarith.html and IEEE standard 854-1987: www.cs.berkeley.edu/~ejr/projects/754/private/drafts/854-1987/dir.html Decimal floating point has finite precision with arbitrarily large bounds. The purpose of the module is to support arithmetic using familiar "schoolhouse" rules and to avoid the some of tricky representation issues associated with binary floating point. The package is especially useful for financial applications or for contexts where users have expectations that are at odds with binary floating point (for instance, in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead of the expected Decimal("0.00") returned by decimal floating point). Here are some examples of using the decimal module: >>> from decimal import * >>> setcontext(ExtendedContext) >>> Decimal(0) Decimal("0") >>> Decimal("1") Decimal("1") >>> Decimal("-.0123") Decimal("-0.0123") >>> Decimal(123456) Decimal("123456") >>> Decimal("123.45e12345678901234567890") Decimal("1.2345E+12345678901234567892") >>> Decimal("1.33") + Decimal("1.27") Decimal("2.60") >>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41") Decimal("-2.20") >>> dig = Decimal(1) >>> print dig / Decimal(3) 0.333333333 >>> getcontext().prec = 18 >>> print dig / Decimal(3) 0.333333333333333333 >>> print dig.sqrt() 1 >>> print Decimal(3).sqrt() 1.73205080756887729 >>> print Decimal(3) ** 123 4.85192780976896427E+58 >>> inf = Decimal(1) / Decimal(0) >>> print inf Infinity >>> neginf = Decimal(-1) / Decimal(0) >>> print neginf -Infinity >>> print neginf + inf NaN >>> print neginf * inf -Infinity >>> print dig / 0 Infinity >>> getcontext().traps[DivisionByZero] = 1 >>> print dig / 0 Traceback (most recent call last): ... ... ... DivisionByZero: x / 0 >>> c = Context() >>> c.traps[InvalidOperation] = 0 >>> print c.flags[InvalidOperation] 0 >>> c.divide(Decimal(0), Decimal(0)) Decimal("NaN") >>> c.traps[InvalidOperation] = 1 >>> print c.flags[InvalidOperation] 1 >>> c.flags[InvalidOperation] = 0 >>> print c.flags[InvalidOperation] 0 >>> print c.divide(Decimal(0), Decimal(0)) Traceback (most recent call last): ... ... ... InvalidOperation: 0 / 0 >>> print c.flags[InvalidOperation] 1 >>> c.flags[InvalidOperation] = 0 >>> c.traps[InvalidOperation] = 0 >>> print c.divide(Decimal(0), Decimal(0)) NaN >>> print c.flags[InvalidOperation] 1 >>> tDecimaltContexttDefaultContextt BasicContexttExtendedContexttDecimalExceptiontClampedtInvalidOperationtDivisionByZerotInexacttRoundedt SubnormaltOverflowt Underflowt ROUND_DOWNt ROUND_HALF_UPtROUND_HALF_EVENt ROUND_CEILINGt ROUND_FLOORtROUND_UPtROUND_HALF_DOWNt setcontextt getcontextNt NEVER_ROUNDt ALWAYS_ROUNDcBstZdZd„ZRS(s-Base 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 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. cGsdS(N((tselftcontexttargs((t$/data/zmath/lib/python2.4/decimal.pythandle­s(t__name__t __module__t__doc__R(((RRšs cBstZdZRS(s(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"). (RRR (((RR±s cBstZdZd„ZRS(sDAn 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 cGsE|o:|ddjo%t|di|didfƒSqAntS(Niitn(RRt_signt_inttNaN(RRR((RRŠs)(RRR R(((RR¾s tConversionSyntaxcBstZdZd„ZRS(sÜTrying to convert badly formed string. This occurs and signals invalid-operation if an string is being converted to a number and it does not conform to the numeric string syntax. The result is [0,qNaN]. cGs dddfS(NiR!(i((RRR((RRŽs(RRR R(((RR%Ös cBstZdZed„ZRS(s²Division 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. cGs(|dj ot|fdSnt|S(Ni(tdoubletNonetInfsigntsign(RRR)R&R((RRīs (RRR R'R(((RRįs tDivisionImpossiblecBstZdZd„ZRS(sņCannot 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]. cGs ttfS(N(R$(RRR((RRūs(RRR R(((RR*ós tDivisionUndefinedcBstZdZed„ZRS(sķUndefined 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]. cGs|dj ottfSntS(N(ttupR'R$(RRR,R((RRs (RRR R'R(((RR+žs cBstZdZRS(s¬Had 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. (RRR (((RR s tInvalidContextcBstZdZd„ZRS(séInvalid 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]. cGstS(N(R$(RRR((RR#s(RRR R(((RR-s cBstZdZRS(sŲNumber 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. (RRR (((RR &s cBstZdZRS(s—Exponent < 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. (RRR (((RR 3s cBstZdZd„ZRS(sNumerical 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. cGsÖ|ittttfjo t|Sn|djoH|itjo t|Snt |d|i |i |i dfƒSn|djoH|it jo t|Snt |d|i |i |i dfƒSndS(Nii i(i (i ( RtroundingRRRRR(R)RRtprectEmaxR(RRR)R((RRUs   ,  (RRR R(((RR ?s cBstZdZRS(svNumerical 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. (RRR (((RR es t MockThreadingcBstZed„ZRS(NcCs |itS(N(tsystmodulesR(RR2((RtlocalŒs(RRR2R4(((RR1‹st__decimal_context__cCsC|tttfjo|iƒ}|iƒn|tiƒ_dS(s%Set this thread's context to context.N( RRRRtcopyt clear_flagst threadingt currentThreadR5(R((RR›s  cCsGytiƒiSWn/tj o#tƒ}|tiƒ_|SnXdS(sĶReturns 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. N(R8R9R5tAttributeErrorRR(R((RR¢s cCs;y |iSWn)tj otƒ}||_|SnXdS(sĶReturns 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. N(t_localR5R:RR(R;R((RR¶s   cCs=|tttfjo|iƒ}|iƒn||_dS(s%Set this thread's context to context.N(RRRRR6R7R;R5(RR;((RRÄs  cBsźtZdZdEZded„Zd„Zd„Zeed „Zd „Z ed „Z d „Z d „Z ed„Z d„Zd„Zd„Zded„Zed„Zed„Zed„Zded„Zed„ZeZed„Zed„Zded„Zed„ZeZed„ZeZded„Zed „Z e Z!ed!„Z"ed"„Z#ed#„Z$ed$„Z%ed%„Z&ed&„Z'ed'„Z(d(„Z)d)„Z*d*„Z+d+„Z,d,„Z-eeed-„Z.hZ/d.„Z0ed/„Z1d0„Z2d1„Z3d2„Z4d3„Z5d4„Z6eed5„Z7ed6„Z8ed7„Z9eedd8„Z:d9„Z;eedd:„Z<eed;„Z=ed<„Z>ed=„Z?ed>„Z@d?„ZAd@„ZBdA„ZCdB„ZDdC„ZEdD„ZFRS(Fs,Floating point class for decimal arithmetic.t_expR#R"t _is_specialt0cCsti|ƒ}t|_t|tƒoG|i |_ t t t t|i ƒƒƒ|_t |iƒ|_|Snt|tƒo8|i|_|i |_ |i|_|i|_|Snt|t tfƒoX|djo d|_ n d|_ d|_t t t tt|ƒƒƒƒ|_|Snt|tt fƒoźt|ƒdjo td‚n|ddjo td‚nxC|dD]7}t|t tfƒ p |djo td‚q„q„W|d|_ t |dƒ|_|ddjo|d|_t|_nt |dƒ|_|Snt|tƒotd d ƒ‚n|djo tƒ}nt|tƒoyt |ƒoLd|_d|_t|_t |ƒdjo d|_ n d|_ |Snt!|ƒo©t!|ƒ\}}}t|_t|ƒ|i$jo)|i%t&ƒ\|_ |_|_|Sn|djo d |_n d |_||_ t t t |ƒƒ|_|Sny"t'|ƒ\|_ |_|_Wn=tj o1t|_|i%t&ƒ\|_ |_|_nX|Sntd |ƒ‚dS(sCreate a decimal point instance. >>> Decimal('3.14') # string input Decimal("3.14") >>> Decimal((0, (3, 1, 4), -2)) # tuple input (sign, digit_tuple, exponent) Decimal("3.14") >>> Decimal(314) # int or long Decimal("314") >>> Decimal(Decimal(314)) # another decimal instance Decimal("314") iiisInvalid argumentss Invalid signsPThe second value in the tuple must be composed of non negative integer elements.itFR!tNs"Cannot convert float to Decimal. s#First convert the float to a stringsCannot convert %r to DecimalN(ii(R?R!R@(i((tobjectt__new__tclsRtFalseR=t isinstancetvaluet_WorkRepR)R"ttupletmaptinttstrR#texpR<Rtlongtabstlisttlent ValueErrortdigittTruetfloatt TypeErrorRR'Rt basestringt _isinfinityt_isnantsigtdiagR/t _raise_errorR%t _string2exact(RCRFRR)RZRRYRR((RRBŁsŠ   !        $   $             !    " &cCsE|io7|i}|djodSqA|djodSqAndS(srReturns whether the number is not actually one. 0 if a number 1 if NaN 2 if sNaN R!iR@iiN(RR=R<RL(RRL((RRX@s     cCs.|idjo|iodSndSndS(syReturns whether the number is infinite 0 if finite or not a number 1 if +INF -1 if -INF R?i’’’’iiN(RR<R"(R((RRWOs  cCsĄ|iƒ}|djo t}n |iƒ}|p|o|djo tƒ}n|djo|i t dd|ƒSn|djo|i t dd|ƒSn|o|Sn|SndS(s½Returns 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. itsNaNiiN( RRXt self_is_nantotherR'RDt other_is_nanRRR[R(RR_RR`R^((Rt _check_nans\s$          cCs%|iodSnt|iƒdjS(sOIs the number non-zero? 0 if self == 0 1 if self != 0 iiN(RR=tsumR#(R((Rt __nonzero__|s c Cst|ƒ}|tjo|Sn|ip |io>|i||ƒ}|odSnt|i ƒ|i ƒƒSn| o| odSn|i |i jodSn|i |i jodSn|i ƒ}|i ƒ}||jo?|id|i|i|id|i|ijodSnb||jo#|iddjod|i Sn2||jo$|iddjod|i  Sn|djo tƒ}n|iƒ}|itƒ}|iƒ}|i|d|ƒ}|i|Œ||_|pdSn|i odSndS(Niii’’’’R(i(i(t_convert_otherR_tNotImplementedRR=RaRtanstcmpRWR"tadjustedt self_adjustedtother_adjustedR#R<R'Rt _shallow_copyt _set_roundingRR.t_ignore_all_flagstflagst__sub__trest _regard_flags( RR_RRiR.RpRjRnRf((Rt__cmp__†sF    D!!       cCs4t|tttfƒptSn|i|ƒdjS(Ni(RER_RRJRMReRRr(RR_((Rt__eq__»scCs4t|tttfƒptSn|i|ƒdjS(Ni(RER_RRJRMReRRr(RR_((Rt__ne__ĄscCswt|ƒ}|tjo|Sn|ip|o/|io%|i||ƒ}|o|Sqant|i ||ƒƒS(s­Compares one to another. -1 => a < b 0 => a = b 1 => a > b NaN => one is NaN Like __cmp__, but returns Decimal instances. N( RdR_ReRR=RaRRfRRr(RR_RRf((RtcompareÅs   cCs’|io1|iƒotdƒ‚ntt|ƒƒSnt|ƒ}|t|ƒjot|ƒSn|i ƒpt ‚tt|i ƒƒƒS(sx.__hash__() <==> hash(x)sCannot hash a NaN value.N( RR=RXRUthashRKRJtiRRctAssertionErrort normalize(RRw((Rt__hash__Śs   cCs|i|i|ifS(seRepresents the number as a triple tuple. To show the internals exactly as they are. N(RR"R#R<(R((Rtas_tupleéscCsdt|ƒS(s0Represents the number as an instance of Decimal.s Decimal("%s")N(RKR(R((Rt__repr__šsic CsI|io¬|iƒoud|i} |idjo d}nditt |iƒƒ}|iƒdjo| d|Sn| d|Sn|i ƒod|i} | dSq¶n|djo t ƒ}ntt |iƒ} t|iƒ} |i| }|o| o |idjo:|idjo*d|id d t|iƒ}|Sn|id d d d }||ijod d ||i}nd }|djoQ|io|d 7}n |d7}|djo|d7}n|t |ƒ7}nd|i|}|Sn|o,|d d d }|d |d d }n|d }d }|idjon,|idjo!|djo| i|dƒnū|idjo<|djo/d gt| ƒ| dd+| idd ƒnÆ| |jo| i|dƒn*| |jo| id g|| ƒn|o]|ip| idƒn,| id ƒ|djo| idƒn| it |ƒƒn|ox)| dd !d gjog| dd +q¢Wt| ƒdjp(| ddjp| diƒdjod g| dd+qn|io| iddƒndi| ƒS(s–Return string representation of the number in scientific notation. Captures all of the information in the underlying representation. t-itiR]R$tInfinityiś’’’s0.R>iitEtet+t.N(i(RR=RXR"tminusR#tinfotjoinRIRKRWRR'RttmpRPt numdigitsR<t leftdigitstengRNtsRLtcapitalstdotplacetadjexptinsertRJtextendtappendtlower( RRŠRR…RŽR‰R‹RLRR‡R„Rˆ((Rt__str__õs†          "          ; cCs|iddd|ƒS(sConvert to engineering-type string. Engineering notation has an exponent which is a multiple of 3, so there are up to 3 digits left of the decimal place. Same rules for when in exponential and when as a value as in __str__. RŠiRN(RR“R(RR((Rt to_eng_stringGscCsĆ|io%|id|ƒ}|o|Sq/n|p d}n|io d}nd}|djo tƒ}n|i t jo&t ||i |i fƒi|ƒSnt ||i |i fƒS(sRReturns a copy with the sign switched. Rounds, if it has reason. RiiN(RR=RaRRfR)R"R'Rt_rounding_decisionRRR#R<t_fix(RRRfR)((Rt__neg__Qs       &cCsŸ|io%|id|ƒ}|o|Sq/n|i}|p d}n|djo tƒ}n|i t jo|i |ƒ}n t |ƒ}||_|S(shReturns a copy, unless it is a sNaN. Rounds the number (if more then precision digits) RiN( RR=RaRRfR"R)R'RR•RR–R(RRRfR)((Rt__pos__is        icCs£|io%|id|ƒ}|o|Sq/n|p7|djo tƒ}n|iƒ}|i t ƒn|i o|i d|ƒ}n|i d|ƒ}|S(s`Returns the absolute value of self. If the second argument is 0, do not round. RN(RR=RaRRftroundR'RRkt_set_rounding_decisionRR"R—R˜(RR™RRf((Rt__abs__‚s      c Cs’t|ƒ}|tjo|Sn|djo tƒ}n|ip |io|i||ƒ}|o|Sn|i ƒoB|i |i jo!|i ƒo|i t dƒSnt|ƒSn|i ƒot|ƒSqŽn|itj} t|i|iƒ} d}|itjo|i |i jo d}n| oE| o=t|i |i ƒ}|o d}nt|d| fƒSn|pZt| |i|idƒ} |i| ddd|ƒ}| o|i|ƒ}n|Sn|pZt| |i|idƒ} |i| ddd|ƒ}| o|i|ƒ}n|Snt|ƒ}t|ƒ}t ||| |iƒ\}}tƒ}|i|ijoĮ|i"|i"joG| |i#ƒjo|i#ƒ} |i t$ƒnt|d| fƒSn|i"|i"jo||}}n|idjo&d|_|i|i|_|_qd|_n9|idjod|_d \|_|_n d|_|idjo|i"|i"|_"n|i"|i"|_"|i|_t|ƒ}| o|i|ƒ}n|S( sbReturns self + other. -INF + INF (or the reverse) cause InvalidOperation errors. s -INF + INFiitwatchexpRN(i(i(ii(%RdR_ReRR'RRR=RaRfRWR"R[RRR•Rt shouldroundtminR<RLt negativezeroR.RR)tmaxR/t_rescaleR–RGtop1top2t _normalizetresultRJtEtinyR( RR_RR¢R£RfR)R„RŸRRL((Rt__add__™s„       #            cCsŒt|ƒ}|tjo|Sn|ip |io(|i|d|ƒ}|o|Sq]nt|ƒ}d|i |_ |i |d|ƒS(sReturn self + (-other)RiN( RdR_ReRR=RaRRfRR‡R"R§(RR_RR‡Rf((RRoõs    cCsPt|ƒ}|tjo|Snt|ƒ}d|i|_|i|d|ƒS(sReturn other + (-self)iRN( RdR_ReRRR‡R"R§R(RR_RR‡((Rt__rsub__s   cCs.|io/|id|ƒ}|o|Snt|ƒSnt|iƒ}|dcd7 true division 1 => (a //b, a%b) 2 => a //b 3 => a%b Actually, if divmod is 2 or 3 a tuple is returned, but errors for computing the other value are not raised. iis(+-)INF // (+-)INFs(+-)INF % (+-)INFs(+-)INF/(+-)INFsINF % xiisDivision by infinitys0 / 0s0e-x / ys0e+x / ys divmod(x,0)sx / 0RRœi N(ii(i(i(i(i(i(i(0RdR_RetdivmodRR'RRR"R)R=RaRfRWR[RR(R$RRR¦R+t othersideRžR<RLR0RR•RRR›R”tans2R–RGR¢R£t_adjust_coefficientstadjustRpR/R*t prec_limitRJRmtfrozenRq(RR_R±RR¢R£R³R)RfR¶RpRLRR²R·Rµ((RRÆwsī             #!    , '      % *    cCs4t|ƒ}|tjo|Sn|i|d|ƒS(s%Swaps self/other and returns __div__.RN(RdR_ReR°RR(RR_R((Rt__rdiv__s   cCs|i|d|ƒS(s/ (self // other, self % other) iN(RRÆR_R(RR_R((Rt __divmod__%scCs4t|ƒ}|tjo|Sn|i|d|ƒS(s(Swaps self/other and returns __divmod__.RN(RdR_ReR¹RR(RR_R((Rt __rdivmod__+s   cCs”t|ƒ}|tjo|Sn|ip |io%|i||ƒ}|o|SqZn|o| o|it dƒSn|i |d|ƒdS(s self % other sx % 0iiN( RdR_ReRR=RaRRfR[RRÆ(RR_RRf((Rt__mod__2s   cCs4t|ƒ}|tjo|Sn|i|d|ƒS(s%Swaps self/other and returns __mod__.RN(RdR_ReR»RR(RR_R((Rt__rmod__Ds   c Csœt|ƒ}|tjo|Sn|ip |io%|i||ƒ}|o|SqZn|o| o|it dƒSn|djo t ƒ}n|i ƒ}|i ttƒ} |i|d|ƒ\} } | iƒo|i| Œ| Sn|i ƒ}|itƒ}|io|itdƒd|ƒ}n|itdƒd|ƒ}|i|ƒ|i| Œ| i|i}}d\| _|_| |jo7||| _|_|i|d|ƒ| i|ƒSn||| _|_|itƒ}|i|d|ƒ\} } |i|ƒ| iƒo| Sn| i ƒ }|itƒ}| i"d|ƒ} |i|ƒ| i|i}}d \| _|_| |jp|oÕ| |joČ||| _|_|i#d7_#t$| i%tdƒd|ƒi&ƒ|i#jo$|i#d8_#|it'ƒdSn|i#d8_#|i|ijo| i(|d|ƒ} q| i%|d|ƒ} n||| _|_| i|ƒS( sI Remainder nearest to 0- abs(remainder-near) <= other/2 sx % 0Riž’’’iiiN(ii(ii()RdR_ReRR=RaRRfR[RR'RRkt _ignore_flagsR R RnR¹tsidetrRXRqRšRR.R"R°Rt comparisonts1ts2R–t_iseventdecreaseR›R/RPR§R#R*Ro( RR_RRÄRfR.RĀRĮRĄRæRnR¾((Rtremainder_nearKsn                 !.cCs|i|d|ƒdS(s self // otheriiN(RRÆR_R(RR_R((Rt __floordiv__šscCs4t|ƒ}|tjo|Sn|i|d|ƒS(s*Swaps self/other and returns __floordiv__.RN(RdR_ReRĘRR(RR_R((Rt __rfloordiv__žs   cCstt|ƒƒS(sFloat representation.N(RTRKR(R((Rt __float__„scCsŻ|ioE|iƒotƒ}|itƒSqO|iƒo td‚qOn|i djo*di t t |i ƒƒd|i }n#di t t |i ƒƒ|i }|djo d}nd|i}t||ƒS(s1Converts self to an int, truncating if necessary.sCannot convert infinity to longiR~R>R}N(RR=RXRRR[R-RWt OverflowErrorR<R†RIRKR#R‹R"R)RJ(RR‹RR)((Rt__int__©s    *"   cCst|iƒƒS(sCConverts to a long. Equivalent to long(int(self)) N(RMRRŹ(R((Rt__long__ŗscCs†|io|Sn|djo tƒ}n|i}|i|ƒ}t|i ƒ|jo(|i |d|ƒ}|i|ƒ}n|S(sÜRound if it is necessary to keep self within prec precision. Rounds and fixes the exponent. Does not raise on a sNaN. Arguments: self - Decimal instance context - context used. RN( RR=RR'RR/t _fixexponentsRfRPR#t_round(RRR/Rf((RR–Įs    c Cs§|i}|i}|}|iƒ}||jo®|iƒ}|i |jov|p*t |ƒ}||_ |i t ƒ|Sn|i |d|ƒ}|i tƒ|ito|i tƒqŪq£|o|i tƒq£nÅ|iƒ}|o6|i |jo&|i t ƒ|i |d|ƒ}n||i}||joe|p*t |ƒ}||_ |i t ƒ|Sn|i tƒ|i tƒ|i td|iƒSn|S(sFix the exponents and return a copy with the exponent in bounds. Only call if known to not be a special value. Rs above EmaxN(Rt_clamptfolddowntEminRRfRht ans_adjustedR¦R<RR[RR”R RnR R tEtopR0R R R"( RRRĻRŃRŠR0R¦RŅRf((RRĢÕsD                  c Cs|io@|id|ƒ} | o| Sn|iƒot|ƒSqJn|djo tƒ}n|djo |i }n|djo |i }n|p‚|djo$d}t |i ƒ||i}n%d|}t |i ƒ|i|}t|i||fƒ} |itƒ| Sn|djo&t|ƒ}d|i |_ d}nZ|djo@|it |i ƒ|d}t|id |fƒ}d}n t|ƒ}t |i ƒ} || jo|Sn|| }|djoJt|i ƒ}|idg|ƒt|i||i|fƒ} | Sn|i |} | d t | ƒjo;t|i|i | |i|fƒ} |itƒ| Snt||i|ƒ} ||i jo|iƒ}||_ n| |||ƒ} |itƒ|itdƒ| S( s•Returns a rounded version of self. You can specify the precision or rounding method. Otherwise, the context determines it. RiisChanged in roundingN(i(i(i(ii(i(RR=RaRRfRWRR'RR.R/tdigRPR#R<RLR"R[R ttempRˆtexpdiffROR‡Rt lostdigitstgetattrt_pick_rounding_functiont this_functionRkR ( RR/R.RR‡RÕRLRŌRÓRÖRfRŁRˆ((RRĶsj                     &    cCs$t|i|i| |i|fƒS(s(Also known as round-towards-0, truncate.N(RRR"R#R/R<RÕ(RR/RÕR((Rt _round_downQscCs©|djo*t|i|i| |i|fƒ}n|i|djoZ|i ddd|ƒ}t |iƒ|jo(t|i|id |idfƒSq„n|S(sRounds 5 up (away from 0)iR™iRi’’’’iN( R‡R'RRR"R#R/R<RÕR«RRP(RR/RÕRR‡((Rt_round_half_upUs *,cCs½t|i|i| |i|fƒ}|i|dj}|o8x5|i|dD]}|djo d}PqRqRWn|o(|i|dd@djo|Sq§n|i ||||ƒS(s!Round 5 to even, rest to nearest.iiiN( RRR"R#R/R<RÕR‡thalfRRRŪR(RR/RÕRR‡RRRÜ((Rt_round_half_even`s&   cCst|i|i| |i|fƒ}|i|dj}|o8x5|i|dD]}|djo d}PqRqRWn|o|Sn|i ||||ƒS(s Round 5 downiiiN( RRR"R#R/R<RÕR‡RÜRRRŪR(RR/RÕRR‡RRRÜ((Rt_round_half_downos&  cCs­t|i|i| |i|fƒ}x€|i|D]q}|djo^|i ddd|ƒ}t |iƒ|jo(t|i|id |idfƒSq„|Sq4q4W|S(sRounds away from 0.iR™iRi’’’’N( RRR"R#R/R<RÕR‡RRR«RRP(RR/RÕRR‡RR((Rt _round_up}s& ( cCs8|io|i|||ƒSn|i|||ƒSdS(s(Rounds up (not away from 0 if negative.)N(RR"RŚR/RÕRRß(RR/RÕR((Rt_round_ceiling‰s cCs8|ip|i|||ƒSn|i|||ƒSdS(s'Rounds down (not towards 0 if negative)N(RR"RŚR/RÕRRß(RR/RÕR((Rt _round_floors c Csšt|ƒ}|tjo|Sn|d jo tƒ}n|ip|ip|iƒdjoY|i ƒp|iƒdjo|i t dƒSn|i ||ƒ}|o|Sq»n|iƒp|i t dƒSn| o| o|i t dƒSn|ptdƒSn|tdƒjotdƒSn|io |iƒ }t|ƒ}|i ƒoK|o|i t dƒSn|djo t|Snt|d dfƒSn| o†|djoy|it|iƒd||ijoQ|oJtdƒ}||_|i tƒ|i tƒ|i td |ƒ|Snttt|ƒƒƒ}|i!} | o/| |dt#ijo|i td |ƒSnt|ƒ} tdƒ}|i&ƒ}| |d|_!|djo&| }tdƒi'| d |ƒ} nd} x| |jo| dK} qW| dL} x‘| o‰|i)|d |ƒ}|i ƒot|}Pn| |@o|i)| d |ƒ}n|d j o|i*|d |ƒ}n| dL} q1W| |_!|i+t,jo|i-|ƒSn|S(sfReturn self ** n (mod modulo) If modulo is None (default), don't take it mod modulo. isx ** INFsx ** (non-integer)s0 ** 0isINF % xitinfs Big powersToo much precision.RN(i(.RdR!ReRR'RRR=RhRWR[RRaRft _isintegerRR"RĆR)RJtmoduloR(R<RPR#R0R‡R R R RKRNtelengthR/t firstprecRtmultvalRkR°RŖR®R»R•RR–( RR!RäRR)RåRfR‡RčRēRŖRę((Rt__pow__—s„    '       D                 cCs4t|ƒ}|tjo|Sn|i|d|ƒS(s%Swaps self/other and returns __pow__.RN(RdR_ReRéRR(RR_R((Rt__rpow__ņs   cCsŻ|io%|id|ƒ}|o|Sq/n|i|ƒ}|iƒo|Sn|pt|i ddfƒSnt |i ƒ}|i }x1|i |ddjo|d7}|d8}qWt|i |i | |fƒS(s?Normalize- strip trailing 0s, change anything equal to 0 to 0e0RiiN(i(RR=RaRRfR–tdupRWRR"RPR#tendR<RL(RRRģRLRfRė((RRyłs"     cCs¼|ip |io|i||ƒ}|o|Sn|iƒp |iƒoP|iƒo|iƒo|Sn|djo tƒ}n|i t dƒSq£n|i |i |||ƒS(s‡Quantize self so its exponent is the same as that of exp. Similar to self._rescale(exp._exp) but with error checking. squantize with one INFN(RR=RLRaRRfRWR'RR[RR”R<R.Rœ(RRLR.RRœRf((Rtquantizes   cCs |ip |io||iƒp |iƒo"|iƒo|iƒotSn|iƒp |iƒo"|iƒo|iƒotSqn|i|ijS(syTest whether self and other have the same exponent. same as self._exp == other._exp, except NaN == sNaN N(RR=R_RXRSRWR<(RR_((Rt same_quantum"s"&c Cs9|djo tƒ}n|ioF|iƒo|itdƒSn|id|ƒ}|o|Sqjn|o7|i |jp|i ƒ|jo|itdƒSn|p&t|ƒ}d|_||_|Sn|i|} t|iƒ| }|o$||ijo|itdƒSnt|ƒ}d |i|_|d7}|djo$| |i|_d |_d}n|i||d|ƒ}|iddjo*t|iƒdjo|id|_n||_|iƒ}|o!||ijo|itƒn,|o$||i jo|itdƒSn|S( sīRescales so that the exponent is exp. exp = exp to scale to (an integer) rounding = rounding version watchexp: if set (default) an error is returned if exp is greater than Emax or less than Etiny. srescale with an INFRsrescale(a, INF)isRescale > preciN(i(i(ii(RR'RRR=RWR[RRaRfRœR0RLR¦RR#R<tdiffRPtdigitsR/R‡RĶR.Rht tmp_adjustedRŠR ( RRLR.RRœRšR‡RńRfRļ((RR”.sJ     *         *  cCsœ|io%|id|ƒ}|o|Sq/n|idjo|Sn|djo tƒ}n|it t ƒ}|i d|d|ƒ}|i|ƒ|S(s@Rounds to the nearest integer, without raising inexact, rounded.RiN(RR=RaRRfR<R'RR½R R RnR”R.Rq(RR.RRnRf((Rt to_integrales     cCs||ioP|id|ƒ}|o|Sn|iƒo|idjot|ƒSqZn|pK|id}|idjotdd|fƒSq¬tdd|fƒSn|d jo t ƒ}n|idjo|i t dƒSnt|ƒ}|id}|id@o|id7_d|_n d|_|iƒ}|iƒ}|i}d|_|iƒd@djostdd|iƒdfƒ}|i|itddd fƒd|ƒd|ƒ}|id|iƒd8_nztdd|it|iƒdfƒ}|i|itddd fƒd|ƒd|ƒ}|id|iƒd8_|i|i} } titi|_|_td ƒ} |d}|it ƒ}xjt"d|id|ƒ|_| i|i|i#|d|ƒd|ƒd|ƒ}|i|joPq·q·W||_|iƒ} |i%d|ƒ}|d|_| |iƒjo"|id7_|id8_n|i&tdd|idfƒd|ƒ} |it(ƒ| i| d|ƒ|jo+|i&tdd|ifƒd|ƒ}n€|itdd|idfƒd|ƒ}|it*ƒ|i|d|ƒ|jo+|itdd|ifƒd|ƒ}n|i|7_||_||_!|i+|ƒ}|i,t-ƒ}|i|d|ƒ|jp+|i.|ƒ|i t/ƒ|i t0ƒnO|id}|i|i|7_|i1|d|ƒ}||_|i.|ƒ| | |_|_|i+|ƒS(s£Return the square root of self. Uses a converging algorithm (Xn+1 = 0.5*(Xn + self / Xn)) Should quadratically approach the right answer. Riiissqrt(-x), x > 0iii iiž’’’iż’’’s0.5N(i(i(i(iii (iii (iii (iii (i(i(i(i(i(2RR=RaRRfRWR"RR<RLR'RR[RR‡texpaddR#RkRmRnR/RęRhR§R®RPR0RŠRRÜtmaxpRlRR.RžR°tprevexpRĶRoR’RtupperRR–RšRRqR R R”(RRRöRfR‡R.RōRóRLRŠRÜR’RõR0RnRę((Rtsqrtts¢             !)        + ++ +       cCs­t|ƒ}|tjo|Sn|ip |io‚|iƒ}|iƒ}|p|oX|djo|djo|Sn|djo|djo|Sn|i||ƒSq·n|}|i |ƒ}|djo~|i |i jo|i o |}qSqn|i|ijo|i o |}qn|i|ijo|i o |}qnn|djo |}n|djo tƒ}n|itjo|i|ƒSn|S(s Returns the larger value. like max(self, other) except if one is not a number, returns NaN (and signals if one is sNaN). Also rounds. iiii’’’’N(RdR_ReRR=RXtsntonRaRRfRrtcR"R<R'RR•RR–(RR_RRśRłRųRf((RR īs<           cCs­t|ƒ}|tjo|Sn|ip |io‚|iƒ}|iƒ}|p|oX|djo|djo|Sn|djo|djo|Sn|i||ƒSq·n|}|i |ƒ}|djo~|i |i jo|i o |}qSqn|i|ijo|i o |}qn|i|ijo|i o |}qnn|djo |}n|djo tƒ}n|itjo|i|ƒSn|S(s”Returns the smaller value. like min(self, other) except if one is not a number, returns NaN (and signals if one is sNaN). Also rounds. iiiN(RdR_ReRR=RXRųRłRaRRfRrRśR"R<R'RR•RR–(RR_RRśRłRųRf((RRžs<           cCs<|idjotSn|i|i}|dt|ƒjS(s"Returns whether self is an integeriN(i(RR<RSR#trestRP(RRū((RRćPs cCs4|idjodSn|id|id@djS(s7Returns 1 if self is even. Assumes self is an integer.iii’’’’N(RR<R#(R((RRĆWscCs:y|it|iƒdSWntj o dSnXdS(s$Return the adjusted exponent of selfiiN(RR<RPR#RU(R((RRh]s cCs|it|ƒffS(N(Rt __class__RK(R((Rt __reduce__fscCs.t|ƒtjo|Sn|it|ƒƒS(N(ttypeRRRüRK(R((Rt__copy__iscCs.t|ƒtjo|Sn|it|ƒƒS(N(RžRRRüRK(Rtmemo((Rt __deepcopy__ns(s_exps_ints_signs _is_special(GRRR t __slots__R'RBRXRWRaRcRrRsRtRuRzR{R|R“R”R—R˜R›R§t__radd__RoRØR«R®t__rmul__R°t __truediv__RÆRøt __rtruediv__R¹RŗR»R¼RÅRĘRĒRČRŹRĖR–RĢRĶRŲRŚRŪRŻRŽRßRąRįRéRźRyRķRīR”RņR÷R RžRćRĆRhRżR’R(((RRŠsˆ g   5      R   Z  " < ¦      O       +O     [   7 z 1 1    t_round_ic Bs‘tZdZeeeeeeeeded„ Zd„Zd„Zd„Zd„ZeZ ed„Z d„Z d „Z d „Z d „Zd „Zd „Zd„Zd„Zdd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Z ed„Z!d „Z"d!„Z#d"„Z$d#„Z%d$„Z&d%„Z'd&„Z(d'„Z)d(„Z*RS()s,Contains the context for a Decimal instance. Contains: prec - precision (for use in rounding, division, square roots..) rounding - rounding type. (how you round) _rounding_decision - ALWAYS_ROUND, NEVER_ROUND -- do 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 incremented. (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) ic Cs=|djo g}n| djo g} nt|tƒp:tg} tD]}| |||jfqL~ ƒ}~n|dj oKt|tƒ o:tg} tD]}| |||jfq¤~ ƒ}~nx`t ƒi ƒD]O\} } | djo&t || titt| ƒƒƒqąt || | ƒqąW|`dS(N(RnR't_ignored_flagsREtdictt_[1]t_signalsR‹ttrapstlocalstitemstnameRčtsetattrRt_copyR6R×R(RR/R.R RnR•RŠR0RŒRĪRRčRR R‹((Rt__init__”s     33  &cCsŚg}|idt|ƒƒ|iddig}|iiƒD]!\}}|o||i q@q@~ƒdƒ|iddig}|i iƒD]!\}}|o||i q–q–~ƒdƒdi|ƒdS(sShow the current context.saContext(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)dsflags=[s, t]straps=[t)N( R‹R‘tvarsRR†R RnRtftvRR tt(RRR R‹RR((RR|«s VVcCs%x|iD]}d|i|cCst|d|ƒ}|i|ƒS(s9Creates a new Decimal instance but using self as context.RN(RtnumRtdR–(RR!R"((Rtcreate_decimal$ scCs|id|ƒS(s!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") RN(taR›R(RR$((RRN* scCs|i|d|ƒS(sęReturn 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") RN(R$R§tbR(RR$R%((Rtadd< scCst|i|ƒƒS(N(RKR$R–R(RR$((Rt_applyF scCs|i|d|ƒS(s³Compares 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") RN(R$RuR%R(RR$R%((RRuI scCs|i|d|ƒS(s¼Decimal 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") RN(R$R°R%R(RR$R%((Rtdividef scCs|i|d|ƒS(sTDivides 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") RN(R$RĘR%R(RR$R%((Rt divide_int€ s cCs|i|d|ƒS(NR(R$R¹R%R(RR$R%((RR±Œ scCs|i|d|ƒS(sńmax 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 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") RN(R$R R%R(RR$R%((RR  scCs|i|d|ƒS(sõmin 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 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") RN(R$RžR%R(RR$R%((RRž£ scCs|id|ƒS(s“Minus 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") RN(R$R—R(RR$((RR„· s cCs|i|d|ƒS(s multiply 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") RN(R$R®R%R(RR$R%((RtmultiplyÅ scCs|id|ƒS(sunormalize 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") RN(R$RyR(RR$((RRyŚ scCs|id|ƒS(s„Plus 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") RN(R$R˜R(RR$((Rtplusļ s cCs|i||d|ƒS(sERaises a to the power of b, to modulo if given. The right-hand operand must be a whole number whose integer part (after any exponent has been applied) has no more than 9 digits and whose fractional part (if any) is all zeros before any rounding. The operand may be positive, negative, or zero; if negative, the absolute value of the power is used, and the left-hand operand is inverted (divided into 1) before use. If the increased precision needed for the intermediate calculations exceeds the capabilities of the implementation then an Invalid operation condition is raised. If, when raising to a negative power, an underflow occurs during the division into 1, the operation is not halted at that point but continues. >>> ExtendedContext.power(Decimal('2'), Decimal('3')) Decimal("8") >>> ExtendedContext.power(Decimal('2'), Decimal('-3')) Decimal("0.125") >>> ExtendedContext.power(Decimal('1.7'), Decimal('8')) Decimal("69.7575744") >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-2')) Decimal("0") >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-1')) Decimal("0") >>> ExtendedContext.power(Decimal('Infinity'), Decimal('0')) Decimal("1") >>> ExtendedContext.power(Decimal('Infinity'), Decimal('1')) Decimal("Infinity") >>> ExtendedContext.power(Decimal('Infinity'), Decimal('2')) Decimal("Infinity") >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-2')) Decimal("0") >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-1')) Decimal("-0") >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('0')) Decimal("1") >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('1')) Decimal("-Infinity") >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('2')) Decimal("Infinity") >>> ExtendedContext.power(Decimal('0'), Decimal('0')) Decimal("NaN") RN(R$RéR%RäR(RR$R%Rä((Rtpowerż s.cCs|i|d|ƒS(sD Returns a value equal to 'a' (rounded) and 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") RN(R$RķR%R(RR$R%((RRķ. s0cCs|i|d|ƒS(s=Returns 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") RN(R$R»R%R(RR$R%((Rt remaindera scCs|i|d|ƒS(s_Returns 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") RN(R$RÅR%R(RR$R%((RRÅ| scCs |i|ƒS(sReturns 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 N(R$RīR%(RR$R%((RRī— scCs|id|ƒS(spReturns the square 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.prec 9 RN(R$R÷R(RR$((RR÷Ø scCs|i|d|ƒS(sTReturn 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") RN(R$RoR%R(RR$R%((RtsubtractÅ s cCs|id|ƒS(syConverts a number to a string, using scientific notation. The operation is not affected by the context. RN(R$R”R(RR$((RR”Ń scCs|id|ƒS(syConverts a number to a string, using scientific notation. The operation is not affected by the context. RN(R$R“R(RR$((Rt to_sci_stringŲ scCs|id|ƒS(sRounds 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(Decimal('2.1')) Decimal("2") >>> ExtendedContext.to_integral(Decimal('100')) Decimal("100") >>> ExtendedContext.to_integral(Decimal('100.0')) Decimal("100") >>> ExtendedContext.to_integral(Decimal('101.5')) Decimal("102") >>> ExtendedContext.to_integral(Decimal('-101.5')) Decimal("-102") >>> ExtendedContext.to_integral(Decimal('10E+5')) Decimal("1.0E+6") >>> ExtendedContext.to_integral(Decimal('7.89E+77')) Decimal("7.89E+77") >>> ExtendedContext.to_integral(Decimal('-Inf')) Decimal("-Infinity") RN(R$RņR(RR$((RRņß s(+RRR R'RR|R7RkR6R’R[RmR½RqRzR¦RŅRšRlR#RNR&R'RuR(R)R±R RžR„R*RyR+R,RķR-RÅRīR÷R.R”R/Rņ(((RR€sP '                          1 3      RGcBs)tZdZdd„Zd„ZeZRS(NR)RJRLcCs“|djod|_d|_d|_n…t|tƒoM|i|_d}x|i D]}|d|}qXW||_|i |_n(|d|_|d|_|d|_dS(Nii ii( RFR'RR)RJRLRERR"tcumR#RRR<(RRFRRR0((RR s         cCsd|i|i|ifS(Ns (%r, %r, %r)(RR)RJRL(R((RR| s(ssignsintsexp(RRRR'RR|R“(((RRGū s  ic Cs;t|i|iƒ} | djo| } |}|}n |}|}|oĀ| |djo±t t |iƒƒ}t t |iƒƒ}| ||d|joj|d|}|djo d}n|id|9_|i|8_d|_|i|_||fSqn|id| 9_|i| 8_||fS(scNormalizes op1, op2 to have the same exp and length of coefficient. Done during addition. iiii N(RJR¢RLR£RˆR‡R_RR/RPRKttmp_lent other_lenR( R¢R£RR/R‡RR1R_R2Rˆ((RR¤ s.      cCsd}x@|i|ijo,|id9_|id8_|d7}q WxD|id|ijo,|id9_|id8_|d8}qLW|||fS(s½Adjust op1, op2 so that op2.int * 10 > op1.int >= op2.int. Returns the adjusted op1, op2 as well as the change in op1.exp-op2.exp. Used on _WorkRep instances during division. ii iN(RµR£RJR¢RL(R¢R£Rµ((RR“A scCs@t|tƒo|Snt|ttfƒot|ƒSntS(s]Convert other to Decimal. Verifies that it's ok to use in an implicit construction. N(RER_RRJRMRe(R_((RRdY s Rātinfinitys+infs +infinitys-infi’’’’s -infinitycCs"t|ƒiƒ}ti|dƒS(s}Determines whether a string or float is infinity. +1 for negative infinity; 0 for finite ; +1 for positive infinity iN(RKR!R’t _infinity_mapR(R!((RRWm scCs%t|ƒiƒ}|pdSnd}|ddjo|d}n&|ddjo|d}d}n|idƒoKt|ƒdjo|diƒ odSnd||didƒfSn|idƒoKt|ƒd jo|d iƒ odSnd ||d idƒfSndS( s„Determines whether a string or float is NaN (1, sign, diagnostic info as string) => NaN (2, sign, diagnostic info as string) => sNaN 0 => not a NaN iR‚iR}tnaniR>tsnaniiN(RKR!R’R)t startswithRPtisdigittlstrip(R!R)((RRXu s&  %%R/iR.R RnR•R0i’ɚ;RŠi6eÄRŒi tInfs-InfR$s£ # \s* (?P[-+])? ( (?P\d+) (\. (?P\d*))? | \. (?P\d+) ) ([eE](?P[-+]? \d+))? # \s* $ c Cs…t|ƒ}|djotd|ƒ‚n|idƒdjo d}nd}|idƒ}|djo d}n t|ƒ}|idƒ}|djod}|id ƒ}n'|id ƒ}|djo d}n|t |ƒ8}||}t t|ƒ}|}x$|o|ddjo |d=qW|p/|o|t|ƒ|fSn|d |fSnt|ƒ}|||fS( Nsinvalid literal for Decimal: %rR)R}iiRLRJR~tonlyfractfrac(i(t_parserR‹tmR'RQtgroupR)RLRJtintparttfracpartRPtmantissaRIR‡tbackupRH( R‹R‡R>R)RBRLRARCR@((RR\Ś s<            t__main__(OR t__all__R6RRRRRRRRRRtArithmeticErrorRRRR%tZeroDivisionErrorRR*R+R R-R R R R R RR8t ImportErrorR2R1R4R:thasattrR9R5RRRARR t__dict__tkeysRR7trounding_functionsRöt globalnametglobalsRčRŲRRGR¤R“RdR4RWRXRRRR:tnegInfR(R$tretcompiletVERBOSEtmatchR=R\RtdoctestttestmodR3(9RdR“R(R\RR-RGR RRRWRRRRR=RERRR R$R R:RR%RR8R*R R4RRR2RRRMRORR1RLR R RRRRR RčRRTR¤R4RXR+RRRP((Rt?ts¼K        &*           ’’’’’’­>  ’’}'  <  #          +