grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026î £ fP}ã+@sfdZddddddddd d d d d ddddddddddddddddddd d!d"d#d$g$Zd%Zd&Zd'd(lZd'd(lZd'd(lZy#d'd)l m Z e dd*ƒZ Wne k rëd+d,„Z YnXdZdZdZdZdZdZdZdZd-Zejd.d/d0krOd1Zd1Zd1 Znd2Zd2Zd2 Zeed0ZGd3d„deƒZGd4d„deƒZGd5d „d eƒZGd6d„deƒZ Gd7d „d ee!ƒZ"Gd8d„deƒZ#Gd9d„dee!ƒZ$Gd:d „d eƒZ%Gd;d„deƒZ&Gd<d „d eƒZ'Gd=d „d eƒZ(Gd>d„de%e'ƒZ)Gd?d„de%e'e(ƒZ*Gd@d„dee+ƒZ,ee"e%e)e'e*ee(e,g Z-iee 6ee#6ee$6ee&6Z.eeeeeeeefZ/yd'd(l0Z0Wn4e k r]GdAdB„dBe1ƒZ2e2ƒZ0[2YnXy e0j3WnNe4k r¹e5e0j6ƒdCƒr�e0j6ƒ`7ndDd„Z8dEd„Z9YnIXe0j3ƒZ3e5e3dCƒrÞe3`7ne3dFd„Z9e3dGd„Z8[0[3d(dHd„Z:GdId„de1ƒZ;dJdKdL„Z<ej=j>e;ƒGdMdN„dNe1ƒZ?GdOd„de1ƒZ@GdPdQ„dQe1ƒZAd'dRdS„ZBeCjDZEdTdU„ZFdVdW„ZGdXdY„ZHdZd[„ZId\d]d^„ZJd_d`„ZKdadb„ZLGdcdd„dde1ƒZMeMƒjNZOd\dedf„ZPdgdh„ZQdidj„ZRi dkdl6dmdn6dodp6dqdr6dsdt6dudv6dwdx6dydz6d{d|6d}d~„ZSdJdJdd€„ZTdJd�d‚„ZUe@dƒd„d…ed†e"e)egd‡gdˆd‰dŠd‰ d‹d0dŒd'ƒZVe@dƒd�d…ed†e"e)eee*gd‡gƒZWe@dƒd�d…ed†gd‡gƒZXd'd(lYZYeYjZdŽeYj[eYj\Bƒj]Z^eYjZd�ƒj]Z_eYjZd�ƒj]Z`eYjZd‘eYj[eYjaBƒZb[Yyd'd(lcZdWne k rÕYnXd(d’d“„Zed”d•„Zfd–d—„Zgd0d˜d™„Zhdšd›„Zidœd�„Zje;džƒZke;dŸƒZle;d ƒZme;d'ƒZne;d0ƒZoe;d0 ƒZpekelfZqejrjsZtejrjuZvejrjwZxeydyetd.etƒZz[yd'd(l{Z{Wne k rÚYnTXe|e}ƒƒZ~e|e}e{ƒƒZxe~eD]Z€e�ƒe€=qW[~[[€d'd¡l{Te‚d¢krbd'd(lƒZƒd'd(l„Z„eƒj…e„ƒnd(S)£a¿ This is an implementation of decimal floating point arithmetic based on the General Decimal Arithmetic Specification: http://speleotrove.com/decimal/decarith.html and IEEE standard 854-1987: http://en.wikipedia.org/wiki/IEEE_854-1987 Decimal floating point has finite precision with arbitrarily large bounds. The purpose of this module is to support arithmetic using familiar "schoolhouse" rules and to avoid some of the 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 0.0; Decimal('1.00') % Decimal('0.1') returns the expected Decimal('0.00')). 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.45e12345678') Decimal('1.2345E+12345680') >>> 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): ... ... ... decimal.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): ... ... ... decimal.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 >>> ÚDecimalÚContextÚ DecimalTupleÚDefaultContextÚ BasicContextÚExtendedContextÚDecimalExceptionÚClampedÚInvalidOperationÚDivisionByZeroÚInexactÚRoundedÚ SubnormalÚOverflowÚ UnderflowÚFloatOperationÚDivisionImpossibleÚInvalidContextÚConversionSyntaxÚDivisionUndefinedÚ ROUND_DOWNÚ ROUND_HALF_UPÚROUND_HALF_EVENÚ ROUND_CEILINGÚ ROUND_FLOORÚROUND_UPÚROUND_HALF_DOWNÚ ROUND_05UPÚ setcontextÚ getcontextÚ localcontextÚMAX_PRECÚMAX_EMAXÚMIN_EMINÚ MIN_ETINYÚ HAVE_THREADSz1.70z2.4.1éN)Ú namedtuplezsign digits exponentcGs|S)N©)Úargsr'r'ú,/opt/alt/python34/lib64/python3.4/decimal.pyÚŸsr*Téé?élÿÇNÎZoi@üTc@s"eZdZdZdd„ZdS)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. cGsdS)Nr')ÚselfÚcontextr(r'r'r)ÚhandleÍszDecimalException.handleN)Ú__name__Ú __module__Ú __qualname__Ú__doc__r0r'r'r'r)rºs c@seZdZdZdS)ra)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"). N)r1r2r3r4r'r'r'r)rÑs c@s"eZdZdZdd„ZdS)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. cGs:|r6t|dj|djddƒ}|j|ƒStS)Nr%ÚnT)Ú_dec_from_tripleÚ_signÚ_intÚ_fix_nanÚ_NaN)r.r/r(Úansr'r'r)r0ôs# zInvalidOperation.handleN)r1r2r3r4r0r'r'r'r)r Ýs c@s"eZdZdZdd„ZdS)rzÝ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]. cGstS)N)r:)r.r/r(r'r'r)r0szConversionSyntax.handleN)r1r2r3r4r0r'r'r'r)rús c@s"eZdZdZdd„ZdS)r a²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. cGst|S)N)Ú_SignedInfinity)r.r/Úsignr(r'r'r)r0szDivisionByZero.handleN)r1r2r3r4r0r'r'r'r)r s c@s"eZdZdZdd„ZdS)rzó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]. cGstS)N)r:)r.r/r(r'r'r)r0szDivisionImpossible.handleN)r1r2r3r4r0r'r'r'r)rs c@s"eZdZdZdd„ZdS)rzî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]. cGstS)N)r:)r.r/r(r'r'r)r0'szDivisionUndefined.handleN)r1r2r3r4r0r'r'r'r)rs c@seZdZdZdS)r a­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. N)r1r2r3r4r'r'r'r)r *s c@s"eZdZdZdd„ZdS)raì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:)r.r/r(r'r'r)r0AszInvalidContext.handleN)r1r2r3r4r0r'r'r'r)r6s c@seZdZdZdS)r aÙ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. N)r1r2r3r4r'r'r'r)r Ds c@seZdZdZdS)r a˜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. N)r1r2r3r4r'r'r'r)r Ps c@s"eZdZdZdd„ZdS)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. cGs·|jttttfkr#t|S|dkrk|jtkrFt|St|d|j|j |jdƒS|dkr³|jt krŽt|St|d|j|j |jdƒSdS)Nr%Ú9r-) Úroundingrrrrr<rr6ÚprecÚEmaxr)r.r/r=r(r'r'r)r0qs   zOverflow.handleN)r1r2r3r4r0r'r'r'r)r[s c@seZdZdZdS)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. N)r1r2r3r4r'r'r'r)r�s c@seZdZdZdS)raœEnable 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. N)r1r2r3r4r'r'r'r)r�s c@seZdZedd„ZdS)Ú MockThreadingcCs |jtS)N)Úmodulesr1)r.Úsysr'r'r)ÚlocalºszMockThreading.localN)r1r2r3rDrEr'r'r'r)rB¹s rBÚ__decimal_context__cCsA|tttfkr.|jƒ}|jƒn|tjƒ_dS)z%Set this thread's context to context.N)rrrÚcopyÚ clear_flagsÚ threadingÚcurrent_threadrF)r/r'r'r)rÉs  c CsFytjƒjSWn.tk rAtƒ}|tjƒ_|SYnXdS)zÍ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)rIrJrFÚAttributeErrorr)r/r'r'r)rÐs   c Cs:y |jSWn(tk r5tƒ}||_|SYnXdS)zÍ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)rFrKr)Ú_localr/r'r'r)räs     cCs;|tttfkr.|jƒ}|jƒn||_dS)z%Set this thread's context to context.N)rrrrGrHrF)r/rLr'r'r)ròs  cCs"|dkrtƒ}nt|ƒ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 N)rÚ_ContextManager)Zctxr'r'r)rûs$ c@såeZdZdZdüZdddd „Zed d „ƒZd d „Zdd„Z dddd„Z dd„Z dd„Z dd„Z ddd„Zddd„Zddd„Zddd„Zdd d!„Zdd"d#„Zdd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,dd-d.„Zdd/d0„Zdd1d2„Zdd3d4„Zd5dd6d7„Zdd8d9„ZeZdd:d;„Zdd<d=„Z dd>d?„Z!e!Z"dd@dA„Z#dBdC„Z$ddDdE„Z%ddFdG„Z&ddHdI„Z'ddJdK„Z(ddLdM„Z)ddNdO„Z*ddPdQ„Z+ddRdS„Z,dTdU„Z-dVdW„Z.e.Z/dXdY„Z0e1e0ƒZ0dZd[„Z2e1e2ƒZ2d\d]„Z3d^d_„Z4d`da„Z5dbdc„Z6ddde„Z7dfdg„Z8dhdi„Z9djdk„Z:dldm„Z;dndo„Z<dpdq„Z=drds„Z>e?dte7due8dve9dwe:dxe;dye<dze=d{e>ƒZ@dd|d}„ZAd~d„ZBd€d�„ZCdd‚dƒ„ZDdd„d…„ZEd†d‡„ZFdddˆd‰„ZGddŠd‹„ZHddŒd�„ZIddd5dŽd�„ZJdd�d‘„ZKd’d“„ZLd”d•„ZMddd–d—„ZNddd˜d™„ZOeOZPddšd›„ZQddœd�„ZRddždŸ„ZSd d¡„ZTd¢d£„ZUd¤d¥„ZVd¦d§„ZWdd¨d©„ZXddªd«„ZYdd¬d­„ZZd®d¯„Z[d°d±„Z\dd²d³„Z]dd´dµ„Z^d¶d·„Z_d¸d¹„Z`dºd»„Zad¼d½„Zbdd¾d¿„ZcdÀdÁ„ZddÂdÄZedÄdÅ„ZfddÆdÇ„ZgdÈdÉ„ZhdÊdË„ZiddÌdÍ„ZjdÎdÏ„ZkddÐdÑ„ZlddÒdÓ„ZmdÔdÕ„ZndÖdׄZoddØdÙ„ZpddÚdÛ„ZqddÜdÝ„ZrddÞdß„Zsddàdá„Ztddâdã„Zuddädå„Zvddædç„Zwddèdé„Zxddêdë„Zydìdí„Zzddîdï„Z{ddðdñ„Z|ddòdó„Z}dôdõ„Z~död÷„Zdødù„Z€dddúdû„Z�dS)ýrz,Floating point class for decimal arithmetic.Ú_expr8r7Ú _is_specialÚ0Nc CsÔtj|ƒ}t|tƒršt|jƒƒ}|dkrh|dkrTtƒ}n|jtd|ƒS|j dƒdkr‰d|_ n d|_ |j dƒ}|dk r|j dƒp¿d }t |j d ƒp×d ƒ}tt ||ƒƒ|_ |t |ƒ|_d |_n�|j d ƒ}|dk r{tt |p?d ƒƒjd ƒ|_ |j dƒrod|_q�d|_nd |_ d|_d|_|St|t ƒrõ|dkrÁd|_ n d|_ d|_tt|ƒƒ|_ d |_|St|tƒr8|j|_|j |_ |j |_ |j|_|St|tƒr„|j|_ t|j ƒ|_ t |jƒ|_d |_|St|ttfƒrFt |ƒdkrºtdƒ‚nt|dt ƒoÚ|ddksìtdƒ‚n|d|_ |ddkr+d |_ |d|_d|_ng} xn|dD]b} t| t ƒr’d| kohdknr’| s| dkrž| j| ƒqžq<tdƒ‚q<W|ddkræd jtt| ƒƒ|_ |d|_d|_n\t|dt ƒr6d jtt| pdgƒƒ|_ |d|_d |_n tdƒ‚|St|tƒrÀ|dkrmtƒ}n|jtdƒtj|ƒ}|j|_|j |_ |j |_ |j|_|Std|ƒ‚dS)aêCreate a decimal point instance. >>> 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') NzInvalid literal for Decimal: %rr=ú-r-r%ÚintZfracÚÚexprPFÚdiagÚsignalÚNr5ÚFTéztInvalid tuple size in creation of Decimal from list or tuple. The list or tuple should have exactly three elements.z|Invalid sign. The first value in the tuple should be an integer; either 0 for a positive number or 1 for a negative number.r+é zTThe second value in the tuple must be composed of integers in the range 0 through 9.zUThe third value in the tuple must be an integer, or one of the strings 'F', 'n', 'N'.z;strict semantics for mixing floats and Decimals are enabledzCannot convert %r to Decimal)r%r-)r5rW) ÚobjectÚ__new__Ú isinstanceÚstrÚ_parserÚstriprÚ _raise_errorrÚgroupr7rRr8ÚlenrNrOÚlstripÚabsrÚ_WorkRepr=rTÚlistÚtupleÚ ValueErrorÚappendÚjoinÚmapÚfloatrÚ from_floatÚ TypeError) ÚclsÚvaluer/r.ÚmÚintpartÚfracpartrTrUÚdigitsÚdigitr'r'r)r\2s¬          $                #    +  $          zDecimal.__new__cCsót|tƒr||ƒSt|tƒs7tdƒ‚ntj|ƒsUtj|ƒre|t|ƒƒStjd|ƒdkr†d}nd}t |ƒj ƒ\}}|j ƒd}t |t |d|ƒ| ƒ}|tkrå|S||ƒSdS)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') zargument must be int or float.gð?r%r-éN)r]rRrmroÚ_mathZisinfZisnanÚreprZcopysignreÚas_integer_ratioÚ bit_lengthr6r^r)rpÚfr=r5ÚdÚkÚresultr'r'r)rnÈs  ! zDecimal.from_floatcCs9|jr5|j}|dkr"dS|dkr5dSndS)zrReturns whether the number is not actually one. 0 if a number 1 if NaN 2 if sNaN r5r-rWr+r%)rOrN)r.rTr'r'r)Ú_isnanðs    zDecimal._isnancCs$|jdkr |jrdSdSdS)zyReturns whether the number is infinite 0 if finite or not a number 1 if +INF -1 if -INF rXr-r%éÿÿÿÿ)rNr7)r.r'r'r)Ú _isinfinityÿs  zDecimal._isinfinitycCs³|jƒ}|dkr!d}n |jƒ}|s9|r¯|dkrQtƒ}n|dkrp|jtd|ƒS|dkr�|jtd|ƒS|r¢|j|ƒS|j|ƒSdS)z½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. NFr+ÚsNaNr%)r€rrar r9)r.Úotherr/Ú self_is_nanÚ other_is_nanr'r'r)Ú _check_nans s"             zDecimal._check_nanscCs­|dkrtƒ}n|js*|jr©|jƒrI|jtd|ƒS|jƒrh|jtd|ƒS|jƒr‡|jtd|ƒS|jƒr©|jtd|ƒSndS)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. Nzcomparison involving sNaNzcomparison involving NaNr%)rrOÚis_snanrar Úis_qnan)r.r„r/r'r'r)Ú_compare_check_nans,s(           zDecimal._compare_check_nanscCs|jp|jdkS)zuReturn True if self is nonzero; otherwise return False. NaNs and infinities are considered nonzero. rP)rOr8)r.r'r'r)Ú__bool__MszDecimal.__bool__cCsd|js|jrQ|jƒ}|jƒ}||kr:dS||krJdSdSn|sp|sadSd|j Sn|s�d|jS|j|jkr—dS|j|jkr­dS|jƒ}|jƒ}||kr=|jd|j|j}|jd|j|j}||krdS||kr/d |j Sd |jSn#||krTd |jSd |j SdS) z¸Compare 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%r-rPNr�r�r�r�r�r�r�r�)rOr‚r7Úadjustedr8rN)r.r„Zself_infZ other_infÚ self_adjustedZother_adjustedÚ self_paddedZ other_paddedr'r'r)Ú_cmpTs>             z Decimal._cmpcCsTt||ddƒ\}}|tkr+|S|j||ƒrAdS|j|ƒdkS)NÚ equality_opTFr%)Ú_convert_for_comparisonÚNotImplementedr‡r�)r.r„r/r'r'r)Ú__eq__”s  zDecimal.__eq__cCsTt||ddƒ\}}|tkr+|S|j||ƒrAdS|j|ƒdkS)Nr�Tr%)r‘r’r‡r�)r.r„r/r'r'r)Ú__ne__œs  zDecimal.__ne__cCsTt||ƒ\}}|tkr%|S|j||ƒ}|rAdS|j|ƒdkS)NFr%)r‘r’rŠr�)r.r„r/r;r'r'r)Ú__lt__¥s zDecimal.__lt__cCsTt||ƒ\}}|tkr%|S|j||ƒ}|rAdS|j|ƒdkS)NFr%)r‘r’rŠr�)r.r„r/r;r'r'r)Ú__le__®s zDecimal.__le__cCsTt||ƒ\}}|tkr%|S|j||ƒ}|rAdS|j|ƒdkS)NFr%)r‘r’rŠr�)r.r„r/r;r'r'r)Ú__gt__·s zDecimal.__gt__cCsTt||ƒ\}}|tkr%|S|j||ƒ}|rAdS|j|ƒdkS)NFr%)r‘r’rŠr�)r.r„r/r;r'r'r)Ú__ge__Às zDecimal.__ge__cCs\t|ddƒ}|js*|rI|jrI|j||ƒ}|rI|Snt|j|ƒƒS)zàCompare 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') ÚraiseitT)Ú_convert_otherrOr‡rr�)r.r„r/r;r'r'r)ÚcompareÉs zDecimal.comparecCsÊ|jrI|jƒr$tdƒ‚qI|jƒr4tS|jrBt StSn|jdkrptd|jt ƒ}ntt |j t ƒ}t |j ƒ|t }|dkr¯|n| }|dkrÆdS|S)zx.__hash__() <==> hash(x)z"Cannot hash a signaling NaN value.r%é r-r+r�éþÿÿÿ) rOrˆroÚis_nanÚ _PyHASH_NANr7Ú _PyHASH_INFrNÚpowÚ_PyHASH_MODULUSÚ _PyHASH_10INVrRr8)r.Zexp_hashZhash_r;r'r'r)Ú__hash__Ûs    zDecimal.__hash__cCs(t|jttt|jƒƒ|jƒS)zeRepresents the number as a triple tuple. To show the internals exactly as they are. )rr7rhrlrRr8rN)r.r'r'r)Úas_tupleõszDecimal.as_tuplecCsdt|ƒS)z0Represents the number as an instance of Decimal.z Decimal('%s'))r^)r.r'r'r)Ú__repr__üszDecimal.__repr__Fc CsÏddg|j}|jrc|jdkr3|dS|jdkrQ|d|jS|d|jSn|jt|jƒ}|jdkr�|dkr�|}nE|s¬d }n6|jd krÐ|d d d }n|d d d }|dkr d }d d | |j}nf|t|jƒkrI|jd |t|jƒ}d}n*|jd|…}d |j|d…}||krˆd}n7|dkr tƒ}nddg|jd||}||||S)z–Return string representation of the number in scientific notation. Captures all of the information in the underlying representation. rSrQrXZInfinityr5ÚNaNrƒr%ér-rPrYÚ.NÚeÚEz%+diúÿÿÿ)r7rOrNr8rcrÚcapitals) r.Úengr/r=Ú leftdigitsÚdotplacersrtrTr'r'r)Ú__str__s:         zDecimal.__str__cCs|jddd|ƒS)aConvert 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­Tr/)r°)r.r/r'r'r)Ú to_eng_string5szDecimal.to_eng_stringcCs~|jr(|jd|ƒ}|r(|Sn|dkr@tƒ}n| re|jtkre|jƒ}n |jƒ}|j|ƒS)zRReturns a copy with the sign switched. Rounds, if it has reason. r/N)rOr‡rr?rÚcopy_absÚ copy_negateÚ_fix)r.r/r;r'r'r)Ú__neg__?s    zDecimal.__neg__cCs~|jr(|jd|ƒ}|r(|Sn|dkr@tƒ}n| re|jtkre|jƒ}n t|ƒ}|j|ƒS)zhReturns a copy, unless it is a sNaN. Rounds the number (if more then precision digits) r/N)rOr‡rr?rr²rr´)r.r/r;r'r'r)Ú__pos__Us    zDecimal.__pos__TcCsl|s|jƒS|jr8|jd|ƒ}|r8|Sn|jrV|jd|ƒ}n|jd|ƒ}|S)zÉReturns 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(). r/)r²rOr‡r7rµr¶)r.Úroundr/r;r'r'r)Ú__abs__js   zDecimal.__abs__c Csqt|ƒ}|tkr|S|dkr4tƒ}n|jsF|jr¿|j||ƒ}|rb|S|jƒr¦|j|jkrœ|jƒrœ|jtdƒSt |ƒS|jƒr¿t |ƒSnt |j |j ƒ}d}|j t kr|j|jkrd}n| r[| r[t |j|jƒ}|r6d}nt|d|ƒ}|j|ƒ}|S|s¦t||j |jdƒ}|j||j ƒ}|j|ƒ}|S|sñt||j |jdƒ}|j||j ƒ}|j|ƒ}|St|ƒ}t|ƒ}t|||jƒ\}}tƒ} |j|jkrØ|j|jkrvt|d|ƒ}|j|ƒ}|S|j|jkr˜||}}n|jdkrÌd| _|j|j|_|_qd| _n6|jdkrd| _d\|_|_n d| _|jdkr3|j|j| _n|j|j| _|j| _t | ƒ}|j|ƒ}|S)zbReturns self + other. -INF + INF (or the reverse) cause InvalidOperation errors. Nz -INF + INFr%r-rP)r%r%)ršr’rrOr‡r‚r7rar rÚminrNr?rr6r´Úmaxr@Ú_rescalerfÚ _normalizer=rRrT) r.r„r/r;rTZ negativezeror=Úop1Úop2rr'r'r)Ú__add__€s|        !           zDecimal.__add__cCsit|ƒ}|tkr|S|js.|jrP|j|d|ƒ}|rP|Sn|j|jƒd|ƒS)zReturn self - otherr/)ršr’rOr‡r¿r³)r.r„r/r;r'r'r)Ú__sub__Øs  zDecimal.__sub__cCs/t|ƒ}|tkr|S|j|d|ƒS)zReturn other - selfr/)ršr’rÀ)r.r„r/r'r'r)Ú__rsub__æs  zDecimal.__rsub__cCsÇt|ƒ}|tkr|S|dkr4tƒ}n|j|jA}|jsV|jrÉ|j||ƒ}|rr|S|jƒrœ|s”|jtdƒSt |S|jƒrÉ|s¾|jtdƒSt |Sn|j |j }| sç| r t |d|ƒ}|j |ƒ}|S|j dkrCt ||j |ƒ}|j |ƒ}|S|j dkrzt ||j |ƒ}|j |ƒ}|St|ƒ}t|ƒ}t |t|j|jƒ|ƒ}|j |ƒ}|S)z\Return self * other. (+-) INF * 0 (or its reverse) raise InvalidOperation. Nz (+-)INF * 0z 0 * (+-)INFrPÚ1)ršr’rr7rOr‡r‚rar r<rNr6r´r8rfr^rR)r.r„r/Z resultsignr;Z resultexpr½r¾r'r'r)Ú__mul__îsH         "zDecimal.__mul__c Cslt|ƒ}|tkrtS|dkr4tƒ}n|j|jA}|jsV|jrã|j||ƒ}|rr|S|jƒrš|jƒrš|jtdƒS|jƒr®t |S|jƒrã|jt dƒt |d|j ƒƒSn|s|sÿ|jt dƒS|jtd|ƒS|s1|j|j}d}nt|jƒt|jƒ|jd}|j|j|}t|ƒ}t|ƒ} |dkr¶t|jd || jƒ\}} n$t|j| jd | ƒ\}} | r|d dkrG|d7}qGnG|j|j} x4|| krF|d dkrF|d }|d7}qWt |t|ƒ|ƒ}|j|ƒS) zReturn self / other.Nz(+-)INF/(+-)INFzDivision by infinityrPz0 / 0zx / 0r%r-rœrw)ršr’rr7rOr‡r‚rar r<rr6ÚEtinyrr rNrcr8r@rfÚdivmodrRr^r´) r.r„r/r=r;rTÚcoeffÚshiftr½r¾Ú remainderÚ ideal_expr'r'r)Ú __truediv__'sP       '   &$ zDecimal.__truediv__c Cs‹|j|jA}|jƒr(|j}nt|j|jƒ}|jƒ|jƒ}| sr|jƒsr|dkr—t|ddƒ|j||jƒfS||jkrot |ƒ}t |ƒ}|j |j krð|j d|j |j 9_ n|j d|j |j 9_ t |j |j ƒ\}} |d|jkrot|t |ƒdƒt|jt | ƒ|ƒfSn|jtdƒ} | | fS)z½Return (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. r+rPr%rœz%quotient too large in //, % or divmodr�)r7r‚rNr¹rŒr6r»r?r@rfrTrRrÅr^rar) r.r„r/r=rÉÚexpdiffr½r¾ÚqÚrr;r'r'r)Ú_dividebs*       zDecimal._dividecCs/t|ƒ}|tkr|S|j|d|ƒS)z)Swaps self/other and returns __truediv__.r/)ršr’rÊ)r.r„r/r'r'r)Ú __rtruediv__ƒs  zDecimal.__rtruediv__cCs8t|ƒ}|tkr|S|dkr4tƒ}n|j||ƒ}|rV||fS|j|jA}|jƒr·|jƒrš|jtdƒ}||fSt||jtdƒfSn|s|sß|jt dƒ}||fS|jt d|ƒ|jtdƒfSn|j ||ƒ\}}|j |ƒ}||fS)z6 Return (self // other, self % other) Nzdivmod(INF, INF)zINF % xz divmod(0, 0)zx // 0zx % 0) ršr’rr‡r7r‚rar r<rr rÎr´)r.r„r/r;r=ZquotientrÈr'r'r)Ú __divmod__Šs0         zDecimal.__divmod__cCs/t|ƒ}|tkr|S|j|d|ƒS)z(Swaps self/other and returns __divmod__.r/)ršr’rÐ)r.r„r/r'r'r)Ú __rdivmod__®s  zDecimal.__rdivmod__cCsÄt|ƒ}|tkr|S|dkr4tƒ}n|j||ƒ}|rP|S|jƒrl|jtdƒS|s›|rˆ|jtdƒS|jtdƒSn|j||ƒd}|j |ƒ}|S)z self % other NzINF % xzx % 0z0 % 0r-) ršr’rr‡r‚rar rrÎr´)r.r„r/r;rÈr'r'r)Ú__mod__µs"     zDecimal.__mod__cCs/t|ƒ}|tkr|S|j|d|ƒS)z%Swaps self/other and returns __mod__.r/)ršr’rÒ)r.r„r/r'r'r)Ú__rmod__Ðs  zDecimal.__rmod__c Cs||dkrtƒ}nt|ddƒ}|j||ƒ}|rF|S|jƒrb|jtdƒS|s‘|r~|jtdƒS|jtdƒSn|jƒr¶t|ƒ}|j|ƒSt |j |j ƒ}|sót |j d|ƒ}|j|ƒS|j ƒ|j ƒ}||jdkr)|jtƒS|d krW|j||jƒ}|j|ƒSt|ƒ}t|ƒ}|j|jkr¡|jd |j|j9_n|jd |j|j9_t|j|jƒ\}} d | |d@|jkr| |j8} |d7}n|d |jkr.|jtƒS|j } | d krWd| } | } nt | t| ƒ|ƒ}|j|ƒS) zI Remainder nearest to 0- abs(remainder-near) <= other/2 Nr™Tzremainder_near(infinity, x)zremainder_near(x, 0)zremainder_near(0, 0)rPr-r+rœr%r�)rršr‡r‚rar rrr´r¹rNr6r7rŒr@rr»r?rfrTrRrÅr^) r.r„r/r;Úideal_exponentrËr½r¾rÌrÍr=r'r'r)Úremainder_near×sZ                        zDecimal.remainder_nearcCsÝt|ƒ}|tkr|S|dkr4tƒ}n|j||ƒ}|rP|S|jƒr�|jƒrx|jtdƒSt|j|jASn|sÉ|r¶|jt d|j|jAƒS|jt dƒSn|j ||ƒdS)z self // otherNz INF // INFzx // 0z0 // 0r%) ršr’rr‡r‚rar r<r7r rrÎ)r.r„r/r;r'r'r)Ú __floordiv__"s$       zDecimal.__floordiv__cCs/t|ƒ}|tkr|S|j|d|ƒS)z*Swaps self/other and returns __floordiv__.r/)ršr’rÖ)r.r„r/r'r'r)Ú __rfloordiv__>s  zDecimal.__rfloordiv__cCsU|jƒr?|jƒr'tdƒ‚n|jr6dnd}n t|ƒ}t|ƒS)zFloat representation.z%Cannot convert signaling NaN to floatz-nanÚnan)r€rˆrir7r^rm)r.Úsr'r'r)Ú __float__Es    zDecimal.__float__cCs¢|jrB|jƒr$tdƒ‚qB|jƒrBtdƒ‚qBnd|j}|jdkrz|t|jƒd|jS|t|jd|j…p™dƒSdS) z1Converts self to an int, truncating if necessary.zCannot convert NaN to integerz"Cannot convert infinity to integerr-r%rœNrPr�) rOr€rir‚Ú OverflowErrorr7rNrRr8)r.rÙr'r'r)Ú__int__Os    zDecimal.__int__cCs|S)Nr')r.r'r'r)Úreal^sz Decimal.realcCs tdƒS)Nr%)r)r.r'r'r)Úimagbsz Decimal.imagcCs|S)Nr')r.r'r'r)Ú conjugatefszDecimal.conjugatecCstt|ƒƒS)N)Úcomplexrm)r.r'r'r)Ú __complex__iszDecimal.__complex__cCsq|j}|j|j}t|ƒ|krg|t|ƒ|d…jdƒ}t|j||jdƒSt|ƒS)z2Decapitate the payload of a NaN to fit the contextNrPT) r8r@Úclamprcrdr6r7rNr)r.r/ZpayloadZmax_payload_lenr'r'r)r9ls  #zDecimal._fix_nancCs;|jr/|jƒr"|j|ƒSt|ƒSn|jƒ}|jƒ}|sº|j|g|j}tt |j |ƒ|ƒ}||j kr­|j t ƒt |jd|ƒSt|ƒSnt|jƒ|j |j}||kr|j td|jƒ}|j tƒ|j tƒ|S||k}|r4|}n|j |krÅt|jƒ|j |} | dkr‹t |jd|dƒ}d} n|j|j} | || ƒ} |jd| …pÀd} | dkrtt| ƒdƒ} t| ƒ|jkr| dd…} |d7}qn||krA|j td|jƒ}nt |j| |ƒ}| rr|rr|j tƒn|rˆ|j tƒn| rž|j tƒn|j tƒ|sÁ|j t ƒn|S|rÛ|j tƒn|jdkr1|j |kr1|j t ƒ|jd|j |} t |j| |ƒSt|ƒS)zÜ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. rPz above Emaxr%rÂr-Nr�)rOr€r9rrÄÚEtoprArâr¹rºrNrarr6r7rcr8r@rr r Ú_pick_rounding_functionr?r^rRrr )r.r/rÄrãÚexp_maxZnew_expZexp_minr;Zself_is_subnormalruZrounding_methodÚchangedrÆrŽr'r'r)r´xsn                    z Decimal._fixcCst|j|ƒrdSdSdS)z(Also known as round-towards-0, truncate.r%r-Nr�)Ú _all_zerosr8)r.r@r'r'r)Ú _round_downÞszDecimal._round_downcCs|j|ƒ S)zRounds away from 0.)rè)r.r@r'r'r)Ú _round_upåszDecimal._round_upcCs5|j|dkrdSt|j|ƒr-dSdSdS)zRounds 5 up (away from 0)Z56789r-r%Nr�)r8rç)r.r@r'r'r)Ú_round_half_upés zDecimal._round_half_upcCs't|j|ƒrdS|j|ƒSdS)z Round 5 downr-Nr�)Ú _exact_halfr8rê)r.r@r'r'r)Ú_round_half_downòszDecimal._round_half_downcCsJt|j|ƒr9|dks5|j|ddkr9dS|j|ƒSdS)z!Round 5 to even, rest to nearest.r%r-Ú02468Nr�)rër8rê)r.r@r'r'r)Ú_round_half_evenùs#zDecimal._round_half_evencCs(|jr|j|ƒS|j|ƒ SdS)z(Rounds up (not away from 0 if negative.)N)r7rè)r.r@r'r'r)Ú_round_ceilings  zDecimal._round_ceilingcCs(|js|j|ƒS|j|ƒ SdS)z'Rounds down (not towards 0 if negative)N)r7rè)r.r@r'r'r)Ú _round_floors  zDecimal._round_floorcCs<|r*|j|ddkr*|j|ƒS|j|ƒ SdS)z)Round down unless digit prec-1 is 0 or 5.r-Z05N)r8rè)r.r@r'r'r)Ú _round_05ups zDecimal._round_05uprrrrrrrrcCs“|dk rJt|tƒs*tdƒ‚ntdd| ƒ}|j|ƒS|jr}|jƒrntdƒ‚q}tdƒ‚nt|j dt ƒƒS)aÊRound 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') Nz+Second argument to round should be integralr%rÂzcannot round a NaNzcannot round an infinity) r]rRror6ÚquantizerOržrirÛr»r)r.r5rTr'r'r)Ú __round__!s/    zDecimal.__round__cCsI|jr3|jƒr$tdƒ‚q3tdƒ‚nt|jdtƒƒS)zãReturn 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. zcannot round a NaNzcannot round an infinityr%)rOržrirÛrRr»r)r.r'r'r)Ú __floor___s   zDecimal.__floor__cCsI|jr3|jƒr$tdƒ‚q3tdƒ‚nt|jdtƒƒS)zâReturn 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. zcannot round a NaNzcannot round an infinityr%)rOržrirÛrRr»r)r.r'r'r)Ú__ceil__ns   zDecimal.__ceil__cCs�t|ddƒ}t|ddƒ}|js6|jr=|dkrNtƒ}n|jdkrp|jtd|ƒS|jdkr’|jtd|ƒS|jdkrª|}q|jdkrÂ|}q|jdkrþ|sç|jtdƒSt|j|jA}q|jdkr|s#|jtd ƒSt|j|jA}qnBt|j|jAt t |j ƒt |j ƒƒ|j|jƒ}|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. r™TNrWrƒr5rXzINF * 0 in fmaz0 * INF in fma) ršrOrrNrar r<r7r6r^rRr8r¿)r.r„Zthirdr/Úproductr'r'r)Úfma}s6       z Decimal.fmac CsŽt|ƒ}|tkr|St|ƒ}|tkr8|S|dkrPtƒ}n|jƒ}|jƒ}|jƒ}|s†|s†|r|dkr¥|jtd|ƒS|dkrÄ|jtd|ƒS|dkrã|jtd|ƒS|rö|j|ƒS|r |j|ƒS|j|ƒS|jƒo7|jƒo7|jƒsJ|jtdƒS|dkrf|jtdƒS|s||jtdƒS|jƒ|j kr¡|jtdƒS| r¿| r¿|jtd ƒS|j ƒrÔd}n |j }t t |ƒƒ}t|jƒƒ}t|jƒƒ} |j |td |j|ƒ|}x)t| jƒD]} t|d |ƒ}qGWt|| j |ƒ}t|t|ƒdƒS) z!Three argument version of __pow__Nr+rƒz@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œ)ršr’rr€rar r9Ú _isintegerrŒr@Ú_isevenr7rerRrfÚto_integral_valuer¡rTÚranger6r^) r.r„Úmodulor/r…r†Z modulo_is_nanr=ÚbaseÚexponentÚir'r'r)Ú _power_modulo©sl                              $zDecimal._power_modulocCs?t|ƒ}|j|j}}x(|ddkrI|d}|d7}q"Wt|ƒ}|j|j}}x(|ddkr“|d}|d7}qlW|dkrv||9}x(|ddkrÔ|d}|d7}q­W|dkrådS|d|} |jdkr | } n|jƒrT|jdkrT|jt|ƒ} t| | |dƒ} nd} tddd| | | ƒS|jdkry|d} | dkrI|| @|kr°dSt |ƒd} |d d }|t t |ƒƒkrêdSt | ||ƒ} t |||ƒ}| dks(|dkr,dS| |kr<dSd | }nû| d kr@t |ƒdd } t d | |ƒ\}}|rŒdSx(|d dkr¶|d }| d8} q�W|dd}|t t |ƒƒkrádSt | ||ƒ} t |||ƒ}| dks|dkr#dS| |kr3dSd| }ndS|d|krXdS| |}tdt |ƒ|ƒS|dkr�|d|d}}n|dkrÐt t t||ƒƒƒ| krÐdSt |ƒ}|dkrt t t|ƒ|ƒƒ| krdS|d| }}x<|d|dkoCdknr_|d}|d}q$Wx<|d |d ko‚dknrž|d }|d }qcW|dkrq|dkrÇ||krÇdSt ||ƒ\}}|dkrìdSdt |ƒ | >}xGt |||dƒ\}}||kr2Pq||d||}qW||koa|dkshdS|}n|dkr›||dt|ƒkr›dS||}||9}|d|krÃdSt |ƒ}|jƒr|jdkr|jt|ƒ} t|| |t |ƒƒ} nd} td|d| || ƒ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.rœr%r-NrÂrPr+ér¨éé]éArwérYéd)r+rr¨r)rfrRrTr=rør7rNr¹r6Ú_nbitsrcr^Ú_decimal_lshift_exactrÅreÚ _log10_lb)r.r„ÚpÚxÚxcÚxeÚyÚycÚyerþrÔZzerosZ last_digitrªZemaxrÈrrr5Zxc_bitsÚremÚarÌrÍZstr_xcr'r'r)Ú _power_exactþsÒ:                   / /' '     &    zDecimal._power_exactcCsÓ|dk r|j|||ƒSt|ƒ}|tkr;|S|dkrStƒ}n|j||ƒ}|ro|S|s’|s‹|jtdƒStSnd}|jdkrð|j ƒrË|j ƒsád}qán|rá|jtdƒS|j ƒ}n|s |jdkrt |ddƒSt |Sn|jƒrV|jdkrCt |St |ddƒSn|tkr-|j ƒrí|jdkr†d}n'||jkr¡|j}n t|ƒ}|j|}|d|jkrd|j}|jtƒqn'|jtƒ|jtƒd|j}t |dd| |ƒS|jƒ}|jƒr{|jdk|dkkrpt |ddƒSt |Snd}d} |jƒ|jƒ} |dk|jdkkrò| tt|jƒƒkr0t |d|jdƒ}q0n>|jƒ} | tt| ƒƒkr0t |d| dƒ}n|dkr”|j||jdƒ}|dk r”|dkrˆt d|j|jƒ}nd } q”n|dkr~|j} t|ƒ} | j| j}}t|ƒ}|j|j}}|jdkr| }nd }xZt||||| |ƒ\}}|d d tt|ƒƒ| drUPn|d 7}q Wt |t|ƒ|ƒ}n| rÀ|j ƒ rÀt|jƒ|jkrí|jdt|jƒ}t |j|jd||j|ƒ}n|j ƒ}|j!ƒxt"D]}d|j#| 0rœrTrw)rrOr‡r‚r7rr6rNr´rar r@rfrTrRrcr8rÅr^Ú _shallow_copyÚ _set_roundingrr?)r.r/r;r@ÚoprªÚcÚlrÇrrÈr5rÌr?r'r'r)Úsqrt¢ s`                       z Decimal.sqrtcCst|ddƒ}|dkr*tƒ}n|js<|jr½|jƒ}|jƒ}|s`|r½|dkr…|dkr…|j|ƒS|dkrª|dkrª|j|ƒS|j||ƒSn|j|ƒ}|dkrê|j|ƒ}n|dkrÿ|}n|}|j|ƒS)z 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. r™TNr-r%r�)ršrrOr€r´r‡r�Ú compare_total)r.r„r/ÚsnÚonr*r;r'r'r)rº s&          z Decimal.maxcCst|ddƒ}|dkr*tƒ}n|js<|jr½|jƒ}|jƒ}|s`|r½|dkr…|dkr…|j|ƒS|dkrª|dkrª|j|ƒS|j||ƒSn|j|ƒ}|dkrê|j|ƒ}n|dkrÿ|}n|}|j|ƒS)z¡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. r™TNr-r%r�)ršrrOr€r´r‡r�r-)r.r„r/r.r/r*r;r'r'r)r¹/ s&          z Decimal.mincCsJ|jr dS|jdkr dS|j|jd…}|dt|ƒkS)z"Returns whether self is an integerFr%TNrP)rOrNr8rc)r.Úrestr'r'r)røQ s  zDecimal._isintegercCs2| s|jdkrdS|jd|jdkS)z:Returns True if self is even. Assumes self is an integer.r%Tr-rír�)rNr8)r.r'r'r)rùZ szDecimal._isevenc Cs9y|jt|jƒdSWntk r4dSYnXdS)z$Return the adjusted exponent of selfr-r%N)rNrcr8ro)r.r'r'r)rŒ` s zDecimal.adjustedcCs|S)z«Returns 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')r.r'r'r)Ú canonicalh szDecimal.canonicalcCsAt|ddƒ}|j||ƒ}|r.|S|j|d|ƒS)z¶Compares self to the other operand numerically. It's pretty much like compare(), but all NaNs signal, with signaling NaNs taking precedence over quiet NaNs. r™Tr/)ršrŠr›)r.r„r/r;r'r'r)Úcompare_signalp s zDecimal.compare_signalcCsÝt|ddƒ}|jr)|j r)tS|j r@|jr@tS|j}|jƒ}|jƒ}|sm|rs||krçt|jƒ|jf}t|jƒ|jf}||krÆ|r¿tStSn||krã|rÜtStSntS|r0|dkrýtS|dkr tS|dkrtS|dkrptSqs|dkr@tS|dkrPtS|dkr`tS|dkrstSn||krƒtS||kr“tS|j|jkr¶|r¯tStSn|j|jkrÙ|rÒtStSntS)zõCompares 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. r™Tr-r+) ršr7Ú _NegativeOnerr€rcr8Ú_ZerorN)r.r„r/r=Zself_nanZ other_nanZself_keyZ other_keyr'r'r)r-| sf                 zDecimal.compare_totalcCs7t|ddƒ}|jƒ}|jƒ}|j|ƒS)z–Compares self to other using abstract repr., ignoring sign. Like compare_total, but with operand's sign ignored and assumed to be 0. r™T)ršr²r-)r.r„r/rÙÚor'r'r)Úcompare_total_magÅ s  zDecimal.compare_total_magcCstd|j|j|jƒS)z'Returns a copy with the sign set to 0. r%)r6r8rNrO)r.r'r'r)r²Ð szDecimal.copy_abscCsE|jr%td|j|j|jƒStd|j|j|jƒSdS)z&Returns a copy with the sign inverted.r%r-N)r7r6r8rNrO)r.r'r'r)r³Ô s zDecimal.copy_negatecCs1t|ddƒ}t|j|j|j|jƒS)z$Returns self with the sign of other.r™T)ršr6r7r8rNrO)r.r„r/r'r'r)Ú copy_signÛ szDecimal.copy_signc Cs�|dkrtƒ}n|jd|ƒ}|r4|S|jƒd krJtS|sTtS|jƒdkrpt|ƒS|j}|jƒ}|jdkrÓ|t t |j ddƒƒkrÓt dd|j dƒ}n†|jdkr(|t t |j ƒ ddƒƒkr(t dd|j ƒdƒ}n1|jdkrj|| krjt ddd|dd| ƒ}nï|jdkr¬|| dkr¬t dd|d| dƒ}n­t|ƒ}|j|j}}|jdkrä| }nd}xTt||||ƒ\} } | d d t t | ƒƒ|dr3Pn|d7}qíWt dt | ƒ| ƒ}|jƒ}|jtƒ} |j|ƒ}| |_|S) zReturns e ** self.Nr/r-r%rYrÂrPr>rwrœr�)rr‡r‚r4rrr@rŒr7rcr^rAr6rÄrfrRrTr=Ú_dexpr'r(rr´r?) r.r/r;r Úadjr)r*rªrrÆrTr?r'r'r)rTá sJ     26& "  &  z Decimal.expcCsdS)zÃReturn 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')r.r'r'r)Ú is_canonical, szDecimal.is_canonicalcCs|j S)z�Return True if self is finite; otherwise return False. A Decimal instance is considered finite if it is neither infinite nor a NaN. )rO)r.r'r'r)Ú is_finite4 szDecimal.is_finitecCs |jdkS)z8Return True if self is infinite; otherwise return False.rX)rN)r.r'r'r)r"< szDecimal.is_infinitecCs |jdkS)z>Return True if self is a qNaN or sNaN; otherwise return False.r5rW)r5rW)rN)r.r'r'r)rž@ szDecimal.is_nancCs?|js| rdS|dkr,tƒ}n|j|jƒkS)z?Return True if self is a normal number; otherwise return False.FN)rOrr!rŒ)r.r/r'r'r)Ú is_normalD s   zDecimal.is_normalcCs |jdkS)z;Return True if self is a quiet NaN; otherwise return False.r5)rN)r.r'r'r)r‰L szDecimal.is_qnancCs |jdkS)z8Return True if self is negative; otherwise return False.r-)r7)r.r'r'r)Ú is_signedP szDecimal.is_signedcCs |jdkS)z?Return True if self is a signaling NaN; otherwise return False.rW)rN)r.r'r'r)rˆT szDecimal.is_snancCs?|js| rdS|dkr,tƒ}n|jƒ|jkS)z9Return True if self is subnormal; otherwise return False.FN)rOrrŒr!)r.r/r'r'r)Ú is_subnormalX s   zDecimal.is_subnormalcCs|j o|jdkS)z6Return True if self is a zero; otherwise return False.rP)rOr8)r.r'r'r)Úis_zero` szDecimal.is_zerocCsù|jt|jƒd}|dkrBtt|ddƒƒdS|dkrnttd|ddƒƒdSt|ƒ}|j|j}}|dkrØt|d| ƒ}t|ƒ}t|ƒt|ƒ||kS|ttd| |ƒƒdS)zÌCompute 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. r-érœr+r%r�r�)rNrcr8r^rfrRrT)r.r9r)r*rªÚnumÚdenr'r'r)Ú _ln_exp_boundd s      zDecimal._ln_exp_boundc Cst|dkrtƒ}n|jd|ƒ}|r4|S|s>tS|jƒdkrTtS|tkrdtS|jdkrƒ|jt dƒSt |ƒ}|j |j }}|j }||jƒd}xPt|||ƒ}|ddttt|ƒƒƒ|drPn|d7}qÂWtt |d kƒtt|ƒƒ| ƒ}|jƒ}|jtƒ} |j|ƒ}| |_|S) z/Returns the natural (base e) logarithm of self.Nr/r-zln of a negative valuer+rwrœrYr%)rr‡Ú_NegativeInfinityr‚Ú _Infinityrr4r7rar rfrRrTr@rCÚ_dlogrcr^rer6r'r(rr´r?) r.r/r;r)r*rªr r$rÆr?r'r'r)Úln} s:      ,+  z Decimal.lncCs|jt|jƒd}|dkr:tt|ƒƒdS|dkr^ttd|ƒƒdSt|ƒ}|j|j}}|dkrÐt|d| ƒ}td|ƒ}t|ƒt|ƒ||kdStd| |ƒ}t|ƒ||dkdS) zÎCompute 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. r-r+r%rœéçZ231r�r�)rNrcr8r^rfrRrT)r.r9r)r*rªrArBr'r'r)r¯ s     "zDecimal._log10_exp_boundc CsÄ|dkrtƒ}n|jd|ƒ}|r4|S|s>tS|jƒdkrTtS|jdkrs|jtdƒS|jddkrÓ|jdd…dt |jƒdkrÓt |j t |jƒdƒ}nºt |ƒ}|j |j}}|j}||jƒd}xPt|||ƒ}|d d t tt|ƒƒƒ|drTPn|d 7}qWtt |dkƒtt|ƒƒ| ƒ}|jƒ}|jtƒ} |j|ƒ}| |_|S) z&Returns the base 10 logarithm of self.Nr/r-zlog10 of a negative valuer%rÂrPr+rwrœrY)rr‡rDr‚rEr7rar r8rcrrNrfrRrTr@rÚ_dlog10r^rer6r'r(rr´r?) r.r/r;r)r*rªr r$rÆr?r'r'r)Úlog10Í s:   =#  ,+  z Decimal.log10cCs||jd|ƒ}|r|S|dkr4tƒ}n|jƒrDtS|s]|jtddƒSt|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). r/Nzlogb(0)r-) r‡rr‚rErar rrŒr´)r.r/r;r'r'r)Úlogb s    z Decimal.logbcCsJ|jdks|jdkr"dSx!|jD]}|dkr,dSq,WdS)z×Return 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%FZ01T)r7rNr8)r.Údigr'r'r)Ú _islogical s  zDecimal._islogicalcCs°|jt|ƒ}|dkr0d||}n#|dkrS||j d…}n|jt|ƒ}|dkrƒd||}n#|dkr¦||j d…}n||fS)Nr%rP)r@rc)r.r/ÚopaÚopbZdifr'r'r)Ú _fill_logical, s    zDecimal._fill_logicalcCs¶|dkrtƒ}nt|ddƒ}|jƒ sD|jƒ rQ|jtƒS|j||j|jƒ\}}djdd„t||ƒDƒƒ}t d|j dƒp¯ddƒS) z;Applies an 'and' operation between self and other's digits.Nr™TrScSs2g|](\}}tt|ƒt|ƒ@ƒ‘qSr')r^rR)Ú.0rÚbr'r'r)ú G s z'Decimal.logical_and..r%rP) rršrMrar rPr8rkÚzipr6rd)r.r„r/rNrOrr'r'r)Ú logical_and9 s   !%zDecimal.logical_andcCs;|dkrtƒ}n|jtdd|jdƒ|ƒS)zInvert all its digits.Nr%rÂ)rÚ logical_xorr6r@)r.r/r'r'r)Úlogical_invertJ s  zDecimal.logical_invertcCs¶|dkrtƒ}nt|ddƒ}|jƒ sD|jƒ rQ|jtƒS|j||j|jƒ\}}djdd„t||ƒDƒƒ}t d|j dƒp¯ddƒS) z:Applies an 'or' operation between self and other's digits.Nr™TrScSs2g|](\}}tt|ƒt|ƒBƒ‘qSr')r^rR)rQrrRr'r'r)rS_ s z&Decimal.logical_or..r%rP) rršrMrar rPr8rkrTr6rd)r.r„r/rNrOrr'r'r)Ú logical_orQ s   !%zDecimal.logical_orcCs¶|dkrtƒ}nt|ddƒ}|jƒ sD|jƒ rQ|jtƒS|j||j|jƒ\}}djdd„t||ƒDƒƒ}t d|j dƒp¯ddƒS) z;Applies an 'xor' operation between self and other's digits.Nr™TrScSs2g|](\}}tt|ƒt|ƒAƒ‘qSr')r^rR)rQrrRr'r'r)rSp s z'Decimal.logical_xor..r%rP) rršrMrar rPr8rkrTr6rd)r.r„r/rNrOrr'r'r)rVb s   !%zDecimal.logical_xorcCst|ddƒ}|dkr*tƒ}n|js<|jr½|jƒ}|jƒ}|s`|r½|dkr…|dkr…|j|ƒS|dkrª|dkrª|j|ƒS|j||ƒSn|jƒj|jƒƒ}|dkrö|j|ƒ}n|dkr |}n|}|j|ƒS)z8Compares the values numerically with their sign ignored.r™TNr-r%r�) ršrrOr€r´r‡r²r�r-)r.r„r/r.r/r*r;r'r'r)Úmax_mags s&          zDecimal.max_magcCst|ddƒ}|dkr*tƒ}n|js<|jr½|jƒ}|jƒ}|s`|r½|dkr…|dkr…|j|ƒS|dkrª|dkrª|j|ƒS|j||ƒSn|jƒj|jƒƒ}|dkrö|j|ƒ}n|dkr |}n|}|j|ƒS)z8Compares the values numerically with their sign ignored.r™TNr-r%r�) ršrrOr€r´r‡r²r�r-)r.r„r/r.r/r*r;r'r'r)Úmin_mag‘ s&          zDecimal.min_magcCsá|dkrtƒ}n|jd|ƒ}|r4|S|jƒdkrJtS|jƒdkrytdd|j|jƒƒS|jƒ}|jt ƒ|j ƒ|j |ƒ}||kr»|S|j tdd|j ƒdƒ|ƒS)z=Returns the largest representable number smaller than itself.Nr/r-r%r>rÂr�)rr‡r‚rDr6r@rãrGr(rÚ_ignore_all_flagsr´rÀrÄ)r.r/r;Únew_selfr'r'r)Ú next_minus¯ s"      zDecimal.next_minuscCsá|dkrtƒ}n|jd|ƒ}|r4|S|jƒdkrJtS|jƒdkrytdd|j|jƒƒS|jƒ}|jt ƒ|j ƒ|j |ƒ}||kr»|S|j tdd|j ƒdƒ|ƒS)z=Returns the smallest representable number larger than itself.Nr/r-r>r%rÂr�)rr‡r‚rEr6r@rãrGr(rr[r´r¿rÄ)r.r/r;r\r'r'r)Ú next_plusÆ s"      zDecimal.next_pluscCs@t|ddƒ}|dkr*tƒ}n|j||ƒ}|rF|S|j|ƒ}|dkrn|j|ƒS|dkrŒ|j|ƒ}n|j|ƒ}|jƒrÚ|jt d|j ƒ|jt ƒ|jt ƒnb|j ƒ|jkr<|jtƒ|jtƒ|jt ƒ|jt ƒ|s<|jtƒq<n|S)a‹Returns 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. r™TNr%r-z Infinite result from next_towardr�)ršrr‡r�r7r^r]r‚rarr7r r rŒr!rr r)r.r„r/r;Z comparisonr'r'r)Ú next_towardÝ s4              zDecimal.next_towardcCs¿|jƒrdS|jƒr dS|jƒ}|dkr<dS|dkrLdS|jƒrl|jredSdSn|dkr„tƒ}n|jd |ƒrª|jr£d Sd Sn|jr·d Sd SdS)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 rƒr§r-z +Infinityz -Infinityz-Zeroz+ZeroNr/z -Subnormalz +Subnormalz-Normalz+Normalr�)rˆr‰r‚r?r7rr>)r.r/Úinfr'r'r)Ú number_class s,           zDecimal.number_classcCs tdƒS)z'Just returns 10, as this is Decimal, :)rœ)r)r.r'r'r)Úradix5sz Decimal.radixcCsV|dkrtƒ}nt|ddƒ}|j||ƒ}|rF|S|jdkrb|jtƒS|j t|ƒko†|jkns˜|jtƒS|jƒr®t |ƒSt|ƒ}|j }|jt |ƒ}|dkród||}n |dkr|| d…}n||d…|d|…}t |j |jdƒpLd|jƒS)z5Returns a rotated copy of self, value-of-other times.Nr™Tr%rP)rršr‡rNrar r@rRr‚rr8rcr6r7rd)r.r„r/r;ÚtorotÚrotdigÚtopadZrotatedr'r'r)Úrotate9s,   )        zDecimal.rotatecCs|dkrtƒ}nt|ddƒ}|j||ƒ}|rF|S|jdkrb|jtƒSd|j|j}d|j|j}|t|ƒko§|kns¹|jtƒS|j ƒrÏt |ƒSt |j |j |jt|ƒƒ}|j|ƒ}|S)z>Returns self operand after adding the second value to its exp.Nr™Tr%r+r�)rršr‡rNrar rAr@rRr‚rr6r7r8r´)r.r„r/r;ZliminfZlimsupr}r'r'r)ÚscalebZs"   "   %zDecimal.scalebcCsy|dkrtƒ}nt|ddƒ}|j||ƒ}|rF|S|jdkrb|jtƒS|j t|ƒko†|jkns˜|jtƒS|jƒr®t |ƒSt|ƒ}|j }|jt |ƒ}|dkród||}n |dkr|| d…}n|dkr2|d|…}n"|d|}||j d…}t |j |jdƒpod|jƒS)z5Returns a shifted copy of self, value-of-other times.Nr™Tr%rP)rršr‡rNrar r@rRr‚rr8rcr6r7rd)r.r„r/r;rcrdreZshiftedr'r'r)rÇss2   )         z Decimal.shiftcCs|jt|ƒffS)N)Ú __class__r^)r.r'r'r)Ú __reduce__šszDecimal.__reduce__cCs)t|ƒtkr|S|jt|ƒƒS)N)Útyperrhr^)r.r'r'r)Ú__copy__�szDecimal.__copy__cCs)t|ƒtkr|S|jt|ƒƒS)N)rjrrhr^)r.Úmemor'r'r)Ú __deepcopy__¢szDecimal.__deepcopy__cCs|dkrtƒ}nt|d|ƒ}|jr„t|j|ƒ}t|jƒƒ}|ddkrt|d7}nt|||ƒS|ddkr®ddg|j|dr?rCrGrrJrKrMrPrUrWrXrVrYrZr]r^r_rarbrfrgrÇrirkrmrzr'r'r'r)r)s  –(   !  @        4 V7; !$K        f        >  ,U íØG " c*"    I  K         2 3  .* !'   FcCs7tjtƒ}||_||_||_||_|S)z½Create a decimal instance directly, without any validation, normalization (e.g. removal of leading zeros) or argument conversion. This function is for *internal use only*. )r[r\rr7r8rNrO)r=Z coefficientrþZspecialr.r'r'r)r6ûs     r6c@s:eZdZdZdd„Zdd„Zdd„ZdS) rMz­Context manager class to support localcontext(). Sets a copy of the supplied context in __enter__() and restores the previous decimal context in __exit__() cCs|jƒ|_dS)N)rGÚ new_context)r.rƒr'r'r)Ú__init__sz_ContextManager.__init__cCs tƒ|_t|jƒ|jS)N)rÚ saved_contextrrƒ)r.r'r'r)Ú __enter__s  z_ContextManager.__enter__cCst|jƒdS)N)rr…)r.ÚtÚvÚtbr'r'r)Ú__exit__sz_ContextManager.__exit__N)r1r2r3r4r„r†rŠr'r'r'r)rMs   rMc @seZdZdZddddddddddd„ Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„ZeZddd„Zdd„Zdd„Zdd „ZdZd!d"„Zd#d$„Zd%d&„Zd'd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„Zd6d7„Zd8d9„Z d:d;„Z!d<d=„Z"d>d?„Z#d@dA„Z$dBdC„Z%dDdE„Z&dFdG„Z'dHdI„Z(dJdK„Z)dLdM„Z*dNdO„Z+dPdQ„Z,dRdS„Z-dTdU„Z.dVdW„Z/dXdY„Z0dZd[„Z1d\d]„Z2d^d_„Z3d`da„Z4dbdc„Z5ddde„Z6dfdg„Z7dhdi„Z8djdk„Z9dldm„Z:dndo„Z;dpdq„Z<drds„Z=dtdu„Z>dvdw„Z?dxdy„Z@dzd{„ZAd|d}„ZBd~d„ZCd€d�„ZDd‚dƒ„ZEd„d…„ZFd†d‡„ZGddˆd‰„ZHdŠd‹„ZIdŒd�„ZJdŽd�„ZKd�d‘„ZLd’d“„ZMd”d•„ZNd–d—„ZOd˜d™„ZPdšd›„ZQdœd�„ZRdždŸ„ZSd d¡„ZTd¢d£„ZUd¤d¥„ZVeVZWdS)¦raßContains 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 s¿y t} Wntk rYnX|dk r1|n| j|_|dk rO|n| j|_|dk rm|n| j|_|dk r‹|n| j|_|dk r©|n| j|_|dk rÇ|n| j|_| dkrëg|_n | |_ˆdkr| j j ƒ|_ nAt ˆt ƒsMt ‡fdd†t ˆDƒƒ|_ n ˆ|_ ˆdkrzt jt dƒ|_nAt ˆt ƒs²t ‡fdd†t ˆDƒƒ|_n ˆ|_dS)Nc3s'|]}|t|ˆkƒfVqdS)N)rR)rQrÙ)rr'r)ú Nsz#Context.__init__..r%c3s'|]}|t|ˆkƒfVqdS)N)rR)rQrÙ)rr'r)r‹Us)rÚ NameErrorr@r?r!rAr¬râÚ_ignored_flagsrrGr]r�rÚfromkeysr) r.r@r?r!rAr¬rârrr�Zdcr')rrr)r„5s.      )  )zContext.__init__cCsàt|tƒs"td|ƒ‚n|dkr\||krÍtd||||fƒ‚qÍnq|dkr–||krÍtd||||fƒ‚qÍn7||ks®||krÍtd||||fƒ‚ntj|||ƒS)Nz%s must be an integerz-infz%s must be in [%s, %d]. got: %sr`z%s must be in [%d, %s]. got: %sz%s must be in [%d, %d]. got %s)r]rRrorir[Ú __setattr__)r.ÚnamerqZvminZvmaxr'r'r)Ú_set_integer_checkYs  "  "zContext._set_integer_checkcCs•t|tƒs"td|ƒ‚nx-|D]%}|tkr)td|ƒ‚q)q)Wx-tD]%}||krYtd|ƒ‚qYqYWtj|||ƒS)Nz%s must be a signal dictz%s is not a valid signal dict)r]r�rorÚKeyErrorr[r�)r.r�r}Úkeyr'r'r)Ú_set_signal_dictgs    zContext._set_signal_dictcCsC|dkr"|j||ddƒS|dkrD|j||ddƒS|dkrf|j||ddƒS|dkrˆ|j||ddƒS|d krª|j||ddƒS|d krè|tkrÕtd |ƒ‚ntj|||ƒS|d ks|d kr|j||ƒS|dkr/tj|||ƒStd|ƒ‚dS)Nr@r-r`r!z-infr%rAr¬râr?z%s: invalid rounding moderrr�z.'decimal.Context' object has no attribute '%s')r‘Ú_rounding_modesror[r�r”rK)r.r�rqr'r'r)r�rs(        zContext.__setattr__cCstd|ƒ‚dS)Nz%s cannot be deleted)rK)r.r�r'r'r)Ú __delattr__‹szContext.__delattr__c Csodd„|jjƒDƒ}dd„|jjƒDƒ}|j|j|j|j|j|j|j ||ffS)NcSs"g|]\}}|r|‘qSr'r')rQÚsigrˆr'r'r)rS�s z&Context.__reduce__..cSs"g|]\}}|r|‘qSr'r')rQr—rˆr'r'r)rS‘s ) rÚitemsrrhr@r?r!rAr¬râ)r.rrr'r'r)ri�s zContext.__reduce__cCs¢g}|jdt|ƒƒdd„|jjƒDƒ}|jddj|ƒdƒdd„|jjƒDƒ}|jddj|ƒdƒdj|ƒd S) zShow the current context.zrContext(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d, clamp=%(clamp)dcSs%g|]\}}|r|j‘qSr')r1)rQr|rˆr'r'r)rS�s z$Context.__repr__..zflags=[z, ú]cSs%g|]\}}|r|j‘qSr')r1)rQr‡rˆr'r'r)rSŸs ztraps=[ú))rjÚvarsrr˜rkr)r.rÙÚnamesr'r'r)r¦–s zContext.__repr__cCs%x|jD]}d|j|>> 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 )rrnr´)r.r|r}r'r'r)Úcreate_decimal_from_floatsz!Context.create_decimal_from_floatcCs"t|ddƒ}|jd|ƒ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') r™Tr/)ršr¸)r.rr'r'r)re&sz Context.abscCsNt|ddƒ}|j|d|ƒ}|tkrFtd|ƒ‚n|SdS)a«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') >>> ExtendedContext.add(1, Decimal(2)) Decimal('3') >>> ExtendedContext.add(Decimal(8), 5) Decimal('13') >>> ExtendedContext.add(5, 5) Decimal('10') r™Tr/zUnable to convert %s to DecimalN)ršr¿r’ro)r.rrRrÍr'r'r)Úadd;s  z Context.addcCst|j|ƒƒS)N)r^r´)r.rr'r'r)Ú_applyPszContext._applycCs(t|tƒstdƒ‚n|jƒS)zûReturns 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.)r]rror1)r.rr'r'r)r1Ss zContext.canonicalcCs%t|ddƒ}|j|d|ƒS)a…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') >>> ExtendedContext.compare(1, 2) Decimal('-1') >>> ExtendedContext.compare(Decimal(1), 2) Decimal('-1') >>> ExtendedContext.compare(1, Decimal(2)) Decimal('-1') r™Tr/)ršr›)r.rrRr'r'r)r›`s!zContext.comparecCs%t|ddƒ}|j|d|ƒ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') r™Tr/)ršr2)r.rrRr'r'r)r2„s zContext.compare_signalcCst|ddƒ}|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') r™T)ršr-)r.rrRr'r'r)r-§szContext.compare_totalcCst|ddƒ}|j|ƒS)z£Compares two operands using their abstract representation ignoring sign. Like compare_total, but with operand's sign ignored and assumed to be 0. r™T)ršr6)r.rrRr'r'r)r6ÄszContext.compare_total_magcCst|ddƒ}|jƒS)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') r™T)ršr²)r.rr'r'r)r²Ìs zContext.copy_abscCst|ddƒ}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') r™T)ršr)r.rr'r'r)Ú copy_decimalÙs zContext.copy_decimalcCst|ddƒ}|jƒS)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') r™T)ršr³)r.rr'r'r)r³æs zContext.copy_negatecCst|ddƒ}|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') r™T)ršr7)r.rrRr'r'r)r7ószContext.copy_signcCsNt|ddƒ}|j|d|ƒ}|tkrFtd|ƒ‚n|SdS)aˆ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') >>> ExtendedContext.divide(5, 5) Decimal('1') >>> ExtendedContext.divide(Decimal(5), 5) Decimal('1') >>> ExtendedContext.divide(5, Decimal(5)) Decimal('1') r™Tr/zUnable to convert %s to DecimalN)ršrÊr’ro)r.rrRrÍr'r'r)Údivide s  zContext.dividecCsNt|ddƒ}|j|d|ƒ}|tkrFtd|ƒ‚n|SdS)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') r™Tr/zUnable to convert %s to DecimalN)ršrÖr’ro)r.rrRrÍr'r'r)Ú divide_int0s  zContext.divide_intcCsNt|ddƒ}|j|d|ƒ}|tkrFtd|ƒ‚n|SdS)aÝReturn (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')) r™Tr/zUnable to convert %s to DecimalN)ršrÐr’ro)r.rrRrÍr'r'r)rÅGs  zContext.divmodcCs"t|ddƒ}|jd|ƒ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') r™Tr/)ršrT)r.rr'r'r)rT\sz Context.expcCs(t|ddƒ}|j||d|ƒ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') r™Tr/)ršr÷)r.rrRr*r'r'r)r÷tsz Context.fmacCs(t|tƒstdƒ‚n|jƒS)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.)r]rror:)r.rr'r'r)r:‹s zContext.is_canonicalcCst|ddƒ}|jƒS)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 r™T)ršr;)r.rr'r'r)r;˜szContext.is_finitecCst|ddƒ}|jƒS)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 r™T)ršr")r.rr'r'r)r"®s zContext.is_infinitecCst|ddƒ}|jƒS)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 r™T)ršrž)r.rr'r'r)rž½s zContext.is_nancCs"t|ddƒ}|jd|ƒS)aïReturn 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 r™Tr/)ršr<)r.rr'r'r)r<ÍszContext.is_normalcCst|ddƒ}|jƒS)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 r™T)ršr‰)r.rr'r'r)r‰äs zContext.is_qnancCst|ddƒ}|jƒS)a�Return 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 r™T)ršr=)r.rr'r'r)r=ószContext.is_signedcCst|ddƒ}|jƒS)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 r™T)ršrˆ)r.rr'r'r)rˆs zContext.is_snancCs"t|ddƒ}|jd|ƒS)aôReturn 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 r™Tr/)ršr>)r.rr'r'r)r>szContext.is_subnormalcCst|ddƒ}|jƒS)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 r™T)ršr?)r.rr'r'r)r?*szContext.is_zerocCs"t|ddƒ}|jd|ƒS)aþReturns 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') r™Tr/)ršrG)r.rr'r'r)rG;sz Context.lncCs"t|ddƒ}|jd|ƒS)a§Returns 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') r™Tr/)ršrJ)r.rr'r'r)rJQsz Context.log10cCs"t|ddƒ}|jd|ƒ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') r™Tr/)ršrK)r.rr'r'r)rKmsz Context.logbcCs%t|ddƒ}|j|d|ƒS)a”Applies 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') r™Tr/)ršrU)r.rrRr'r'r)rU‡szContext.logical_andcCs"t|ddƒ}|jd|ƒ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') r™Tr/)ršrW)r.rr'r'r)rW¢szContext.logical_invertcCs%t|ddƒ}|j|d|ƒS)a�Applies 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') r™Tr/)ršrX)r.rrRr'r'r)rXµszContext.logical_orcCs%t|ddƒ}|j|d|ƒS)a˜Applies 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') r™Tr/)ršrV)r.rrRr'r'r)rVÐszContext.logical_xorcCs%t|ddƒ}|j|d|ƒS)a³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 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') r™Tr/)ršrº)r.rrRr'r'r)rºësz Context.maxcCs%t|ddƒ}|j|d|ƒS)aÇCompares 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') r™Tr/)ršrY)r.rrRr'r'r)rYszContext.max_magcCs%t|ddƒ}|j|d|ƒS)a¸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 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') r™Tr/)ršr¹)r.rrRr'r'r)r¹sz Context.mincCs%t|ddƒ}|j|d|ƒS)aÄCompares 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') r™Tr/)ršrZ)r.rrRr'r'r)rZ2szContext.min_magcCs"t|ddƒ}|jd|ƒS)aÎ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') >>> ExtendedContext.minus(1) Decimal('-1') r™Tr/)ršrµ)r.rr'r'r)ÚminusCsz Context.minuscCsNt|ddƒ}|j|d|ƒ}|tkrFtd|ƒ‚n|SdS)aà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') >>> ExtendedContext.multiply(7, 7) Decimal('49') >>> ExtendedContext.multiply(Decimal(7), 7) Decimal('49') >>> ExtendedContext.multiply(7, Decimal(7)) Decimal('49') r™Tr/zUnable to convert %s to DecimalN)ršrÃr’ro)r.rrRrÍr'r'r)ÚmultiplyTs  zContext.multiplycCs"t|ddƒ}|jd|ƒ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') r™Tr/)ršr])r.rr'r'r)r]tszContext.next_minuscCs"t|ddƒ}|jd|ƒ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') r™Tr/)ršr^)r.rr'r'r)r^ˆszContext.next_pluscCs%t|ddƒ}|j|d|ƒS)a´Returns 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') r™Tr/)ršr_)r.rrRr'r'r)r_œs zContext.next_towardcCs"t|ddƒ}|jd|ƒS)a³normalize 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') r™Tr/)ršr )r.rr'r'r)r ¿szContext.normalizecCs"t|ddƒ}|jd|ƒS)aâReturns 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' r™Tr/)ršra)r.rr'r'r)ra×s/zContext.number_classcCs"t|ddƒ}|jd|ƒS)a¿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') >>> ExtendedContext.plus(-1) Decimal('-1') r™Tr/)ršr¶)r.rr'r'r)Úplus sz Context.pluscCsQt|ddƒ}|j||d|ƒ}|tkrItd|ƒ‚n|SdS)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') r™Tr/zUnable to convert %s to DecimalN)ršrr’ro)r.rrRrürÍr'r'r)Úpowers I z Context.powercCs%t|ddƒ}|j|d|ƒ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') r™Tr/)ršrò)r.rrRr'r'r)ròjs7zContext.quantizecCs tdƒS)zkJust returns 10, as this is Decimal, :) >>> ExtendedContext.radix() Decimal('10') rœ)r)r.r'r'r)rb¤sz Context.radixcCsNt|ddƒ}|j|d|ƒ}|tkrFtd|ƒ‚n|SdS)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') r™Tr/zUnable to convert %s to DecimalN)ršrÒr’ro)r.rrRrÍr'r'r)rȬs  zContext.remaindercCs%t|ddƒ}|j|d|ƒ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') r™Tr/)ršrÕ)r.rrRr'r'r)rÕÒszContext.remainder_nearcCs%t|ddƒ}|j|d|ƒ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') r™Tr/)ršrf)r.rrRr'r'r)rfôszContext.rotatecCst|ddƒ}|j|ƒS)aÝReturns 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 r™T)ršr#)r.rrRr'r'r)r#szContext.same_quantumcCs%t|ddƒ}|j|d|ƒ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') r™Tr/)ršrg)r.rrRr'r'r)rg)szContext.scalebcCs%t|ddƒ}|j|d|ƒ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') r™Tr/)ršrÇ)r.rrRr'r'r)rÇ<sz Context.shiftcCs"t|ddƒ}|jd|ƒS)a¦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.sqrt(2) Decimal('1.41421356') >>> ExtendedContext.prec 9 r™Tr/)ršr,)r.rr'r'r)r,Zsz Context.sqrtcCsNt|ddƒ}|j|d|ƒ}|tkrFtd|ƒ‚n|SdS)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') r™Tr/zUnable to convert %s to DecimalN)ršrÀr’ro)r.rrRrÍr'r'r)Úsubtractzs  zContext.subtractcCs"t|ddƒ}|jd|ƒS)zyConverts a number to a string, using scientific notation. The operation is not affected by the context. r™Tr/)ršr±)r.rr'r'r)r±‘szContext.to_eng_stringcCs"t|ddƒ}|jd|ƒS)zyConverts a number to a string, using scientific notation. The operation is not affected by the context. r™Tr/)ršr°)r.rr'r'r)Ú to_sci_string™szContext.to_sci_stringcCs"t|ddƒ}|jd|ƒ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') r™Tr/)ršr&)r.rr'r'r)r&¡szContext.to_integral_exactcCs"t|ddƒ}|jd|ƒ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') r™Tr/)ršrú)r.rr'r'r)rú¿szContext.to_integral_value)Xr1r2r3r4r„r‘r”r�r–rir¦rHržr'rGrkrar[r£r¥r¤rÄrãr(r¦r§rer¨r©r1r›r2r-r6r²rªr³r7r«r¬rÅrTr÷r:r;r"ržr<r‰r=rˆr>r?rGrJrKrUrWrXrVrºrYr¹rZr­r®r]r^r_r rar¯r°ròrbrÈrÕrfr#rgrÇr,r±r±r²r&rúr‚r'r'r'r)r"s®   "                   $ #    %                            #  2 P :  & "         c@s7eZdZd Zddd„Zdd„ZeZdS) rfr=rRrTNcCs‘|dkr*d|_d|_d|_nct|tƒrf|j|_t|jƒ|_|j|_n'|d|_|d|_|d|_dS)Nr%r-r+)r=rRrTr]rr7r8rN)r.rqr'r'r)r„ås       z_WorkRep.__init__cCsd|j|j|jfS)Nz (%r, %r, %r))r=rRrT)r.r'r'r)r¦ôsz_WorkRep.__repr__)zsignzintzexp)r1r2r3r{r„r¦r°r'r'r'r)rfßs  rfcCsÔ|j|jkr!|}|}n |}|}tt|jƒƒ}tt|jƒƒ}|jtd||dƒ}||jd|kr¡d|_||_n|jd|j|j9_|j|_||fS)zcNormalizes op1, op2 to have the same exp and length of coefficient. Done during addition. r-r+rœr�)rTrcr^rRr¹)r½r¾r@Ztmpr„Ztmp_lenZ other_lenrTr'r'r)r¼ûs    r¼cCs{|dkrdS|dkr(|d|Stt|ƒƒ}t|ƒt|jdƒƒ}|| krjdS|d| SdS)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%rœrPN)r^rercÚrstrip)r5rªZstr_nZval_nr'r'r)rs   rcCs^|dks|dkr'tdƒ‚nd}x*||krY||| |d?}}q0W|S)zóClosest 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.r-)ri)r5rrRr'r'r)Ú _sqrt_nearest0s r´cCs7d|>||?}}|d||d@|d@|kS)z‰Given an integer x and a nonnegative integer shift, return closest integer to x / 2**shift; use round-to-even in case of a tie. r-r+r')r rÇrRrÌr'r'r)Ú_rshift_nearest?srµcCs/t||ƒ\}}|d||d@|kS)zaClosest integer to a/b, a and b positive integers; rounds to even in the case of a tie. r+r-)rÅ)rrRrÌrÍr'r'r)Ú _div_nearestGsr¶rc Cs7||}d}x�||kr9t|ƒ||>|ks_||kr¡t|ƒ||?|kr¡t||d>|t||t||ƒ|ƒƒ}|d7}qWtdtt|ƒƒd|ƒ }t||ƒ}t||ƒ}x>t|dddƒD]&}t||ƒt|||ƒ}qüWt|||ƒS)aÉInteger approximation to M*log(x/M), with absolute error boundable in terms only of x/M. Given positive integers x and M, return an integer approximation to M * log(x/M). For L = 8 and 0.1 <= x/M <= 10 the difference between the approximation and the exact result is at most 22. For L = 8 and 1.0 <= x/M <= 10.0 the difference is at most 15. In both cases these are upper bounds on the error; it will usually be much smaller.r%r-rœrYiöÿÿÿr�)rer¶r´rµrRrcr^rû) r ÚMÚLrÚRÚTZyshiftÚwr~r'r'r)Ú_ilogOs )&'%$r¼c Csï|d7}tt|ƒƒ}||||dk}|dkrÄd|}|||}|dkru|d|9}nt|d| ƒ}t||ƒ}t|ƒ}t|||ƒ}||} nd}t|d| ƒ} t| |dƒS)z¾Given integers c, e and p with c > 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.r+r-r%rœr)rcr^r¶r¼Ú _log10_digits) r*rªr r+r|r·r~Úlog_dZlog_10Z log_tenpowerr'r'r)rIs       rIc Cs|d7}tt|ƒƒ}||||dk}|dkr•|||}|dkrk|d|9}nt|d| ƒ}t|d|ƒ}nd}|rúttt|ƒƒƒd}||dkrñt|t||ƒd|ƒ}qd}nd}t||dƒS)z´Given 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.r+r-r%rœr)rcr^r¶r¼rer½) r*rªr r+r|r~r¾rZ f_log_tenr'r'r)rF¡s"   $ rFc@s.eZdZdZdd„Zdd„ZdS)Ú _Log10Memoizez¾Class 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__.cCs d|_dS)NZ/23025850929940456840179914546843642076011014886)ru)r.r'r'r)r„Ñsz_Log10Memoize.__init__cCsÔ|dkrtdƒ‚n|t|jƒkr¹d}xad||d}tttd||ƒdƒƒ}|| d…d|krŒPn|d7}q9W|jdƒdd …|_nt|jd|d …ƒS) ztGiven an integer p >= 0, return floor(10**p)*log(10). For example, self.getdigits(3) returns 2302. r%zp should be nonnegativerYrœr+rNrPr-r�)rircrur^r¶r¼r³rR)r.r rr·rur'r'r)Ú getdigitsÔs "z_Log10Memoize.getdigitsN)r1r2r3r4r„rÀr'r'r'r)r¿Ís  r¿c CsÜt||>|ƒ}tdtt|ƒƒd|ƒ }t||ƒ}||>}x9t|dddƒD]!}t|||||ƒ}qiWxCt|ddd ƒD]+}||d>}t||||ƒ}q¥W||S) zëGiven 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œrYr-r%r+iöÿÿÿr�r�r�)rrRrcr^r¶rû) r r·r¸r¹rºrZMshiftrÿr~r'r'r)Ú_iexpòs% rÁc CsÃ|d7}td|tt|ƒƒdƒ}||}||}|dkr^|d|}n|d| }t|t|ƒƒ\}}t|d|ƒ}tt|d|ƒdƒ||dfS)aÐCompute 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.r+r%r-rœièrY)rºrcr^rÅr½r¶rÁ) r*rªr rrÌrÇZcshiftZquotrr'r'r)r8s #   r8c Cs*ttt|ƒƒƒ|}t||||dƒ}||}|dkra||d|}nt||d| ƒ}|dkrætt|ƒƒ|dk|dkkrÍd|ddd|} } q d|d| } } n:t||d |dƒ\} } t| dƒ} | d7} | | 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. r-r%rœ)rcr^rerFr¶r8) r r rrr rRZlxcrÇZpcrÆrTr'r'r)r;s   ( ! rrrÂéFÚ2é5Ú3é(Ú4éÚ5r@Ú6éÚ7rœÚ8rwr>cCsA|dkrtdƒ‚nt|ƒ}dt|ƒ||dS)z@Compute a lower bound for 100*log10(c) for a positive integer c.r%z0The argument to _log10_lb should be nonnegative.r)rir^rc)r*Z correctionZstr_cr'r'r)r es  r cCskt|tƒr|St|tƒr,t|ƒS|rNt|tƒrNtj|ƒS|rgtd|ƒ‚ntS)zÙConvert 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). zUnable to convert %s to Decimal)r]rrRrmrnror’)r„r™Z allow_floatr'r'r)ršps  ršcCst|tƒr||fSt|tjƒrx|jset|jtt|j ƒ|j ƒ|j ƒ}n|t|j ƒfS|r«t|tj ƒr«|jdkr«|j}nt|tƒrütƒ}|rÙd|jt[-+])? # 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 cCs+tj|ƒ}|dkr.td|ƒ‚n|jƒ}|d}|d}|ddk |d<|dr­|dk r‹td|ƒ‚n|dk r­td|ƒ‚q­n|p¶d|d<|pÆd |d<|d dkrêd |d ', '=' 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: ÚfillÚalignÚzeropadz7Fill character conflicts with '0' in format specifier: z2Alignment conflicts with '0' in format specifier: ú ú>r=rQÚ minimumwidthrPrrr%rjZgGnr-r5rpÚ thousands_sepzJExplicit thousands separator conflicts with 'n' type in format specifier: ÚgroupingÚ decimal_pointrSrYr©)Ú_parse_format_specifier_regexÚmatchriÚ groupdictrRÚ_localeÚ localeconv)Z format_specrnrrZ format_dictrÑrÒr'r'r)rtsN               rtc Csè|d}|d}||t|ƒt|ƒ}|d}|dkrY|||}n‹|dkrv|||}nn|dkr“|||}nQ|dkrØt|ƒd}|d |…||||d …}n td ƒ‚|S) zÜGiven 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). rÖrÑrÒúqsn                    &           .ÿÿÿÿÿÿÿÿÿÿÿÿÞÿÿÿÿÿÿà     0 " ,# % $ *#( *          P  % )