3 \T$@sFdddddddddd d d d d ddddddddddddddddddd d!d"d#g$ZeZd$Zd%Zd&Zd'd(lZd'd(lZd'd(l Z yd'd)l m Z e dd*Z Wnek rd+d,Z YnXdZdZdZdZdZdZdZdZd-Ze jdkrd1Zd1ZdZn d2Zd2ZdZeed0ZGd3ddeZGd4ddeZGd5ddeZ Gd6dde Z!Gd7d d ee"Z#Gd8dde Z$Gd9dde e"Z%Gd:d d eZ&Gd;dde Z'Gdd d e&e(Z*Gd?dde&e(e)Z+Gd@ddee,Z-ee#e&e*e(e+e e)e-g Z.e!e e$e e%e e'e iZ/eeeeeeeefZ0y d'd(l1Z1Wn.ek rdGdAdBdBe2Z3e3Z1[3YnXy e1j4Wn>e5k re6e1j7dCre1j7`8dDdZ9dEdZ:Yn6Xe1j4Z4e6e4dCre4`8e4fdFdZ:e4fdGdZ9[1[4ddHdZ;GdIdde2Zj?e<GdMdNdNe2Z@GdOdde2ZAGdPdQdQe2ZBddRdSZCeDjEZFdTdUZGdVdWZHdXdYZIdZd[ZJdd]d^ZKd_d`ZLdadbZMGdcdddde2ZNeNjOZPddedfZQdgdhZRdidjZSdkdldmdndodpdqdrdsdt fdudvZTddwdxZUddydzZVeAd{ee#e*e ggd|dd0d'd}ZWeAd~ee#e*e ee+ggdZXeAd~eggdZYd'd(lZZZeZj[deZj\eZj]Bj^Z_eZj[dj^Z`eZj[dj^ZaeZj[deZj\eZjbBZc[Zy d'd(ldZeWnek rYnXdddZfddZgddZhdddZiddZjddZkesr+T?lNZoi@Tc@seZdZddZdS)rcGsdS)Nr()selfcontextr)r(r(r*handleszDecimalException.handleN)__name__ __module__ __qualname__r1r(r(r(r*rsc@s eZdZdS)rN)r2r3r4r(r(r(r*rs c@seZdZddZdS)r cGs,|r(t|dj|djdd}|j|StS)Nr&nT)_dec_from_triple_sign_int_fix_nan_NaN)r/r0r)ansr(r(r*r1s zInvalidOperation.handleN)r2r3r4r1r(r(r(r*r sc@seZdZddZdS)rcGstS)N)r:)r/r0r)r(r(r*r1szConversionSyntax.handleN)r2r3r4r1r(r(r(r*rsc@seZdZddZdS)r cGst|S)N)_SignedInfinity)r/r0signr)r(r(r*r1szDivisionByZero.handleN)r2r3r4r1r(r(r(r*r s c@seZdZddZdS)rcGstS)N)r:)r/r0r)r(r(r*r1szDivisionImpossible.handleN)r2r3r4r1r(r(r(r*rsc@seZdZddZdS)rcGstS)N)r:)r/r0r)r(r(r*r1)szDivisionUndefined.handleN)r2r3r4r1r(r(r(r*r!sc@s eZdZdS)r N)r2r3r4r(r(r(r*r ,s c@seZdZddZdS)rcGstS)N)r:)r/r0r)r(r(r*r1CszInvalidContext.handleN)r2r3r4r1r(r(r(r*r8s c@s eZdZdS)r N)r2r3r4r(r(r(r*r Fs c@s eZdZdS)r N)r2r3r4r(r(r(r*r Rs c@seZdZddZdS)rcGs|jttttfkrt|S|dkrR|jtkr4t|St|d|j|j |jdS|dkr|jt krlt|St|d|j|j |jdSdS)Nr&9r.) roundingrrrrr<rr6precEmaxr)r/r0r=r)r(r(r*r1ss     zOverflow.handleN)r2r3r4r1r(r(r(r*r]sc@s eZdZdS)rN)r2r3r4r(r(r(r*rs c@s eZdZdS)rN)r2r3r4r(r(r(r*rs c@seZdZefddZdS) MockThreadingcCs |jtS)N)modules __xname__)r/sysr(r(r*localszMockThreading.localN)r2r3r4rErFr(r(r(r*rBsrB__decimal_context__cCs,|tttfkr|j}|j|tj_dS)N)rrrcopy clear_flags threadingcurrent_threadrG)r0r(r(r*rsc Cs4y tjjStk r.t}|tj_|SXdS)N)rJrKrGAttributeErrorr)r0r(r(r*rs   c Cs,y|jStk r&t}||_|SXdS)N)rGrLr)_localr0r(r(r*rs cCs(|tttfkr|j}|j||_dS)N)rrrrHrIrG)r0rMr(r(r*rscCs|dkrt}t|S)N)r_ContextManager)Zctxr(r(r*rs$c @seZdZdZdddZed d Zd d Zd dZdddZ ddZ ddZ ddZ dddZ dddZdddZdddZddd Zdd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zdd,d-Zdd.d/Zdd0d1Zdd2d3Zdd5d6Zdd7d8ZeZdd9d:Zdd;d<Zdd=d>Z e Z!dd?d@Z"dAdBZ#ddCdDZ$ddEdFZ%d dGdHZ&d dIdJZ'd dKdLZ(d dMdNZ)d dOdPZ*ddQdRZ+dSdTZ,dUdVZ-e-Z.dWdXZ/e0e/Z/dYdZZ1e0e1Z1d[d\Z2d]d^Z3d_d`Z4dadbZ5dcddZ6dedfZ7dgdhZ8didjZ9dkdlZ:dmdnZ;dodpZe6e7e8e9e:e;e          z Decimal._cmpcCs<t||dd\}}|tkr|S|j||r.dS|j|dkS)NT) equality_opFr&)_convert_for_comparisonNotImplementedrr)r/rr0r(r(r*__eq__s  zDecimal.__eq__cCs<t||\}}|tkr|S|j||}|r.dS|j|dkS)NFr&)rrrr)r/rr0r;r(r(r*__lt__s zDecimal.__lt__cCs<t||\}}|tkr|S|j||}|r.dS|j|dkS)NFr&)rrrr)r/rr0r;r(r(r*__le__s zDecimal.__le__cCs<t||\}}|tkr|S|j||}|r.dS|j|dkS)NFr&)rrrr)r/rr0r;r(r(r*__gt__s zDecimal.__gt__cCs<t||\}}|tkr|S|j||}|r.dS|j|dkS)NFr&)rrrr)r/rr0r;r(r(r*__ge__s zDecimal.__ge__cCs>t|dd}|js|r0|jr0|j||}|r0|St|j|S)NT)raiseit)_convert_otherrPrrr)r/rr0r;r(r(r*compares   zDecimal.comparecCs|jr4|jrtdn|jr$tS|jr0t StS|jdkrNtd|jt }ntt |j t }t |j |t }|dkr||n| }|dkrdS|S)Nz"Cannot hash a signaling NaN value.r& r.r,r) rPrrris_nan _PyHASH_NANr7 _PyHASH_INFrOpow_PyHASH_MODULUS _PyHASH_10INVrUr8)r/Zexp_hashZhash_r;r(r(r*__hash__s  zDecimal.__hash__cCst|jttt|j|jS)N)rr7rkrorUr8rO)r/r(r(r*as_tupleszDecimal.as_tuplecCs|jr |jrtdntd|s(dSt|j}|jdkrR|d|jd}}nr|j }x(|dkr|ddkr|d}|d8}q\W|j }t|| @jd|}|r||L}||8}d||>}|j r| }||fS)Nz#cannot convert NaN to integer ratioz(cannot convert Infinity to integer ratior&r.rrz)r&r.) rPrrl OverflowErrorrUr8rOminr~r7)r/r5rZd5Zd2Zshift2r(r(r*r}s,     zDecimal.as_integer_ratiocCs dt|S)Nz Decimal('%s'))r`)r/r(r(r*__repr__'szDecimal.__repr__Fc Csdddg|j}|jrL|jdkr&|dS|jdkr>|d|jS|d|jS|jt|j}|jdkrt|dkrt|}n6|s~d }n,|jd kr|d d d }n|d d d }|dkrd }d d | |j}nN|t|jkr|jd |t|j}d}n |jd|}d |j|d}||kr*d}n*|dkr:t}ddg|jd||}||||S)NrSrTrZZInfinityr5NaNrr&r.rQr[.eEz%+di)r7rPrOr8rfrcapitals) r/engr0r= leftdigitsdotplacervrwrVr(r(r*__str__,s:     zDecimal.__str__cCs|jd|dS)NT)rr0)r)r/r0r(r(r* to_eng_string`szDecimal.to_eng_stringcCsT|jr|j|d}|r|S|dkr(t}| rB|jtkrB|j}n|j}|j|S)N)r0)rPrrr?rcopy_abs copy_negate_fix)r/r0r;r(r(r*__neg__is  zDecimal.__neg__cCsT|jr|j|d}|r|S|dkr(t}| rB|jtkrB|j}nt|}|j|S)N)r0)rPrrr?rrrr)r/r0r;r(r(r*__pos__s  zDecimal.__pos__TcCsJ|s |jS|jr&|j|d}|r&|S|jr:|j|d}n |j|d}|S)N)r0)rrPrr7rr)r/roundr0r;r(r(r*__abs__s  zDecimal.__abs__c Cslt|}|tkr|S|dkr"t}|js.|jr|j||}|rB|S|jrr|j|jkrj|jrj|jtdSt |S|jrt |St |j |j }d}|j t kr|j|jkrd}| r| rt |j|j}|rd}t|d|}|j|}|S|s"t||j |jd}|j||j }|j|}|S|sZt||j |jd}|j||j }|j|}|St|}t|}t|||j\}}t} |j|jkr|j|jkrt|d|}|j|}|S|j|jkr||}}|jdkrd| _|j|j|_|_nd| _n&|jdkrd| _d\|_|_nd| _|jdkr@|j|j| _n|j|j| _|j| _t | }|j|}|S)Nz -INF + INFr&r.rQ)r&r&)rrrrPrrr7rdr rrrOr?rr6rmaxr@_rescaleri _normalizer=rUrV) r/rr0r;rVZ negativezeror=op1op2rr(r(r*__add__s|               zDecimal.__add__cCsHt|}|tkr|S|js |jr6|j||d}|r6|S|j|j|dS)N)r0)rrrPrrr)r/rr0r;r(r(r*__sub__s zDecimal.__sub__cCs"t|}|tkr|S|j||dS)N)r0)rrr)r/rr0r(r(r*__rsub__szDecimal.__rsub__cCsDt|}|tkr|S|dkr"t}|j|jA}|js:|jr|j||}|rN|S|jrn|sf|jtdSt |S|jr|s|jtdSt |S|j |j }| s| rt |d|}|j |}|S|j dkrt ||j |}|j |}|S|j dkrt ||j |}|j |}|St|}t|}t |t|j|j|}|j |}|S)Nz (+-)INF * 0z 0 * (+-)INFrQ1)rrrr7rPrrrdr r<rOr6rr8rir`rU)r/rr0Z resultsignr;Z resultexprrr(r(r*__mul__sH              zDecimal.__mul__c Cst|}|tkrtS|dkr"t}|j|jA}|js:|jr|j||}|rN|S|jrj|jrj|jtdS|jrzt |S|jr|jt dt |d|j S|s|s|jt dS|jtd|S|s|j|j}d}nt|jt|j|jd}|j|j|}t|}t|} |dkr:t|jd|| j\}} nt|j| jd| \}} | rt|d dkr|d7}n<|j|j} x.|| kr|ddkr|d}|d7}qWt |t||}|j|S) Nz(+-)INF/(+-)INFzDivision by infinityrQz0 / 0zx / 0r&r.rrz)rrrr7rPrrrdr r<rr6Etinyrr rOrfr8r@ridivmodrUr`r) r/rr0r=r;rVcoeffshiftrr remainder ideal_expr(r(r* __truediv__QsP          zDecimal.__truediv__c Cs |j|jA}|jr|j}nt|j|j}|j|j}| sP|jsP|dkrjt|dd|j||jfS||jkr t |}t |}|j |j kr|j d|j |j 9_ n|j d|j |j 9_ t |j |j \}} |d|jkr t|t |dt|jt | |fS|jtd} | | fS)Nr,rQr&rz%quotient too large in //, % or divmodr)r7rrOrrr6rr?r@rirVrUrr`rdr) r/rr0r=rexpdiffrrqrr;r(r(r*_divides*    zDecimal._dividecCs"t|}|tkr|S|j||dS)N)r0)rrr)r/rr0r(r(r* __rtruediv__szDecimal.__rtruediv__cCst|}|tkr|S|dkr"t}|j||}|r:||fS|j|jA}|jr~|jrj|jtd}||fSt||jtdfS|s|s|jt d}||fS|jt d||jtdfS|j ||\}}|j |}||fS)Nzdivmod(INF, INF)zINF % xz divmod(0, 0)zx // 0zx % 0) rrrrr7rrdr r<rr rr)r/rr0r;r=Zquotientrr(r(r* __divmod__s0      zDecimal.__divmod__cCs"t|}|tkr|S|j||dS)N)r0)rrr)r/rr0r(r(r* __rdivmod__szDecimal.__rdivmod__cCst|}|tkr|S|dkr"t}|j||}|r6|S|jrJ|jtdS|sj|r^|jtdS|jtdS|j||d}|j |}|S)NzINF % xzx % 0z0 % 0r.) rrrrrrdr rrr)r/rr0r;rr(r(r*__mod__s"     zDecimal.__mod__cCs"t|}|tkr|S|j||dS)N)r0)rrr)r/rr0r(r(r*__rmod__szDecimal.__rmod__c Cs|dkrt}t|dd}|j||}|r.|S|jrB|jtdS|sb|rV|jtdS|jtdS|jr|t|}|j|St |j |j }|st |j d|}|j|S|j |j }||jdkr|jtS|d kr|j||j}|j|St|}t|}|j|jkr(|jd |j|j9_n|jd |j|j9_t|j|j\}} d| |d@|jkr~| |j8} |d7}|d |jkr|jtS|j } | d krd| } | } t | t| |}|j|S) NT)rzremainder_near(infinity, x)zremainder_near(x, 0)zremainder_near(0, 0)rQr.r,rr&r)rrrrrdr rrrrrOr6r7rr@rrr?rirVrUrr`) r/rr0r;ideal_exponentrrrrrr=r(r(r*remainder_nearsZ         zDecimal.remainder_nearcCst|}|tkr|S|dkr"t}|j||}|r6|S|jrb|jrR|jtdSt|j|jAS|s|r|jt d|j|jAS|jt dS|j ||dS)Nz INF // INFzx // 0z0 // 0r&) rrrrrrdr r<r7r rr)r/rr0r;r(r(r* __floordiv__Ls$   zDecimal.__floordiv__cCs"t|}|tkr|S|j||dS)N)r0)rrr)r/rr0r(r(r* __rfloordiv__hszDecimal.__rfloordiv__cCs8|jr(|jrtd|jr"dnd}nt|}t|S)Nz%Cannot convert signaling NaN to floatz-nannan)rrrlr7r`rp)r/sr(r(r* __float__os zDecimal.__float__cCst|jr(|jrtdn|jr(tdd|j}|jdkrT|t|jd|jS|t|jd|jpjdSdS)NzCannot convert NaN to integerz"Cannot convert infinity to integerr.r&rrQr) rPrrlrrr7rOrUr8)r/rr(r(r*__int__ys   zDecimal.__int__cCs|S)Nr()r/r(r(r*realsz Decimal.realcCstdS)Nr&)r)r/r(r(r*imagsz Decimal.imagcCs|S)Nr()r/r(r(r* conjugateszDecimal.conjugatecCs tt|S)N)complexrp)r/r(r(r* __complex__szDecimal.__complex__cCsR|j}|j|j}t||krJ|t||djd}t|j||jdSt|S)NrQT) r8r@clamprfrgr6r7rOr)r/r0ZpayloadZmax_payload_lenr(r(r*r9s   zDecimal._fix_nancCsX|jr |jr|j|St|S|j}|j}|s|j|g|j}tt |j ||}||j krx|j t t |jd|St|St|j|j |j}||kr|j td|j}|j t|j t|S||k}|r|}|j |krt|j|j |} | dkrt |jd|d}d} |j|j} | || } |jd| p>d} | dkr~tt| d} t| |jkr~| dd} |d7}||kr|j td|j}nt |j| |}| r|r|j t|r|j t| r|j t|j t|s|j t |S|r |j t|jdkrP|j |krP|j t |jd|j |} t |j| |St|S)NrQz above Emaxr&rr.r)rPrr9rrEtoprArrrrOrdrr6r7rfr8r@rr r _pick_rounding_functionr?r`rUrr )r/r0rrexp_maxZnew_expZexp_minr;Zself_is_subnormalrxZrounding_methodchangedrrr(r(r*rsn                     z Decimal._fixcCst|j|rdSdSdS)Nr&r.r) _all_zerosr8)r/r@r(r(r* _round_downs zDecimal._round_downcCs |j| S)N)r)r/r@r(r(r* _round_upszDecimal._round_upcCs*|j|dkrdSt|j|r"dSdSdS)NZ56789r.r&r)r8r)r/r@r(r(r*_round_half_ups  zDecimal._round_half_upcCst|j|rdS|j|SdS)Nr.r) _exact_halfr8r)r/r@r(r(r*_round_half_downs zDecimal._round_half_downcCs8t|j|r*|dks&|j|ddkr*dS|j|SdS)Nr&r.02468r)rr8r)r/r@r(r(r*_round_half_even#s zDecimal._round_half_evencCs |jr|j|S|j| SdS)N)r7r)r/r@r(r(r*_round_ceiling+s zDecimal._round_ceilingcCs |js|j|S|j| SdS)N)r7r)r/r@r(r(r* _round_floor2s zDecimal._round_floorcCs0|r |j|ddkr |j|S|j| SdS)Nr.Z05)r8r)r/r@r(r(r* _round_05up9s zDecimal._round_05up)rrrrrrrrcCsb|dk r2t|tstdtdd| }|j|S|jrR|jrJtdntdt|j dt S)Nz+Second argument to round should be integralr&rzcannot round a NaNzcannot round an infinity) r_rUrrr6quantizerPrrlrrr)r/r5rVr(r(r* __round__Ks/   zDecimal.__round__cCs0|jr |jrtdntdt|jdtS)Nzcannot round a NaNzcannot round an infinityr&)rPrrlrrUrr)r/r(r(r* __floor__s  zDecimal.__floor__cCs0|jr |jrtdntdt|jdtS)Nzcannot round a NaNzcannot round an infinityr&)rPrrlrrUrr)r/r(r(r*__ceil__s  zDecimal.__ceil__cCst|dd}t|dd}|js$|jr|dkr2t}|jdkrJ|jtd|S|jdkrb|jtd|S|jdkrr|}nf|jdkr|}nV|jdkr|s|jtdSt|j|jA}n*|jdkr|s|jtdSt|j|jA}n0t|j|jAt t |j t |j |j|j}|j ||S) NT)rrYrr5rZzINF * 0 in fmaz0 * INF in fma) rrPrrOrdr r<r7r6r`rUr8r)r/rZthirdr0productr(r(r*fmas6          z Decimal.fmac Cst|}|tkr|St|}|tkr(|S|dkr6t}|j}|j}|j}|sZ|sZ|r|dkrp|jtd|S|dkr|jtd|S|dkr|jtd|S|r|j|S|r|j|S|j|S|jo|jo|js|jtdS|dkr|jtdS|s |jtdS|j|j kr(|jtdS| rD| rD|jtdS|j rTd}n|j }t t |}t|j}t|j} |j |td |j||}x t| jD]} t|d |}qWt|| j |}t|t|dS) Nr,rz@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)rrrrrdr r9 _isintegerrr@_isevenr7rhrUrito_integral_valuerrVranger6r`) r/rmodulor0rrZ modulo_is_nanr=baseexponentir(r(r* _power_modulosl        zDecimal._power_modulocCst|}|j|j}}x |ddkr6|d}|d7}qWt|}|j|j}}x |ddkrn|d}|d7}qPW|dkr||9}x |ddkr|d}|d7}qW|dkrdS|d|} |jdkr| } |jo|jdkr|jt|} t| | |d} nd} tddd| | | S|jdkr|d} | dkr|| @|krPdSt |d} |d d }|t t |kr~dSt | ||} t |||}| dks|dkrdS| |krdSd | }n| d krt |d d } t d | |\}}|rdSx$|d dkr$|d }| d8} qW|dd}|t t |krHdSt | ||} t |||}| dksx|dkr|dS| |krdSd| }ndS|d|krdS| |}tdt ||S|dkr|d|d}}n|dkr t t t||| kr dSt |}|dkr>t t t||| kr>dS|d| }}x:|d|dkoldknr|d}|d}qPWx:|d |d kodknr|d }|d }qW|dkrt|dkr||krdSt ||\}}|dkrdSdt | | >}x>t |||d\}}||kr>Pn||d||}qW||koh|dkspdS|}|dkr||dt|krdS||}||9}|d|krdSt |}|jr|jdkr|jt|} t|| |t |} nd} td|d| || S)Nrr&r.rrQr,r]Arzr[d)r,rrr)rirUrVr=rr7rOrr6_nbitsrfr`_decimal_lshift_exactrrh _log10_lb)r/rpxxcxeyycyerrZzerosZ last_digitrZemaxrrur5Zxc_bitsremarrZstr_xcr(r(r* _power_exact(s:                  &&&&    zDecimal._power_exactcCs@|dk r|j|||St|}|tkr*|S|dkr8t}|j||}|rL|S|sd|s`|jtdStSd}|jdkr|j r|j sd}n|r|jtdS|j }|s|jdkrt |ddSt |S|jr|jdkrt |St |ddS|tkr|j rZ|jdkrd}n||jkr"|j}nt|}|j|}|d|jkrxd|j}|jtn|jt|jtd|j}t |dd| |S|j}|jr|jdk|dkkrt |ddSt |Sd}d} |j|j} |dk|jdkkr| tt|jkrHt |d|jd}n,|j} | tt| krHt |d| d}|dkr|j||jd}|dk r|dkrt d|j|j}d} |dkr:|j} t|} | j| j}}t|}|j|j}}|jdkr| }d }xJt||||| |\}}|d d tt|| drP|d 7}qWt |t||}| r2|j r2t|j|jkr|jdt|j}t |j|jd||j|}|j }|j!xt"D]}d|j#|<qW|j$|}|jt|j%t&r|jt'|j%t(r|jt(d |jx:t't&ttt)fD]}|j%|r|j|qWn |j$|}|S) Nz0 ** 0r&r.z+x ** y with x negative and y not an integerrQrFTr[rzrz above Emax)*rrrrrrdr _Oner7rrrr6r<rr@rUrOr r r_log10_exp_boundrfr`rArrr8rirVr=_dpowerrHrI_signalstrapsrflagsr rrr)r/rrr0r;Z result_signZ multiplierrVZself_adjexactZboundrr r rrrrrextrarrZ newcontextZ exceptionr(r(r*__pow__ s                        "         zDecimal.__pow__cCs"t|}|tkr|S|j||dS)N)r0)rrr)r/rr0r(r(r*__rpow__ szDecimal.__rpow__cCs|dkrt}|jr(|j|d}|r(|S|j|}|jr>|S|sPt|jddS|j|jg|j }t |j }|j }x.|j |ddkr||kr|d7}|d8}qvWt|j|j d||S)N)r0rQr&r.) rrPrrrr6r7rArrrfr8rO)r/r0r;duprendrVr(r(r* normalize s$    zDecimal.normalizecCst|dd}|dkrt}|dkr(|j}|js4|jr||j||}|rH|S|jsX|jr||jrp|jrpt|S|jtdS|j |j ko|j kns|jtdS|st |j d|j }|j|S|j}||j kr|jtdS||j d|jkr|jtdS|j|j |}|j|j kr0|jtdSt|j|jkrN|jtdS|rn|j|jkrn|jt|j |j kr||kr|jt|jt|j|}|S) NT)rzquantize with one INFz)target exponent out of bounds in quantizerQz9exponent of quantize result too large for current contextr.z7quantize result has too many digits for current context)rrr?rPrrrrdr rrOrAr6r7rrr@rrfr8Eminr r r )r/rVr?r0r;rr(r(r*r sT           zDecimal.quantizecCsDt|dd}|js|jr8|jr(|jp6|jo6|jS|j|jkS)NT)r)rrPr is_infiniterO)r/rr0r(r(r* same_quantumJ s  zDecimal.same_quantumcCs|jrt|S|s t|jd|S|j|krHt|j|jd|j||St|j|j|}|dkrzt|jd|d}d}|j|}|||}|jd|pd}|dkrtt |d}t|j||S)NrQr&rr.) rPrr6r7rOr8rfrr`rU)r/rVr?rxZ this_functionrrr(r(r*rY s"    zDecimal._rescalecCsh|dkrtd|js| r$t|S|j|jd||}|j|jkrd|j|jd||}|S)Nr&z'argument should be at least 1 in _roundr.)rlrPrrr)r/placesr?r;r(r(r*_round{ s  zDecimal._roundcCs|jr"|j|d}|r|St|S|jdkr4t|S|sFt|jddS|dkrTt}|dkrb|j}|jd|}||kr|j t |j t |S)N)r0r&rQ) rPrrrOr6r7rr?rrdr r )r/r?r0r;r(r(r*to_integral_exact s$      zDecimal.to_integral_exactcCs`|dkrt}|dkr|j}|jr>|j|d}|r6|St|S|jdkrPt|S|jd|SdS)N)r0r&)rr?rPrrrOr)r/r?r0r;r(r(r*r s  zDecimal.to_integral_valuecCs|dkrt}|jrB|j|d}|r(|S|jrB|jdkrBt|S|sdt|jd|jd}|j|S|jdkrz|j t dS|j d}t |}|j d?}|j d@r|jd}t|jd?d}n|j}t|jdd?}||}|dkr|d|9}d } nt|d| \}} | } ||8}d|} x(|| } | | kr:Pn | | d?} q$W| o\| | |k} | r|dkr|| d|} n| d| 9} ||7}n| d dkr| d7} tdt| |}|j}|jt} |j|}| |_|S) N)r0r&rQr,r.zsqrt(-x), x > 0rrTrz)rrPrrr7rr6rOrrdr r@rirVrUrfr8rr` _shallow_copy _set_roundingrr?)r/r0r;r@oprclrrrr5rr?r(r(r*sqrt s`              z Decimal.sqrtcCst|dd}|dkrt}|js&|jr~|j}|j}|s>|r~|dkrX|dkrX|j|S|dkrr|dkrr|j|S|j||S|j|}|dkr|j|}|dkr|}n|}|j|S)NT)rr.r&r)rrrPrrrr compare_total)r/rr0snonr,r;r(r(r*r% s&       z Decimal.maxcCst|dd}|dkrt}|js&|jr~|j}|j}|s>|r~|dkrX|dkrX|j|S|dkrr|dkrr|j|S|j||S|j|}|dkr|j|}|dkr|}n|}|j|S)NT)rr.r&r)rrrPrrrrr/)r/rr0r0r1r,r;r(r(r*rO s&       z Decimal.mincCs8|jr dS|jdkrdS|j|jd}|dt|kS)NFr&TrQ)rPrOr8rf)r/restr(r(r*rq s  zDecimal._isintegercCs(| s|jdkrdS|jd|jdkS)Nr&Tr.rr)rOr8)r/r(r(r*rz szDecimal._isevenc Cs.y|jt|jdStk r(dSXdS)Nr.r&)rOrfr8rr)r/r(r(r*r szDecimal.adjustedcCs|S)Nr()r/r(r(r* canonical szDecimal.canonicalcCs.t|dd}|j||}|r |S|j||dS)NT)r)r0)rrr)r/rr0r;r(r(r*compare_signal s   zDecimal.compare_signalcCsft|dd}|jr|j rtS|j r0|jr0tS|j}|j}|j}|sP|r||krt|j|jf}t|j|jf}||kr|rtStS||kr|rtStStS|r|dkrtS|dkrtS|dkrtS|dkrtSn4|dkrtS|dkrtS|dkrtS|dkrtS||krtS||kr*tS|j|jkrF|rBtStS|j|jkrb|r^tStStS)NT)rr.r,) rr7 _NegativeOnerrrfr8_ZerorO)r/rr0r=Zself_nanZ other_nanZself_keyZ other_keyr(r(r*r/ sf      zDecimal.compare_totalcCs&t|dd}|j}|j}|j|S)NT)r)rrr/)r/rr0ror(r(r*compare_total_mag s zDecimal.compare_total_magcCstd|j|j|jS)Nr&)r6r8rOrP)r/r(r(r*r szDecimal.copy_abscCs2|jrtd|j|j|jStd|j|j|jSdS)Nr&r.)r7r6r8rOrP)r/r(r(r*r szDecimal.copy_negatecCs"t|dd}t|j|j|j|jS)NT)r)rr6r7r8rOrP)r/rr0r(r(r* copy_sign s  zDecimal.copy_signc Cs|dkrt}|j|d}|r"|S|jd kr2tS|s:tS|jdkrNt|S|j}|j}|jdkr|t t |j ddkrt dd|j d}n0|jdkr|t t |j ddkrt dd|j d}n|jdko|| kr t ddd|dd| }n|jdkrB|| dkrBt dd|d| d}nt|}|j|j}}|jdkrj| }d}xFt||||\} } | dd t t | |drP|d7}qpWt dt | | }|j}|jt} |j|}| |_|S) N)r0r.r&r[rrQr>rzrr)rrrr6rrr@rr7rfr`rAr6rrirUrVr=_dexpr)r*rrr?) r/r0r;r adjr+r,rrrrVr?r(r(r*rV sJ   $(  "  z Decimal.expcCsdS)NTr()r/r(r(r* is_canonicalL szDecimal.is_canonicalcCs|j S)N)rP)r/r(r(r* is_finiteT szDecimal.is_finitecCs |jdkS)NrZ)rO)r/r(r(r*r$\ szDecimal.is_infinitecCs |jdkS)Nr5rY)r5rY)rO)r/r(r(r*r` szDecimal.is_nancCs,|js | rdS|dkrt}|j|jkS)NF)rPrr#r)r/r0r(r(r* is_normald s  zDecimal.is_normalcCs |jdkS)Nr5)rO)r/r(r(r*rl szDecimal.is_qnancCs |jdkS)Nr.)r7)r/r(r(r* is_signedp szDecimal.is_signedcCs |jdkS)NrY)rO)r/r(r(r*rt szDecimal.is_snancCs,|js | rdS|dkrt}|j|jkS)NF)rPrrr#)r/r0r(r(r* is_subnormalx s  zDecimal.is_subnormalcCs|j o|jdkS)NrQ)rPr8)r/r(r(r*is_zero szDecimal.is_zerocCs|jt|jd}|dkr4tt|dddS|dkrXttd|dddSt|}|j|j}}|dkrt|d| }t|}t|t|||kS|ttd| |dS)Nr.rr,r&rr)rOrfr8r`rirUrV)r/r;r+r,rnumdenr(r(r* _ln_exp_bound szDecimal._ln_exp_boundc Cs |dkrt}|j|d}|r"|S|s*tS|jdkr:tS|tkrFtS|jdkr\|jt dSt |}|j |j }}|j }||jd}x>t|||}|ddttt||drP|d7}qWtt |dktt|| }|j}|jt} |j|}| |_|S) N)r0r.zln of a negative valuer,rzrr[r&)rr_NegativeInfinityr _Infinityrr6r7rdr rirUrVr@rE_dlogrfr`rhr6r)r*rrr?) r/r0r;r+r,rr r&rr?r(r(r*ln s:    $   z Decimal.lncCs|jt|jd}|dkr,tt|dS|dkrHttd|dSt|}|j|j}}|dkrt|d| }td|}t|t|||kdStd| |}t|||dkdS) Nr.r,r&rZ231rr)rOrfr8r`rirUrV)r/r;r+r,rrCrDr(r(r*r s  zDecimal._log10_exp_boundc CsH|dkrt}|j|d}|r"|S|s*tS|jdkr:tS|jdkrP|jtdS|jddkr|jdddt |jdkrt |j t |jd}nt |}|j |j}}|j}||jd}x>t|||}|dd t tt||drP|d 7}qWtt |dktt|| }|j}|jt} |j|}| |_|S) N)r0r.zlog10 of a negative valuer&rrQr,rzrr[)rrrFrrGr7rdr r8rfrrOrirUrVr@r_dlog10r`rhr6r)r*rrr?) r/r0r;r+r,rr r&rr?r(r(r*log10 s:   . $   z Decimal.log10cCsV|j|d}|r|S|dkr"t}|jr.tS|s@|jtddSt|j}|j|S)N)r0zlogb(0)r.) rrrrGrdr rrr)r/r0r;r(r(r*logb s  z Decimal.logbcCs8|jdks|jdkrdSx|jD]}|dkr dSq WdS)Nr&FZ01T)r7rOr8)r/digr(r(r* _islogical> s  zDecimal._islogicalcCs|jt|}|dkr$d||}n|dkr<||j d}|jt|}|dkr`d||}n|dkrx||j d}||fS)Nr&rQ)r@rf)r/r0opaopbZdifr(r(r* _fill_logicalL szDecimal._fill_logicalcCs~|dkrt}t|dd}|j s.|j r8|jtS|j||j|j\}}djddt||D}t d|j dpxddS)NT)rrScSs$g|]\}}tt|t|@qSr()r`rU).0rbr(r(r* g sz'Decimal.logical_and..r&rQ) rrrOrdr rRr8rnzipr6rg)r/rr0rPrQrr(r(r* logical_andY s  zDecimal.logical_andcCs(|dkrt}|jtdd|jd|S)Nr&r)r logical_xorr6r@)r/r0r(r(r*logical_invertj szDecimal.logical_invertcCs~|dkrt}t|dd}|j s.|j r8|jtS|j||j|j\}}djddt||D}t d|j dpxddS)NT)rrScSs$g|]\}}tt|t|BqSr()r`rU)rSrrTr(r(r*rU sz&Decimal.logical_or..r&rQ) rrrOrdr rRr8rnrVr6rg)r/rr0rPrQrr(r(r* logical_orq s  zDecimal.logical_orcCs~|dkrt}t|dd}|j s.|j r8|jtS|j||j|j\}}djddt||D}t d|j dpxddS)NT)rrScSs$g|]\}}tt|t|AqSr()r`rU)rSrrTr(r(r*rU sz'Decimal.logical_xor..r&rQ) rrrOrdr rRr8rnrVr6rg)r/rr0rPrQrr(r(r*rX s  zDecimal.logical_xorcCst|dd}|dkrt}|js&|jr~|j}|j}|s>|r~|dkrX|dkrX|j|S|dkrr|dkrr|j|S|j||S|jj|j}|dkr|j|}|dkr|}n|}|j|S)NT)rr.r&r) rrrPrrrrrr/)r/rr0r0r1r,r;r(r(r*max_mag s&      zDecimal.max_magcCst|dd}|dkrt}|js&|jr~|j}|j}|s>|r~|dkrX|dkrX|j|S|dkrr|dkrr|j|S|j||S|jj|j}|dkr|j|}|dkr|}n|}|j|S)NT)rr.r&r) rrrPrrrrrr/)r/rr0r0r1r,r;r(r(r*min_mag s&      zDecimal.min_magcCs|dkrt}|j|d}|r"|S|jdkr2tS|jdkrTtdd|j|jS|j}|jt |j |j |}||kr|S|j tdd|j d|S)N)r0r.r&r>rr)rrrrFr6r@rrHr*r_ignore_all_flagsrrr)r/r0r;new_selfr(r(r* next_minus s"     zDecimal.next_minuscCs|dkrt}|j|d}|r"|S|jdkr2tS|jdkrTtdd|j|jS|j}|jt |j |j |}||kr|S|j tdd|j d|S)N)r0r.r>r&rr)rrrrGr6r@rrHr*rr]rrr)r/r0r;r^r(r(r* next_plus s"     zDecimal.next_pluscCst|dd}|dkrt}|j||}|r.|S|j|}|dkrJ|j|S|dkr^|j|}n |j|}|jr|jt d|j |jt |jt nD|j |jkr|jt|jt|jt |jt |s|jt|S)NT)rr&r.z Infinite result from next_towardr)rrrrr9r`r_rrdrr7r r rr#rr r)r/rr0r;Z comparisonr(r(r* next_toward s4             zDecimal.next_towardcCs|jr dS|jrdS|j}|dkr,dS|d kr8dS|jrN|jrJdSdS|dkr\t}|j|drv|jrrd Sd S|jrd Sd SdS)Nrrr.z +Infinityz -Infinityz-Zeroz+Zero)r0z -Subnormalz +Subnormalz-Normalz+Normalr)rrrrAr7rr@)r/r0infr(r(r* number_class+s, zDecimal.number_classcCstdS)Nr)r)r/r(r(r*radixUsz Decimal.radixcCs|dkrt}t|dd}|j||}|r.|S|jdkrB|jtS|j t|ko^|jknsn|jtS|jr~t |St|}|j }|jt |}|dkrd||}n|dkr|| d}||d|d|}t |j |jdpd|jS)NT)rr&rQ)rrrrOrdr r@rUrrr8rfr6r7rg)r/rr0r;torotrotdigtopadZrotatedr(r(r*rotateYs,    " zDecimal.rotatecCs|dkrt}t|dd}|j||}|r.|S|jdkrB|jtSd|j|j}d|j|j}|t|kox|kns|jtS|j rt |St |j |j |jt|}|j|}|S)NT)rr&r,r)rrrrOrdr rAr@rUrrr6r7r8r)r/rr0r;ZliminfZlimsuprr(r(r*scalebzs"      zDecimal.scalebcCs|dkrt}t|dd}|j||}|r.|S|jdkrB|jtS|j t|ko^|jknsn|jtS|jr~t |St|}|j }|jt |}|dkrd||}n|dkr|| d}|dkr|d|}n|d|}||j d}t |j |jdp d|jS)NT)rr&rQ)rrrrOrdr r@rUrrr8rfr6r7rg)r/rr0r;rerfrgZshiftedr(r(r*rs2    "  z Decimal.shiftcCs|jt|ffS)N) __class__r`)r/r(r(r* __reduce__szDecimal.__reduce__cCst|tkr|S|jt|S)N)typerrjr`)r/r(r(r*__copy__s zDecimal.__copy__cCst|tkr|S|jt|S)N)rlrrjr`)r/memor(r(r* __deepcopy__s zDecimal.__deepcopy__cCsN|dkrt}t||d}|jrXt|j|}t|j}|ddkrL|d7}t|||S|ddkrvddg|j|d<|ddkrt |j|j |j d}|j }|d}|dk r|ddkr|j |d |}nF|dd kr|j| |}n*|dd krt|j |kr|j ||}| rB|j d krB|dd krB|jd |}|j t|j } |ddkr| r||dk r|d |} nd } nB|dd kr| } n.|dd kr|j d kr| dkr| } nd } | d krd} d| |j } nP| t|j kr|j d| t|j } d} n"|j d| p$d} |j | d} | | } t|j| | | |S)N) _localeconvrl%gGr, precisioneEr.zfF%ZgGr&rrQrSi)r_parse_format_specifierrP _format_signr7r`r _format_alignrr6r8rOr?r'rrf_format_number)r/Z specifierr0rpspecr=bodyr?rtrrrvrwrVr(r(r* __format__sZ           "   zDecimal.__format__)rOr8r7rP)rQN)NN)N)N)N)N)N)N)FN)N)N)N)TN)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)NN)N)N)NN)N)NN)NN)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)NN)r2r3r4 __slots__r^ classmethodrqrrrrrrrrrrrrrrr}rrrrrrr__radd__rrr__rmul__rrrrrrrrrrrr __trunc__rpropertyrrrr9rrrrrrrrrdictrrrrrrrrrr"rr%rr'r(r to_integralr.rrrrrr3r4r/r8rrr9rVr<r=r$rr>rr?rr@rArErIrrLrMrOrRrWrYrZrXr[r\r_r`rarcrdrhrirrkrmror|r(r(r(r*r+s  ( !@  2 4    V   7 ;!  $    K     f  > , Un Y   = "   c * "  I   K   2 3          . * !  'FcCs&tjt}||_||_||_||_|S)N)r]r^rr7r8rOrP)r=Z coefficientrZspecialr/r(r(r*r6s  r6c@s$eZdZddZddZddZdS)rNcCs|j|_dS)N)rH new_context)r/rr(r(r*__init__9sz_ContextManager.__init__cCst|_t|j|jS)N)r saved_contextrr)r/r(r(r* __enter__;s z_ContextManager.__enter__cCst|jdS)N)rr)r/tvtbr(r(r*__exit__?sz_ContextManager.__exit__N)r2r3r4rrrr(r(r(r*rN3srNc @seZdZdddZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ e ZdddZddZddZddZdZd d!Zd"d#Zd$d%Zdd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Z d;d<Z!d=d>Z"d?d@Z#dAdBZ$dCdDZ%dEdFZ&dGdHZ'dIdJZ(dKdLZ)dMdNZ*dOdPZ+dQdRZ,dSdTZ-dUdVZ.dWdXZ/dYdZZ0d[d\Z1d]d^Z2d_d`Z3dadbZ4dcddZ5dedfZ6dgdhZ7didjZ8dkdlZ9dmdnZ:dodpZ;dqdrZdwdxZ?dydzZ@d{d|ZAd}d~ZBddZCddZDddZEddZFdddZGddZHddZIddZJddZKddZLddZMddZNddZOddZPddZQddZRddZSddZTddZUeUZVdS)rNc s>yt} Wntk rYnX|dk r*|n| j|_|dk r>|n| j|_|dk rR|n| j|_|dk rf|n| j|_|dk rz|n| j|_|dk r|n| j|_| dkrg|_n| |_dkr| j j |_ n.t t st fddt D|_ n|_ dkr t jt d|_n0t t s4t fddt D|_n|_dS)Nc3s|]}|t|kfVqdS)N)rU)rSr)rr(r* nsz#Context.__init__..r&c3s|]}|t|kfVqdS)N)rU)rSr)rr(r*rus)r NameErrorr@r?r#rArr_ignored_flagsrrHr_rrfromkeysr) r/r@r?r#rArrrrrZdcr()rrr*rUs.   zContext.__init__cCst|tstd||dkr<||krtd||||fnJ|dkrb||krtd||||fn$||ksr||krtd||||ftj|||S)Nz%s must be an integerz-infz%s must be in [%s, %d]. got: %srbz%s must be in [%d, %s]. got: %sz%s must be in [%d, %d]. got %s)r_rUrrrlr] __setattr__)r/namertZvminZvmaxr(r(r*_set_integer_checkys  zContext._set_integer_checkcCsht|tstd|x |D]}|tkrtd|qWx tD]}||kr>td|q>Wtj|||S)Nz%s must be a signal dictz%s is not a valid signal dict)r_rrrrKeyErrorr]r)r/rrkeyr(r(r*_set_signal_dicts    zContext._set_signal_dictcCs|dkr|j||ddS|dkr0|j||ddS|dkrH|j||ddS|dkr`|j||ddS|d krx|j||ddS|d kr|tkrtd |tj|||S|d ks|d kr|j||S|dkrtj|||Std|dS)Nr@r.rbr#z-infr&rArrr?z%s: invalid rounding moderrrz.'decimal.Context' object has no attribute '%s')r_rounding_modesrrr]rrrL)r/rrtr(r(r*rs(  zContext.__setattr__cCstd|dS)Nz%s cannot be deleted)rL)r/rr(r(r* __delattr__szContext.__delattr__c CsNdd|jjD}dd|jjD}|j|j|j|j|j|j|j ||ffS)NcSsg|]\}}|r|qSr(r()rSsigrr(r(r*rUsz&Context.__reduce__..cSsg|]\}}|r|qSr(r()rSrrr(r(r*rUs) ritemsrrjr@r?r#rArr)r/rrr(r(r*rks zContext.__reduce__cCs|g}|jdt|dd|jjD}|jddj|ddd|jjD}|jddj|ddj|d S) NzrContext(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d, clamp=%(clamp)dcSsg|]\}}|r|jqSr()r2)rSrrr(r(r*rUsz$Context.__repr__..zflags=[z, ]cSsg|]\}}|r|jqSr()r2)rSrrr(r(r*rUsztraps=[))rmvarsrrrnr)r/rnamesr(r(r*rs zContext.__repr__cCsx|jD]}d|j|<qWdS)Nr&)r)r/flagr(r(r*rIs zContext.clear_flagscCsx|jD]}d|j|<qWdS)Nr&)r)r/rr(r(r* clear_trapss zContext.clear_trapsc Cs.t|j|j|j|j|j|j|j|j|j }|S)N) rr@r?r#rArrrrr)r/ncr(r(r*r)szContext._shallow_copyc Cs6t|j|j|j|j|j|j|jj|j j|j }|S)N) rr@r?r#rArrrrHrr)r/rr(r(r*rHs z Context.copycGsZtj||}||jkr(|j|f|Sd|j|<|j|sN|j|f|S||dS)Nr.)_condition_mapgetrr1rr)r/Z conditionZ explanationr)errorr(r(r*rds    zContext._raise_errorcCs |jtS)N) _ignore_flagsr)r/r(r(r*r]szContext._ignore_all_flagscGs|jt||_t|S)N)rrj)r/rr(r(r*rszContext._ignore_flagscGs<|rt|dttfr|d}x|D]}|jj|q$WdS)Nr&)r_rkrjrremove)r/rrr(r(r* _regard_flagss zContext._regard_flagscCst|j|jdS)Nr.)rUr#r@)r/r(r(r*rsz Context.EtinycCst|j|jdS)Nr.)rUrAr@)r/r(r(r*r sz Context.EtopcCs|j}||_|S)N)r?)r/rlr?r(r(r*r*szContext._set_roundingrQcCsjt|tr*||jksd|kr*|jtdSt||d}|jr`t|j|j |j kr`|jtdS|j |S)NrRzAtrailing or leading whitespace and underscores are not permitted.)r0zdiagnostic info too long in NaN) r_r`rbrdrrrrfr8r@rr)r/rCrr(r(r*create_decimal#s zContext.create_decimalcCstj|}|j|S)N)rrqr)r/rrr(r(r*create_decimal_from_float4s z!Context.create_decimal_from_floatcCst|dd}|j|dS)NT)r)r0)rr)r/rr(r(r*rhFs z Context.abscCs8t|dd}|j||d}|tkr0td|n|SdS)NT)r)r0zUnable to convert %s to Decimal)rrrrr)r/rrTrr(r(r*add[s  z Context.addcCst|j|S)N)r`r)r/rr(r(r*_applypszContext._applycCst|tstd|jS)Nz,canonical requires a Decimal as an argument.)r_rrrr3)r/rr(r(r*r3ss zContext.canonicalcCst|dd}|j||dS)NT)r)r0)rr)r/rrTr(r(r*rs! zContext.comparecCst|dd}|j||dS)NT)r)r0)rr4)r/rrTr(r(r*r4s zContext.compare_signalcCst|dd}|j|S)NT)r)rr/)r/rrTr(r(r*r/s zContext.compare_totalcCst|dd}|j|S)NT)r)rr8)r/rrTr(r(r*r8s zContext.compare_total_magcCst|dd}|jS)NT)r)rr)r/rr(r(r*rs zContext.copy_abscCst|dd}t|S)NT)r)rr)r/rr(r(r* copy_decimals zContext.copy_decimalcCst|dd}|jS)NT)r)rr)r/rr(r(r*rs zContext.copy_negatecCst|dd}|j|S)NT)r)rr9)r/rrTr(r(r*r9s zContext.copy_signcCs8t|dd}|j||d}|tkr0td|n|SdS)NT)r)r0zUnable to convert %s to Decimal)rrrrr)r/rrTrr(r(r*divide+s  zContext.dividecCs8t|dd}|j||d}|tkr0td|n|SdS)NT)r)r0zUnable to convert %s to Decimal)rrrrr)r/rrTrr(r(r* divide_intPs  zContext.divide_intcCs8t|dd}|j||d}|tkr0td|n|SdS)NT)r)r0zUnable to convert %s to Decimal)rrrrr)r/rrTrr(r(r*rgs  zContext.divmodcCst|dd}|j|dS)NT)r)r0)rrV)r/rr(r(r*rV|s z Context.expcCst|dd}|j|||dS)NT)r)r0)rr)r/rrTr,r(r(r*rs z Context.fmacCst|tstd|jS)Nz/is_canonical requires a Decimal as an argument.)r_rrrr<)r/rr(r(r*r<s zContext.is_canonicalcCst|dd}|jS)NT)r)rr=)r/rr(r(r*r=s zContext.is_finitecCst|dd}|jS)NT)r)rr$)r/rr(r(r*r$s zContext.is_infinitecCst|dd}|jS)NT)r)rr)r/rr(r(r*rs zContext.is_nancCst|dd}|j|dS)NT)r)r0)rr>)r/rr(r(r*r>s zContext.is_normalcCst|dd}|jS)NT)r)rr)r/rr(r(r*rs zContext.is_qnancCst|dd}|jS)NT)r)rr?)r/rr(r(r*r?s zContext.is_signedcCst|dd}|jS)NT)r)rr)r/rr(r(r*r$s zContext.is_snancCst|dd}|j|dS)NT)r)r0)rr@)r/rr(r(r*r@4s zContext.is_subnormalcCst|dd}|jS)NT)r)rrA)r/rr(r(r*rAJs zContext.is_zerocCst|dd}|j|dS)NT)r)r0)rrI)r/rr(r(r*rI[s z Context.lncCst|dd}|j|dS)NT)r)r0)rrL)r/rr(r(r*rLqs z Context.log10cCst|dd}|j|dS)NT)r)r0)rrM)r/rr(r(r*rMs z Context.logbcCst|dd}|j||dS)NT)r)r0)rrW)r/rrTr(r(r*rWs zContext.logical_andcCst|dd}|j|dS)NT)r)r0)rrY)r/rr(r(r*rYs zContext.logical_invertcCst|dd}|j||dS)NT)r)r0)rrZ)r/rrTr(r(r*rZs zContext.logical_orcCst|dd}|j||dS)NT)r)r0)rrX)r/rrTr(r(r*rXs zContext.logical_xorcCst|dd}|j||dS)NT)r)r0)rr)r/rrTr(r(r*r s z Context.maxcCst|dd}|j||dS)NT)r)r0)rr[)r/rrTr(r(r*r[&s zContext.max_magcCst|dd}|j||dS)NT)r)r0)rr)r/rrTr(r(r*r7s z Context.mincCst|dd}|j||dS)NT)r)r0)rr\)r/rrTr(r(r*r\Rs zContext.min_magcCst|dd}|j|dS)NT)r)r0)rr)r/rr(r(r*minuscs z Context.minuscCs8t|dd}|j||d}|tkr0td|n|SdS)NT)r)r0zUnable to convert %s to Decimal)rrrrr)r/rrTrr(r(r*multiplyts  zContext.multiplycCst|dd}|j|dS)NT)r)r0)rr_)r/rr(r(r*r_s zContext.next_minuscCst|dd}|j|dS)NT)r)r0)rr`)r/rr(r(r*r`s zContext.next_pluscCst|dd}|j||dS)NT)r)r0)rra)r/rrTr(r(r*ras zContext.next_towardcCst|dd}|j|dS)NT)r)r0)rr")r/rr(r(r*r"s zContext.normalizecCst|dd}|j|dS)NT)r)r0)rrc)r/rr(r(r*rcs/ zContext.number_classcCst|dd}|j|dS)NT)r)r0)rr)r/rr(r(r*plus)s z Context.pluscCs:t|dd}|j|||d}|tkr2td|n|SdS)NT)r)r0zUnable to convert %s to Decimal)rrrrr)r/rrTrrr(r(r*power:s I z Context.powercCst|dd}|j||dS)NT)r)r0)rr)r/rrTr(r(r*rs7 zContext.quantizecCstdS)Nr)r)r/r(r(r*rdsz Context.radixcCs8t|dd}|j||d}|tkr0td|n|SdS)NT)r)r0zUnable to convert %s to Decimal)rrrrr)r/rrTrr(r(r*rs  zContext.remaindercCst|dd}|j||dS)NT)r)r0)rr)r/rrTr(r(r*rs zContext.remainder_nearcCst|dd}|j||dS)NT)r)r0)rrh)r/rrTr(r(r*rhs zContext.rotatecCst|dd}|j|S)NT)r)rr%)r/rrTr(r(r*r%1s zContext.same_quantumcCst|dd}|j||dS)NT)r)r0)rri)r/rrTr(r(r*riIs zContext.scalebcCst|dd}|j||dS)NT)r)r0)rr)r/rrTr(r(r*r\s z Context.shiftcCst|dd}|j|dS)NT)r)r0)rr.)r/rr(r(r*r.zs z Context.sqrtcCs8t|dd}|j||d}|tkr0td|n|SdS)NT)r)r0zUnable to convert %s to Decimal)rrrrr)r/rrTrr(r(r*subtracts  zContext.subtractcCst|dd}|j|dS)NT)r)r0)rr)r/rr(r(r*rs zContext.to_eng_stringcCst|dd}|j|dS)NT)r)r0)rr)r/rr(r(r* to_sci_strings zContext.to_sci_stringcCst|dd}|j|dS)NT)r)r0)rr()r/rr(r(r*r(s zContext.to_integral_exactcCst|dd}|j|dS)NT)r)r0)rr)r/rr(r(r*rs zContext.to_integral_value) NNNNNNNNN)N)rQ)N)Wr2r3r4rrrrrrkrrIrr)rHrmrdr]rrrrrr*rrrhrrr3rr4r/r8rrrr9rrrrVrr<r=r$rr>rr?rr@rArIrLrMrWrYrZrXrr[rr\rrr_r`rar"rcrrrrdrrrhr%rirr.rrrr(rrr(r(r(r*rBs "     $#   %  #2 P:&" c@s&eZdZd Zd ddZddZeZdS) rir=rUrVNcCsf|dkrd|_d|_d|_nFt|trD|j|_t|j|_|j|_n|d|_|d|_|d|_dS)Nr&r.r,)r=rUrVr_rr7r8rO)r/rtr(r(r*rs     z_WorkRep.__init__cCsd|j|j|jfS)Nz (%r, %r, %r))r=rUrV)r/r(r(r*r(sz_WorkRep.__repr__)r=rUrV)N)r2r3r4r}rrrr(r(r(r*ris ricCs|j|jkr|}|}n|}|}tt|j}tt|j}|jtd||d}||jd|krpd|_||_|jd|j|j9_|j|_||fS)Nr.r,rr)rVrfr`rUr)rrr@ZtmprZtmp_lenZ other_lenrVr(r(r*r/s rcCsb|dkr dS|dkr |d|Stt|}t|t|jd}|| krPdS|d| SdS)Nr&rrQ)r`rhrfrstrip)r5rZstr_nZval_nr(r(r*r Os   r cCsF|dks|dkrtdd}x$||kr@||| |d?}}qW|S)Nr&z3Both arguments to _sqrt_nearest should be positive.r.)rl)r5rrTr(r(r* _sqrt_nearestds  rcCs2d|>||?}}|d||d@|d@|kS)Nr.r,r()r rrTrr(r(r*_rshift_nearestssrcCs&t||\}}|d||d@|kS)Nr,r.)r)rrTrrr(r(r* _div_nearest{srrc Cs||}d}xn||kr*t|||>|ksF||krzt|||?|krzt||d>|t||t|||}|d7}qWtdtt|d| }t||}t||}x0t|dddD]}t||t|||}qWt|||S)Nr&r.rr[ir)rhrrrrUrfr`r) r MLrRTZyshiftwrr(r(r*_ilogs    rc Cs|d7}tt|}||||dk}|dkrd|}|||}|dkrZ|d|9}nt|d| }t||}t|}t|||}||} nd}t|d| } t| |dS)Nr,r.r&rr)rfr`rr _log10_digits) r,rr r-rrrlog_dZlog_10Z log_tenpowerr(r(r*rKs     rKc Cs|d7}tt|}||||dk}|dkrr|||}|dkrR|d|9}nt|d| }t|d|}nd}|rttt|d}||dkrt|t||d|}qd}nd}t||dS)Nr,r.r&rr)rfr`rrrhr) r,rr r-rrrrZ f_log_tenr(r(r*rHs"   rHc@seZdZddZddZdS) _Log10MemoizecCs d|_dS)NZ/23025850929940456840179914546843642076011014886)rx)r/r(r(r*rsz_Log10Memoize.__init__cCs|dkrtd|t|jkrd}xLd||d}tttd||d}|| dd|krdP|d7}q$W|jddd |_t|jd|dS) Nr&zp should be nonnegativer[rr,rrQr.r)rlrfrxr`rrrrU)r/r rrrxr(r(r* getdigitss  z_Log10Memoize.getdigitsN)r2r3r4rrr(r(r(r*rsrc Cst||>|}tdtt|d| }t||}||>}x.t|dddD]}t|||||}qRWx6t|ddd D]"}||d>}t||||}qW||S) Nrr[r.r&r,irrr)r rUrfr`rr) r rrrrrZMshiftrrr(r(r*_iexp&s  rc Cs|d7}td|tt|d}||}||}|dkrH|d|}n|d| }t|t|\}}t|d|}tt|d|d||dfS)Nr,r&r.rir[)rrfr`rrrr) r,rr rrrZcshiftZquotrr(r(r*r:Ksr:c Csttt||}t||||d}||}|dkrJ||d|}nt||d| }|dkrtt||dk|dkkrd|ddd|} } qd|d| } } n,t||d |d\} } t| d} | d7} | | fS)Nr.r&r)rfr`rhrHrr:) rrrrr rTZlxcrZpcrrVr(r(r*ros rrF5(rBrrz) r2345678r>cCs0|dkrtdt|}dt|||dS)Nr&z0The argument to _log10_lb should be nonnegative.r)rlr`rf)r,Z correctionZstr_cr(r(r*r sr cCsLt|tr|St|tr t|S|r8t|tr8tj|S|rHtd|tS)NzUnable to convert %s to Decimal)r_rrUrprqrrr)rrZ allow_floatr(r(r*rs    rcCst|tr||fSt|tjrR|jsDt|jtt|j |j |j }|t|j fS|rrt|tj rr|jdkrr|j}t|trt}|rd|jt<n |jtd|tj|fSttfS)Nr&r.z;strict semantics for mixing floats and Decimals are enabled)r_r_numbersZRationalrPr6r7r`rUr8 denominatorrO numeratorZComplexrrrprrrrdrqr)r/rrr0r(r(r*rs$    rri?B)r@r?rrrAr#rrr\)r@r?rra # 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[-+ ])? (?P\#)? (?P0)? (?P(?!0)\d+)? (?P,)? (?:\.(?P0|(?!0)\d+))? (?P[eEfFgGn%])? \Z cCstj|}|dkrtd||j}|d}|d}|ddk |d<|drv|dk rbtd||dk rvtd||p|d|d<|pd|d<|d dkrd |d <t|d pd |d <|d dk rt|d |d <|d dkr|ddks|ddkrd|d <|ddkrfd|d<|dkr&tj}|ddk r@td||d|d<|d|d<|d|d<n*|ddkr|d|d<ddg|d<d|d<|S)NzInvalid format specifier: fillalignzeropadz7Fill character conflicts with '0' in format specifier: z2Alignment conflicts with '0' in format specifier:  >r=rT minimumwidthrQrtr&rlZgGnr.r5rr thousands_sepzJExplicit thousands separator conflicts with 'n' type in format specifier: grouping decimal_pointrSr[r)_parse_format_specifier_regexmatchrl groupdictrU_locale localeconv) format_specrpruZ format_dictrrr(r(r*rvDsN           rvc Cs|d}|d}||t|t|}|d}|dkrF|||}nj|dkr\|||}nT|dkrr|||}n>|dkrt|d}|d|||||d}ntd |S) Nrrrus~           &         .  ^  0",# %$+   *      P % )